Skip to content

Commit 9902462

Browse files
author
Yui T
committed
Fix emit destructuring in declaration file
1 parent 42a0c34 commit 9902462

File tree

4 files changed

+134
-12
lines changed

4 files changed

+134
-12
lines changed

src/compiler/emitter.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,19 +1042,35 @@ module ts {
10421042
// If we are emitting property it isn't moduleElement and hence we already know it needs to be emitted
10431043
// so there is no check needed to see if declaration is visible
10441044
if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) {
1045-
// If this node is a computed name, it can only be a symbol, because we've already skipped
1046-
// it if it's not a well known symbol. In that case, the text of the name will be exactly
1047-
// what we want, namely the name expression enclosed in brackets.
1048-
writeTextOfNode(currentSourceFile, node.name);
1049-
// If optional property emit ?
1050-
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && hasQuestionToken(node)) {
1051-
write("?");
1052-
}
1053-
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && node.parent.kind === SyntaxKind.TypeLiteral) {
1054-
emitTypeOfVariableDeclarationFromTypeLiteral(node);
1045+
// If the node.name is an object-binding-pattern, decent into each element and emit each element as variable declarations
1046+
if (node.name.kind === SyntaxKind.ObjectBindingPattern || node.name.kind === SyntaxKind.ArrayBindingPattern) {
1047+
let elements = (<BindingPattern>node.name).elements;
1048+
for (let i = 0, n = elements.length; i < n; ++i) {
1049+
let element = elements[i];
1050+
writeTextOfNode(currentSourceFile, element);
1051+
writeTypeOfDeclaration(element, undefined, getVariableDeclarationTypeVisibilityError);
1052+
// Do not emit comma if the element is the last element in the list
1053+
if (i < n - 1) {
1054+
write(", ");
1055+
}
1056+
}
10551057
}
1056-
else if (!(node.flags & NodeFlags.Private)) {
1057-
writeTypeOfDeclaration(node, node.type, getVariableDeclarationTypeVisibilityError);
1058+
else {
1059+
// If this node is a computed name, it can only be a symbol, because we've already skipped
1060+
// it if it's not a well known symbol. In that case, the text of the name will be exactly
1061+
// what we want, namely the name expression enclosed in brackets.
1062+
writeTextOfNode(currentSourceFile, node.name);
1063+
1064+
// If optional property emit ?
1065+
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && hasQuestionToken(node)) {
1066+
write("?");
1067+
}
1068+
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && node.parent.kind === SyntaxKind.TypeLiteral) {
1069+
emitTypeOfVariableDeclarationFromTypeLiteral(node);
1070+
}
1071+
else if (!(node.flags & NodeFlags.Private)) {
1072+
writeTypeOfDeclaration(node, node.type, getVariableDeclarationTypeVisibilityError);
1073+
}
10581074
}
10591075
}
10601076

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [emitDestructuringInDeclarationFile.ts]
2+
var {a, b} = { a:10, b:"10"};
3+
var {c, d}: {c:string, d:string} = { c:"true", d:"false" };
4+
let {k, j} = { k:true, j:{a:10}};
5+
6+
var a1 = ["string", 10];
7+
var [x] = a1;
8+
var [x, y] = a1;
9+
var [x, y, z] = a1;
10+
let [x1] = a1;
11+
12+
//// [emitDestructuringInDeclarationFile.js]
13+
var _a = {
14+
a: 10,
15+
b: "10"
16+
}, a = _a.a, b = _a.b;
17+
var _b = {
18+
c: "true",
19+
d: "false"
20+
}, c = _b.c, d = _b.d;
21+
var _c = {
22+
k: true,
23+
j: {
24+
a: 10
25+
}
26+
}, k = _c.k, j = _c.j;
27+
var a1 = [
28+
"string",
29+
10
30+
];
31+
var x = a1[0];
32+
var x = a1[0], y = a1[1];
33+
var x = a1[0], y = a1[1], z = a1[2];
34+
var x1 = a1[0];
35+
36+
37+
//// [emitDestructuringInDeclarationFile.d.ts]
38+
declare var a: number, b: string;
39+
declare var c: string, d: string;
40+
declare let k: boolean, j: {
41+
a: number;
42+
};
43+
declare var a1: (string | number)[];
44+
declare var x: string | number;
45+
declare var x: string | number, y: string | number;
46+
declare var x: string | number, y: string | number, z: string | number;
47+
declare let x1: string | number;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
=== tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile.ts ===
2+
var {a, b} = { a:10, b:"10"};
3+
>a : number
4+
>b : string
5+
>{ a:10, b:"10"} : { a: number; b: string; }
6+
>a : number
7+
>b : string
8+
9+
var {c, d}: {c:string, d:string} = { c:"true", d:"false" };
10+
>c : string
11+
>d : string
12+
>c : string
13+
>d : string
14+
>{ c:"true", d:"false" } : { c: string; d: string; }
15+
>c : string
16+
>d : string
17+
18+
let {k, j} = { k:true, j:{a:10}};
19+
>k : boolean
20+
>j : { a: number; }
21+
>{ k:true, j:{a:10}} : { k: boolean; j: { a: number; }; }
22+
>k : boolean
23+
>j : { a: number; }
24+
>{a:10} : { a: number; }
25+
>a : number
26+
27+
var a1 = ["string", 10];
28+
>a1 : (string | number)[]
29+
>["string", 10] : (string | number)[]
30+
31+
var [x] = a1;
32+
>x : string | number
33+
>a1 : (string | number)[]
34+
35+
var [x, y] = a1;
36+
>x : string | number
37+
>y : string | number
38+
>a1 : (string | number)[]
39+
40+
var [x, y, z] = a1;
41+
>x : string | number
42+
>y : string | number
43+
>z : string | number
44+
>a1 : (string | number)[]
45+
46+
let [x1] = a1;
47+
>x1 : string | number
48+
>a1 : (string | number)[]
49+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @declaration: true
2+
var {a, b} = { a:10, b:"10"};
3+
var {c, d}: {c:string, d:string} = { c:"true", d:"false" };
4+
let {k, j} = { k:true, j:{a:10}};
5+
6+
var a1 = ["string", 10];
7+
var [x] = a1;
8+
var [x, y] = a1;
9+
var [x, y, z] = a1;
10+
let [x1] = a1;

0 commit comments

Comments
 (0)