Skip to content

Commit 1f055b9

Browse files
author
Yui T
committed
Emit ommittedExpression in binding pattern
1 parent 1c2eae6 commit 1f055b9

File tree

4 files changed

+109
-29
lines changed

4 files changed

+109
-29
lines changed

src/compiler/declarationEmitter.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,11 @@ module ts {
13871387
}
13881388
else if (bindingPattern.kind === SyntaxKind.ArrayBindingPattern) {
13891389
write("[");
1390-
emitCommaList(bindingPattern.elements, emitBindingElement);
1390+
let elements = bindingPattern.elements;
1391+
emitCommaList(elements, emitBindingElement);
1392+
if (elements && elements.hasTrailingComma) {
1393+
write(", ");
1394+
}
13911395
write("]");
13921396
}
13931397
}
@@ -1402,40 +1406,51 @@ module ts {
14021406
} : undefined;
14031407
}
14041408

1405-
if (bindingElement.propertyName) {
1406-
// bindingElement has propertyName property in the following case:
1407-
// { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y"
1408-
// We have to explicitly emit the propertyName before descending into its binding elements.
1409+
if (bindingElement.kind === SyntaxKind.OmittedExpression) {
1410+
// If bindingElement is an omittedExpression (i.e. containing elision),
1411+
// we will emit blank space (although this may differ from users' original code,
1412+
// it allows emitSeparatedList to write separator appropriately)
14091413
// Example:
1410-
// original: function foo({y: [a,b,c]}) {}
1411-
// emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void;
1412-
writeTextOfNode(currentSourceFile, bindingElement.propertyName);
1413-
write(": ");
1414-
1415-
// If bindingElement has propertyName property, then its name must be another bindingPattern of SyntaxKind.ObjectBindingPattern
1416-
emitBindingPattern(<BindingPattern>bindingElement.name);
1414+
// original: function foo([, x, ,]) {}
1415+
// emit : function foo([ , x, , ]) {}
1416+
write(" ");
14171417
}
1418-
else if (bindingElement.name) {
1419-
if (isBindingPattern(bindingElement.name)) {
1420-
// If it is a nested binding pattern, we will recursively descend into each element and emit each one separately.
1421-
// In the case of rest element, we will omit rest element.
1418+
else if (bindingElement.kind === SyntaxKind.BindingElement) {
1419+
if (bindingElement.propertyName) {
1420+
// bindingElement has propertyName property in the following case:
1421+
// { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y"
1422+
// We have to explicitly emit the propertyName before descending into its binding elements.
14221423
// Example:
1423-
// original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {}
1424-
// emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void;
1425-
// original with rest: function foo([a, ...c]) {}
1426-
// emit : declare function foo([a, ...c]): void;
1424+
// original: function foo({y: [a,b,c]}) {}
1425+
// emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void;
1426+
writeTextOfNode(currentSourceFile, bindingElement.propertyName);
1427+
write(": ");
1428+
1429+
// If bindingElement has propertyName property, then its name must be another bindingPattern of SyntaxKind.ObjectBindingPattern
14271430
emitBindingPattern(<BindingPattern>bindingElement.name);
14281431
}
1429-
else {
1430-
Debug.assert(bindingElement.name.kind === SyntaxKind.Identifier);
1431-
// If the node is just an identifier, we will simply emit the text associated with the node's name
1432-
// Example:
1433-
// original: function foo({y = 10, x}) {}
1434-
// emit : declare function foo({y, x}: {number, any}): void;
1435-
if (bindingElement.dotDotDotToken) {
1436-
write("...");
1432+
else if (bindingElement.name) {
1433+
if (isBindingPattern(bindingElement.name)) {
1434+
// If it is a nested binding pattern, we will recursively descend into each element and emit each one separately.
1435+
// In the case of rest element, we will omit rest element.
1436+
// Example:
1437+
// original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {}
1438+
// emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void;
1439+
// original with rest: function foo([a, ...c]) {}
1440+
// emit : declare function foo([a, ...c]): void;
1441+
emitBindingPattern(<BindingPattern>bindingElement.name);
1442+
}
1443+
else {
1444+
Debug.assert(bindingElement.name.kind === SyntaxKind.Identifier);
1445+
// If the node is just an identifier, we will simply emit the text associated with the node's name
1446+
// Example:
1447+
// original: function foo({y = 10, x}) {}
1448+
// emit : declare function foo({y, x}: {number, any}): void;
1449+
if (bindingElement.dotDotDotToken) {
1450+
write("...");
1451+
}
1452+
writeTextOfNode(currentSourceFile, bindingElement.name);
14371453
}
1438-
writeTextOfNode(currentSourceFile, bindingElement.name);
14391454
}
14401455
}
14411456
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [declarationEmitDestructuring5.ts]
2+
function baz([, z, , ]) { }
3+
function foo([, b, ]: [any, any]): void { }
4+
function bar([z, , , ]) { }
5+
function bar1([z, , , ] = [1, 3, 4, 6, 7]) { }
6+
function bar2([,,z, , , ]) { }
7+
8+
//// [declarationEmitDestructuring5.js]
9+
function baz(_a) {
10+
var z = _a[1];
11+
}
12+
function foo(_a) {
13+
var b = _a[1];
14+
}
15+
function bar(_a) {
16+
var z = _a[0];
17+
}
18+
function bar1(_a) {
19+
var _b = _a === void 0 ? [
20+
1,
21+
3,
22+
4,
23+
6,
24+
7
25+
] : _a, z = _b[0];
26+
}
27+
function bar2(_a) {
28+
var z = _a[2];
29+
}
30+
31+
32+
//// [declarationEmitDestructuring5.d.ts]
33+
declare function baz([ , z, , ]: [any, any, any]): void;
34+
declare function foo([ , b, ]: [any, any]): void;
35+
declare function bar([z, , , ]: [any, any, any]): void;
36+
declare function bar1([z, , , ]?: [number, number, number, number, number]): void;
37+
declare function bar2([ , , z, , , ]: [any, any, any, any, any]): void;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/declarationEmitDestructuring5.ts ===
2+
function baz([, z, , ]) { }
3+
>baz : ([, z, , ]: [any, any, any]) => void
4+
>z : any
5+
6+
function foo([, b, ]: [any, any]): void { }
7+
>foo : ([, b, ]: [any, any]) => void
8+
>b : any
9+
10+
function bar([z, , , ]) { }
11+
>bar : ([z, , , ]: [any, any, any]) => void
12+
>z : any
13+
14+
function bar1([z, , , ] = [1, 3, 4, 6, 7]) { }
15+
>bar1 : ([z, , , ]?: [number, number, number, number, number]) => void
16+
>z : number
17+
>[1, 3, 4, 6, 7] : [number, number, number, number, number]
18+
19+
function bar2([,,z, , , ]) { }
20+
>bar2 : ([,,z, , , ]: [any, any, any, any, any]) => void
21+
>z : any
22+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @declaration: true
2+
function baz([, z, , ]) { }
3+
function foo([, b, ]: [any, any]): void { }
4+
function bar([z, , , ]) { }
5+
function bar1([z, , , ] = [1, 3, 4, 6, 7]) { }
6+
function bar2([,,z, , , ]) { }

0 commit comments

Comments
 (0)