From 36ea7c8d7725b978d32da5d5975faad5cb96a9f8 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 19 Mar 2015 14:36:30 -0700 Subject: [PATCH 01/10] Emit destructuring in parameter --- src/compiler/declarationEmitter.ts | 3 +- ...clarationEmitDestructuringArrayPattern5.js | 37 +++++++++++++++++++ ...rationEmitDestructuringArrayPattern5.types | 33 +++++++++++++++++ ...ingOptionalBindingParametersInOverloads.js | 4 +- ...ionEmitDestructuringParameterProperties.js | 6 +-- ...tructuringWithOptionalBindingParameters.js | 4 +- ...clarationEmitDestructuringArrayPattern5.ts | 5 +++ 7 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types create mode 100644 tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index cf75467e1c5b4..6509ae6e2c415 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -1291,7 +1291,8 @@ module ts { write("..."); } if (isBindingPattern(node.name)) { - write("_" + indexOf((node.parent).parameters, node)); + // By emitting binding pattern as binding pattern in function parameters, language service can provide better signature help + write(getTextOfNode(node.name)); } else { writeTextOfNode(currentSourceFile, node.name); diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js new file mode 100644 index 0000000000000..9e8dee3285c0c --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js @@ -0,0 +1,37 @@ +//// [declarationEmitDestructuringArrayPattern5.ts] +function foo([a, b, c]: [string, string, string]): void { } +function far([a, [b], [[c]]]: [number, boolean[], string[][]]): void { } +function bar({a1, b1, c1}: { a1: number, b1: boolean, c1: string }): void { } +function baz({a2, b2: {b1, c1}}: { a2: number, b2: { b1: boolean, c1: string } }): void { } + + +//// [declarationEmitDestructuringArrayPattern5.js] +function foo(_a) { + var a = _a[0], b = _a[1], c = _a[2]; +} +function far(_a) { + var a = _a[0], b = _a[1][0], c = _a[2][0][0]; +} +function bar(_a) { + var a1 = _a.a1, b1 = _a.b1, c1 = _a.c1; +} +function baz(_a) { + var a2 = _a.a2, _b = _a.b2, b1 = _b.b1, c1 = _b.c1; +} + + +//// [declarationEmitDestructuringArrayPattern5.d.ts] +declare function foo([a, b, c]: [string, string, string]): void; +declare function far([a, [b], [[c]]]: [number, boolean[], string[][]]): void; +declare function bar({a1, b1, c1}: { + a1: number; + b1: boolean; + c1: string; +}): void; +declare function baz({a2, b2: {b1, c1}}: { + a2: number; + b2: { + b1: boolean; + c1: string; + }; +}): void; diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types new file mode 100644 index 0000000000000..fdb2bba88ea98 --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types @@ -0,0 +1,33 @@ +=== tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts === +function foo([a, b, c]: [string, string, string]): void { } +>foo : ([a, b, c]: [string, string, string]) => void +>a : string +>b : string +>c : string + +function far([a, [b], [[c]]]: [number, boolean[], string[][]]): void { } +>far : ([a, [b], [[c]]]: [number, boolean[], string[][]]) => void +>a : number +>b : boolean +>c : string + +function bar({a1, b1, c1}: { a1: number, b1: boolean, c1: string }): void { } +>bar : ({a1, b1, c1}: { a1: number; b1: boolean; c1: string; }) => void +>a1 : number +>b1 : boolean +>c1 : string +>a1 : number +>b1 : boolean +>c1 : string + +function baz({a2, b2: {b1, c1}}: { a2: number, b2: { b1: boolean, c1: string } }): void { } +>baz : ({a2, b2: {b1, c1}}: { a2: number; b2: { b1: boolean; c1: string; }; }) => void +>a2 : number +>b2 : unknown +>b1 : boolean +>c1 : string +>a2 : number +>b2 : { b1: boolean; c1: string; } +>b1 : boolean +>c1 : string + diff --git a/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js b/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js index c907add11f776..d44c9d4236319 100644 --- a/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js +++ b/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js @@ -25,8 +25,8 @@ function foo2() { //// [declarationEmitDestructuringOptionalBindingParametersInOverloads.d.ts] -declare function foo(_0?: [string, number, boolean]): any; -declare function foo2(_0?: { +declare function foo([x, y, z]?: [string, number, boolean]): any; +declare function foo2({ x, y, z }?: { x: string; y: number; z: boolean; diff --git a/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js b/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js index cfe4acc1733d0..841508c331e2a 100644 --- a/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js +++ b/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js @@ -43,12 +43,12 @@ var C3 = (function () { //// [declarationEmitDestructuringParameterProperties.d.ts] declare class C1 { x: string, y: string, z: string; - constructor(_0: string[]); + constructor([x, y, z]: string[]); } declare type TupleType1 = [string, number, boolean]; declare class C2 { x: string, y: number, z: boolean; - constructor(_0: TupleType1); + constructor([x, y, z]: TupleType1); } declare type ObjType1 = { x: number; @@ -57,5 +57,5 @@ declare type ObjType1 = { }; declare class C3 { x: number, y: string, z: boolean; - constructor(_0: ObjType1); + constructor({ x, y, z }: ObjType1); } diff --git a/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js b/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js index 284cf107782f0..9ffd25a7fa840 100644 --- a/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js +++ b/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js @@ -14,8 +14,8 @@ function foo1(_a) { //// [declarationEmitDestructuringWithOptionalBindingParameters.d.ts] -declare function foo(_0?: [string, number, boolean]): void; -declare function foo1(_0?: { +declare function foo([x,y,z]?: [string, number, boolean]): void; +declare function foo1({ x, y, z }?: { x: string; y: number; z: boolean; diff --git a/tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts b/tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts new file mode 100644 index 0000000000000..c610e24f93301 --- /dev/null +++ b/tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts @@ -0,0 +1,5 @@ +// @declaration: true +function foo([a, b, c]: [string, string, string]): void { } +function far([a, [b], [[c]]]: [number, boolean[], string[][]]): void { } +function bar({a1, b1, c1}: { a1: number, b1: boolean, c1: string }): void { } +function baz({a2, b2: {b1, c1}}: { a2: number, b2: { b1: boolean, c1: string } }): void { } From b5065f1f3aaa9817b9e5c060bb30927714faeb0f Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 19 Mar 2015 14:42:57 -0700 Subject: [PATCH 02/10] Allow destructuring in ambient context --- src/compiler/checker.ts | 3 --- .../reference/declarationInAmbientContext.errors.txt | 12 ------------ .../reference/declarationInAmbientContext.types | 9 +++++++++ 3 files changed, 9 insertions(+), 15 deletions(-) delete mode 100644 tests/baselines/reference/declarationInAmbientContext.errors.txt create mode 100644 tests/baselines/reference/declarationInAmbientContext.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d50e440c5e935..f67b721465490 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12005,9 +12005,6 @@ module ts { function checkGrammarVariableDeclaration(node: VariableDeclaration) { if (node.parent.parent.kind !== SyntaxKind.ForInStatement && node.parent.parent.kind !== SyntaxKind.ForOfStatement) { if (isInAmbientContext(node)) { - if (isBindingPattern(node.name)) { - return grammarErrorOnNode(node, Diagnostics.Destructuring_declarations_are_not_allowed_in_ambient_contexts); - } if (node.initializer) { // Error on equals token which immediate precedes the initializer let equalsTokenLength = "=".length; diff --git a/tests/baselines/reference/declarationInAmbientContext.errors.txt b/tests/baselines/reference/declarationInAmbientContext.errors.txt deleted file mode 100644 index 338da990a0b39..0000000000000 --- a/tests/baselines/reference/declarationInAmbientContext.errors.txt +++ /dev/null @@ -1,12 +0,0 @@ -tests/cases/conformance/es6/destructuring/declarationInAmbientContext.ts(1,13): error TS1183: Destructuring declarations are not allowed in ambient contexts. -tests/cases/conformance/es6/destructuring/declarationInAmbientContext.ts(2,13): error TS1183: Destructuring declarations are not allowed in ambient contexts. - - -==== tests/cases/conformance/es6/destructuring/declarationInAmbientContext.ts (2 errors) ==== - declare var [a, b]; // Error, destructuring declaration not allowed in ambient context - ~~~~~~ -!!! error TS1183: Destructuring declarations are not allowed in ambient contexts. - declare var {c, d}; // Error, destructuring declaration not allowed in ambient context - ~~~~~~ -!!! error TS1183: Destructuring declarations are not allowed in ambient contexts. - \ No newline at end of file diff --git a/tests/baselines/reference/declarationInAmbientContext.types b/tests/baselines/reference/declarationInAmbientContext.types new file mode 100644 index 0000000000000..ecdd3b7c7ebbb --- /dev/null +++ b/tests/baselines/reference/declarationInAmbientContext.types @@ -0,0 +1,9 @@ +=== tests/cases/conformance/es6/destructuring/declarationInAmbientContext.ts === +declare var [a, b]; // Error, destructuring declaration not allowed in ambient context +>a : any +>b : any + +declare var {c, d}; // Error, destructuring declaration not allowed in ambient context +>c : any +>d : any + From 5979dacf4f0278278b42749aa2ae7f146326bdac Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 23 Mar 2015 11:30:51 -0700 Subject: [PATCH 03/10] Correctly emit bidning pattern with initializer and rest --- src/compiler/declarationEmitter.ts | 97 +++++++++++++++---- ...clarationEmitDestructuringArrayPattern6.js | 65 +++++++++++++ ...rationEmitDestructuringArrayPattern6.types | 49 ++++++++++ ...clarationEmitDestructuringArrayPattern7.js | 20 ++++ ...rationEmitDestructuringArrayPattern7.types | 13 +++ ...ingOptionalBindingParametersInOverloads.js | 2 +- ...ionEmitDestructuringParameterProperties.js | 2 +- ...tructuringWithOptionalBindingParameters.js | 4 +- ...clarationEmitDestructuringArrayPattern6.ts | 5 + ...clarationEmitDestructuringArrayPattern7.ts | 3 + 10 files changed, 236 insertions(+), 24 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern6.js create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern6.types create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern7.types create mode 100644 tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts create mode 100644 tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 6509ae6e2c415..93e0fa3129713 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -1291,8 +1291,10 @@ module ts { write("..."); } if (isBindingPattern(node.name)) { - // By emitting binding pattern as binding pattern in function parameters, language service can provide better signature help - write(getTextOfNode(node.name)); + // For bindingPattern, we can't simply writeTextOfNode from the source file + // because we want to omit the initializer and using writeTextOfNode will result in initializer get emitted. + // Therefore, we will have to recursively emit each element in the bindingPattern. + emitBindingPattern(node.name); } else { writeTextOfNode(currentSourceFile, node.name); @@ -1312,41 +1314,46 @@ module ts { } function getParameterDeclarationTypeVisibilityError(symbolAccesibilityResult: SymbolAccessiblityResult): SymbolAccessibilityDiagnostic { - let diagnosticMessage: DiagnosticMessage; + let diagnosticMessage: DiagnosticMessage = getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccesibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage, + errorNode: node, + typeName: node.name + } : undefined; + } + + function getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccesibilityResult: SymbolAccessiblityResult) { switch (node.parent.kind) { case SyntaxKind.Constructor: - diagnosticMessage = symbolAccesibilityResult.errorModuleName ? + return symbolAccesibilityResult.errorModuleName ? symbolAccesibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ? Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_private_module_2 : Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_private_name_1; - break; case SyntaxKind.ConstructSignature: // Interfaces cannot have parameter types that cannot be named - diagnosticMessage = symbolAccesibilityResult.errorModuleName ? + return symbolAccesibilityResult.errorModuleName ? Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_1; - break; case SyntaxKind.CallSignature: // Interfaces cannot have parameter types that cannot be named - diagnosticMessage = symbolAccesibilityResult.errorModuleName ? + return symbolAccesibilityResult.errorModuleName ? Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_name_1; - break; case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: if (node.parent.flags & NodeFlags.Static) { - diagnosticMessage = symbolAccesibilityResult.errorModuleName ? + return symbolAccesibilityResult.errorModuleName ? symbolAccesibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ? Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_name_1; } else if (node.parent.parent.kind === SyntaxKind.ClassDeclaration) { - diagnosticMessage = symbolAccesibilityResult.errorModuleName ? + return symbolAccesibilityResult.errorModuleName ? symbolAccesibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ? Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : @@ -1354,30 +1361,80 @@ module ts { } else { // Interfaces cannot have parameter types that cannot be named - diagnosticMessage = symbolAccesibilityResult.errorModuleName ? + return symbolAccesibilityResult.errorModuleName ? Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_private_name_1; } - break; case SyntaxKind.FunctionDeclaration: - diagnosticMessage = symbolAccesibilityResult.errorModuleName ? + return symbolAccesibilityResult.errorModuleName ? symbolAccesibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ? Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_private_module_2 : Diagnostics.Parameter_0_of_exported_function_has_or_is_using_private_name_1; - break; default: Debug.fail("This is unknown parent for parameter: " + node.parent.kind); } + } - return { - diagnosticMessage, - errorNode: node, - typeName: node.name - }; + function emitBindingPattern(bindingPattern: BindingPattern) { + // We have to explicitly emit square bracket and bracket because these tokens are not store inside the node. + if (bindingPattern.kind === SyntaxKind.ObjectBindingPattern) { + write("{"); + emitCommaList(bindingPattern.elements, emitBindingElement); + write("}"); + } + else if (bindingPattern.kind === SyntaxKind.ArrayBindingPattern) { + write("["); + emitCommaList(bindingPattern.elements, emitBindingElement); + write("]"); + } } + + function emitBindingElement(bindingElement: BindingElement) { + function getBindingElementTypeVisibilityError(symbolAccesibilityResult: SymbolAccessiblityResult): SymbolAccessibilityDiagnostic { + let diagnosticMessage = getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccesibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage, + errorNode: bindingElement, + typeName: bindingElement.name + } : undefined; + } + + if (bindingElement.propertyName) { + // bindingElement has propertyName property in the following case: + // { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y" + // We have to explicitly emit the propertyName before descending into its binding elements. + // Example: + // original: function foo({y: [a,b,c]}) {} + // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; + writeTextOfNode(currentSourceFile, bindingElement.propertyName); + write(": "); + + // If bindingElement has propertyName property, then its name must be another bindingPattern of SyntaxKind.ObjectBindingPattern + emitBindingPattern(bindingElement.name); + } + else if (bindingElement.name) { + if (isBindingPattern(bindingElement.name)) { + // If it is a nested binding pattern, we will recursively descend into each element and emit each one separately. + // In the case of rest element, we will omit rest element. + // Example: + // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} + // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; + // original with rest: function foo([a ...c]) {} + // emit : declare function foo([a, c]): void; + emitBindingPattern(bindingElement.name); + } + else { + // If the node is just an identifier, we will simply emit the text associated with the node's name + // Example: + // original: function foo({y = 10, x}) {} + // emit : declare function foo({y, x}: {number, any}): void; + writeTextOfNode(currentSourceFile, bindingElement.name); + } + } + } } function emitNode(node: Node) { diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.js b/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.js new file mode 100644 index 0000000000000..803ea9fe01a1d --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.js @@ -0,0 +1,65 @@ +//// [declarationEmitDestructuringArrayPattern6.ts] +function f({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]} = { x: 10, y: [2, 4, 6, 8] }) { } +function g([a, b, c, d] = [1, 2, 3, 4]) { } +function h([a, [b], [[c]], {x = 10, y: [a, b, c], z: {a1, b1}}]){ } +function h1([a, [b], [[c]], {x = 10, y = [1, 2, 3], z: {a1, b1}}]){ } + +//// [declarationEmitDestructuringArrayPattern6.js] +function f(_a) { + var _b = _a === void 0 ? { + x: 10, + y: [ + 2, + 4, + 6, + 8 + ] + } : _a, _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, _e = _d === void 0 ? [ + 1, + 2, + 3, + 4 + ] : _d, a = _e[0], b = _e[1], c = _e[2], d = _e[3]; +} +function g(_a) { + var _b = _a === void 0 ? [ + 1, + 2, + 3, + 4 + ] : _a, a = _b[0], b = _b[1], c = _b[2], d = _b[3]; +} +function h(_a) { + var a = _a[0], b = _a[1][0], c = _a[2][0][0], _b = _a[3], _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, a = _d[0], b = _d[1], c = _d[2], _e = _b.z, a1 = _e.a1, b1 = _e.b1; +} +function h1(_a) { + var a = _a[0], b = _a[1][0], c = _a[2][0][0], _b = _a[3], _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, y = _d === void 0 ? [ + 1, + 2, + 3 + ] : _d, _e = _b.z, a1 = _e.a1, b1 = _e.b1; +} + + +//// [declarationEmitDestructuringArrayPattern6.d.ts] +declare function f({x, y: [a, b, c, d]}?: { + x: number; + y: [number, number, number, number]; +}): void; +declare function g([a, b, c, d]?: [number, number, number, number]): void; +declare function h([a, [b], [[c]], {x, y: [a, b, c], z: {a1, b1}}]: [any, [any], [[any]], { + x?: number; + y: [any, any, any]; + z: { + a1: any; + b1: any; + }; +}]): void; +declare function h1([a, [b], [[c]], {x, y, z: {a1, b1}}]: [any, [any], [[any]], { + x?: number; + y?: number[]; + z: { + a1: any; + b1: any; + }; +}]): void; diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.types b/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.types new file mode 100644 index 0000000000000..6112094cbe550 --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.types @@ -0,0 +1,49 @@ +=== tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts === +function f({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]} = { x: 10, y: [2, 4, 6, 8] }) { } +>f : ({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]}?: { x: number; y: [number, number, number, number]; }) => void +>x : number +>y : unknown +>a : number +>b : number +>c : number +>d : number +>[1, 2, 3, 4] : [number, number, number, number] +>{ x: 10, y: [2, 4, 6, 8] } : { x: number; y: [number, number, number, number]; } +>x : number +>y : [number, number, number, number] +>[2, 4, 6, 8] : [number, number, number, number] + +function g([a, b, c, d] = [1, 2, 3, 4]) { } +>g : ([a, b, c, d]?: [number, number, number, number]) => void +>a : number +>b : number +>c : number +>d : number +>[1, 2, 3, 4] : [number, number, number, number] + +function h([a, [b], [[c]], {x = 10, y: [a, b, c], z: {a1, b1}}]){ } +>h : ([a, [b], [[c]], {x = 10, y: [a, b, c], z: {a1, b1}}]: [any, [any], [[any]], { x?: number; y: [any, any, any]; z: { a1: any; b1: any; }; }]) => void +>a : any +>b : any +>c : any +>x : number +>y : unknown +>a : any +>b : any +>c : any +>z : unknown +>a1 : any +>b1 : any + +function h1([a, [b], [[c]], {x = 10, y = [1, 2, 3], z: {a1, b1}}]){ } +>h1 : ([a, [b], [[c]], {x = 10, y = [1, 2, 3], z: {a1, b1}}]: [any, [any], [[any]], { x?: number; y?: number[]; z: { a1: any; b1: any; }; }]) => void +>a : any +>b : any +>c : any +>x : number +>y : number[] +>[1, 2, 3] : number[] +>z : unknown +>a1 : any +>b1 : any + diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js b/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js new file mode 100644 index 0000000000000..00e65b816b05e --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js @@ -0,0 +1,20 @@ +//// [declarationEmitDestructuringArrayPattern7.ts] +function bar([x, z, ...w]) { } +function foo([x, ...y] = [1, "string", true]) { } + +//// [declarationEmitDestructuringArrayPattern7.js] +function bar(_a) { + var x = _a[0], z = _a[1], w = _a.slice(2); +} +function foo(_a) { + var _b = _a === void 0 ? [ + 1, + "string", + true + ] : _a, x = _b[0], y = _b.slice(1); +} + + +//// [declarationEmitDestructuringArrayPattern7.d.ts] +declare function bar([x, z, w]: any[]): void; +declare function foo([x, y]?: (string | number | boolean)[]): void; diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.types b/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.types new file mode 100644 index 0000000000000..8dc51bf5c781e --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts === +function bar([x, z, ...w]) { } +>bar : ([x, z, ...w]: any[]) => void +>x : any +>z : any +>w : any[] + +function foo([x, ...y] = [1, "string", true]) { } +>foo : ([x, ...y]?: (string | number | boolean)[]) => void +>x : string | number | boolean +>y : (string | number | boolean)[] +>[1, "string", true] : (string | number | boolean)[] + diff --git a/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js b/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js index d44c9d4236319..a3cdee312222c 100644 --- a/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js +++ b/tests/baselines/reference/declarationEmitDestructuringOptionalBindingParametersInOverloads.js @@ -26,7 +26,7 @@ function foo2() { //// [declarationEmitDestructuringOptionalBindingParametersInOverloads.d.ts] declare function foo([x, y, z]?: [string, number, boolean]): any; -declare function foo2({ x, y, z }?: { +declare function foo2({x, y, z}?: { x: string; y: number; z: boolean; diff --git a/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js b/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js index 841508c331e2a..a98620c852902 100644 --- a/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js +++ b/tests/baselines/reference/declarationEmitDestructuringParameterProperties.js @@ -57,5 +57,5 @@ declare type ObjType1 = { }; declare class C3 { x: number, y: string, z: boolean; - constructor({ x, y, z }: ObjType1); + constructor({x, y, z}: ObjType1); } diff --git a/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js b/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js index 9ffd25a7fa840..5c7f4d2cec593 100644 --- a/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js +++ b/tests/baselines/reference/declarationEmitDestructuringWithOptionalBindingParameters.js @@ -14,8 +14,8 @@ function foo1(_a) { //// [declarationEmitDestructuringWithOptionalBindingParameters.d.ts] -declare function foo([x,y,z]?: [string, number, boolean]): void; -declare function foo1({ x, y, z }?: { +declare function foo([x, y, z]?: [string, number, boolean]): void; +declare function foo1({x, y, z}?: { x: string; y: number; z: boolean; diff --git a/tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts b/tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts new file mode 100644 index 0000000000000..34441cd6c7bd8 --- /dev/null +++ b/tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts @@ -0,0 +1,5 @@ +// @declaration: true +function f({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]} = { x: 10, y: [2, 4, 6, 8] }) { } +function g([a, b, c, d] = [1, 2, 3, 4]) { } +function h([a, [b], [[c]], {x = 10, y: [a, b, c], z: {a1, b1}}]){ } +function h1([a, [b], [[c]], {x = 10, y = [1, 2, 3], z: {a1, b1}}]){ } \ No newline at end of file diff --git a/tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts b/tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts new file mode 100644 index 0000000000000..321c6fd420275 --- /dev/null +++ b/tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts @@ -0,0 +1,3 @@ +// @declaration: true +function bar([x, z, ...w]) { } +function foo([x, ...y] = [1, "string", true]) { } \ No newline at end of file From 85624c03219d119f38a44debb0697c0839409200 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 23 Mar 2015 17:58:53 -0700 Subject: [PATCH 04/10] Change test files name --- ...n5.js => declarationEmitDestructuring1.js} | 6 +- ...es => declarationEmitDestructuring1.types} | 2 +- ...n6.js => declarationEmitDestructuring2.js} | 6 +- ...es => declarationEmitDestructuring2.types} | 2 +- .../declarationEmitDestructuring3.js | 22 ++++++ ...es => declarationEmitDestructuring3.types} | 3 +- .../declarationEmitDestructuring4.js | 67 +++++++++++++++++++ .../declarationEmitDestructuring4.types | 32 +++++++++ ...clarationEmitDestructuringArrayPattern7.js | 20 ------ ...n5.ts => declarationEmitDestructuring1.ts} | 0 ...n6.ts => declarationEmitDestructuring2.ts} | 0 .../compiler/declarationEmitDestructuring3.ts | 4 ++ .../compiler/declarationEmitDestructuring4.ts | 12 ++++ ...clarationEmitDestructuringArrayPattern7.ts | 3 - 14 files changed, 147 insertions(+), 32 deletions(-) rename tests/baselines/reference/{declarationEmitDestructuringArrayPattern5.js => declarationEmitDestructuring1.js} (83%) rename tests/baselines/reference/{declarationEmitDestructuringArrayPattern5.types => declarationEmitDestructuring1.types} (89%) rename tests/baselines/reference/{declarationEmitDestructuringArrayPattern6.js => declarationEmitDestructuring2.js} (88%) rename tests/baselines/reference/{declarationEmitDestructuringArrayPattern6.types => declarationEmitDestructuring2.types} (91%) create mode 100644 tests/baselines/reference/declarationEmitDestructuring3.js rename tests/baselines/reference/{declarationEmitDestructuringArrayPattern7.types => declarationEmitDestructuring3.types} (78%) create mode 100644 tests/baselines/reference/declarationEmitDestructuring4.js create mode 100644 tests/baselines/reference/declarationEmitDestructuring4.types delete mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js rename tests/cases/compiler/{declarationEmitDestructuringArrayPattern5.ts => declarationEmitDestructuring1.ts} (100%) rename tests/cases/compiler/{declarationEmitDestructuringArrayPattern6.ts => declarationEmitDestructuring2.ts} (100%) create mode 100644 tests/cases/compiler/declarationEmitDestructuring3.ts create mode 100644 tests/cases/compiler/declarationEmitDestructuring4.ts delete mode 100644 tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js b/tests/baselines/reference/declarationEmitDestructuring1.js similarity index 83% rename from tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js rename to tests/baselines/reference/declarationEmitDestructuring1.js index 9e8dee3285c0c..9ac08032a0dd2 100644 --- a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js +++ b/tests/baselines/reference/declarationEmitDestructuring1.js @@ -1,11 +1,11 @@ -//// [declarationEmitDestructuringArrayPattern5.ts] +//// [declarationEmitDestructuring1.ts] function foo([a, b, c]: [string, string, string]): void { } function far([a, [b], [[c]]]: [number, boolean[], string[][]]): void { } function bar({a1, b1, c1}: { a1: number, b1: boolean, c1: string }): void { } function baz({a2, b2: {b1, c1}}: { a2: number, b2: { b1: boolean, c1: string } }): void { } -//// [declarationEmitDestructuringArrayPattern5.js] +//// [declarationEmitDestructuring1.js] function foo(_a) { var a = _a[0], b = _a[1], c = _a[2]; } @@ -20,7 +20,7 @@ function baz(_a) { } -//// [declarationEmitDestructuringArrayPattern5.d.ts] +//// [declarationEmitDestructuring1.d.ts] declare function foo([a, b, c]: [string, string, string]): void; declare function far([a, [b], [[c]]]: [number, boolean[], string[][]]): void; declare function bar({a1, b1, c1}: { diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types b/tests/baselines/reference/declarationEmitDestructuring1.types similarity index 89% rename from tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types rename to tests/baselines/reference/declarationEmitDestructuring1.types index fdb2bba88ea98..6b22f25b54b21 100644 --- a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types +++ b/tests/baselines/reference/declarationEmitDestructuring1.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts === +=== tests/cases/compiler/declarationEmitDestructuring1.ts === function foo([a, b, c]: [string, string, string]): void { } >foo : ([a, b, c]: [string, string, string]) => void >a : string diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.js b/tests/baselines/reference/declarationEmitDestructuring2.js similarity index 88% rename from tests/baselines/reference/declarationEmitDestructuringArrayPattern6.js rename to tests/baselines/reference/declarationEmitDestructuring2.js index 803ea9fe01a1d..f6d3d17abe1c4 100644 --- a/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.js +++ b/tests/baselines/reference/declarationEmitDestructuring2.js @@ -1,10 +1,10 @@ -//// [declarationEmitDestructuringArrayPattern6.ts] +//// [declarationEmitDestructuring2.ts] function f({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]} = { x: 10, y: [2, 4, 6, 8] }) { } function g([a, b, c, d] = [1, 2, 3, 4]) { } function h([a, [b], [[c]], {x = 10, y: [a, b, c], z: {a1, b1}}]){ } function h1([a, [b], [[c]], {x = 10, y = [1, 2, 3], z: {a1, b1}}]){ } -//// [declarationEmitDestructuringArrayPattern6.js] +//// [declarationEmitDestructuring2.js] function f(_a) { var _b = _a === void 0 ? { x: 10, @@ -41,7 +41,7 @@ function h1(_a) { } -//// [declarationEmitDestructuringArrayPattern6.d.ts] +//// [declarationEmitDestructuring2.d.ts] declare function f({x, y: [a, b, c, d]}?: { x: number; y: [number, number, number, number]; diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.types b/tests/baselines/reference/declarationEmitDestructuring2.types similarity index 91% rename from tests/baselines/reference/declarationEmitDestructuringArrayPattern6.types rename to tests/baselines/reference/declarationEmitDestructuring2.types index 6112094cbe550..3368d4e72fd1f 100644 --- a/tests/baselines/reference/declarationEmitDestructuringArrayPattern6.types +++ b/tests/baselines/reference/declarationEmitDestructuring2.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts === +=== tests/cases/compiler/declarationEmitDestructuring2.ts === function f({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]} = { x: 10, y: [2, 4, 6, 8] }) { } >f : ({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]}?: { x: number; y: [number, number, number, number]; }) => void >x : number diff --git a/tests/baselines/reference/declarationEmitDestructuring3.js b/tests/baselines/reference/declarationEmitDestructuring3.js new file mode 100644 index 0000000000000..66f6b071a872a --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuring3.js @@ -0,0 +1,22 @@ +//// [declarationEmitDestructuring3.ts] +function bar([x, z, ...w]) { } +function foo([x, ...y] = [1, "string", true]) { } + + + +//// [declarationEmitDestructuring3.js] +function bar(_a) { + var x = _a[0], z = _a[1], w = _a.slice(2); +} +function foo(_a) { + var _b = _a === void 0 ? [ + 1, + "string", + true + ] : _a, x = _b[0], y = _b.slice(1); +} + + +//// [declarationEmitDestructuring3.d.ts] +declare function bar([x, z, ...w]: any[]): void; +declare function foo([x, ...y]?: (string | number | boolean)[]): void; diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.types b/tests/baselines/reference/declarationEmitDestructuring3.types similarity index 78% rename from tests/baselines/reference/declarationEmitDestructuringArrayPattern7.types rename to tests/baselines/reference/declarationEmitDestructuring3.types index 8dc51bf5c781e..57764f53eee00 100644 --- a/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.types +++ b/tests/baselines/reference/declarationEmitDestructuring3.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts === +=== tests/cases/compiler/declarationEmitDestructuring3.ts === function bar([x, z, ...w]) { } >bar : ([x, z, ...w]: any[]) => void >x : any @@ -11,3 +11,4 @@ function foo([x, ...y] = [1, "string", true]) { } >y : (string | number | boolean)[] >[1, "string", true] : (string | number | boolean)[] + diff --git a/tests/baselines/reference/declarationEmitDestructuring4.js b/tests/baselines/reference/declarationEmitDestructuring4.js new file mode 100644 index 0000000000000..5b43fbfc85e07 --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuring4.js @@ -0,0 +1,67 @@ +//// [declarationEmitDestructuring4.ts] +function baz([]) { } +function baz1([] = [1,2,3]) { } +function baz2([[]] = [[1,2,3]]) { } +function baz3({}) { } +function baz4({} = { x: 10 }) { } +function baz5({} = { x: 10, y: { a: 2 }, z: [1,2] }) { } + + + +//// [declarationEmitDestructuring4.js] +function baz(_a) { + var ; +} +function baz1(_a) { + var _b = _a === void 0 ? [ + 1, + 2, + 3 + ] : _a; +} +function baz2(_a) { + var _b = (_a === void 0 ? [ + [ + 1, + 2, + 3 + ] + ] : _a)[0]; +} +function baz3(_a) { + var ; +} +function baz4(_a) { + var _b = _a === void 0 ? { + x: 10 + } : _a; +} +function baz5(_a) { + var _b = _a === void 0 ? { + x: 10, + y: { + a: 2 + }, + z: [ + 1, + 2 + ] + } : _a; +} + + +//// [declarationEmitDestructuring4.d.ts] +declare function baz([]: any[]): void; +declare function baz1([]?: number[]): void; +declare function baz2([[]]?: [number[]]): void; +declare function baz3({}: {}): void; +declare function baz4({}?: { + x: number; +}): void; +declare function baz5({}?: { + x: number; + y: { + a: number; + }; + z: number[]; +}): void; diff --git a/tests/baselines/reference/declarationEmitDestructuring4.types b/tests/baselines/reference/declarationEmitDestructuring4.types new file mode 100644 index 0000000000000..6602fbdcb77d2 --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuring4.types @@ -0,0 +1,32 @@ +=== tests/cases/compiler/declarationEmitDestructuring4.ts === +function baz([]) { } +>baz : ([]: any[]) => void + +function baz1([] = [1,2,3]) { } +>baz1 : ([]?: number[]) => void +>[1,2,3] : number[] + +function baz2([[]] = [[1,2,3]]) { } +>baz2 : ([[]]?: [number[]]) => void +>[[1,2,3]] : [number[]] +>[1,2,3] : number[] + +function baz3({}) { } +>baz3 : ({}: {}) => void + +function baz4({} = { x: 10 }) { } +>baz4 : ({}?: { x: number; }) => void +>{ x: 10 } : { x: number; } +>x : number + +function baz5({} = { x: 10, y: { a: 2 }, z: [1,2] }) { } +>baz5 : ({}?: { x: number; y: { a: number; }; z: number[]; }) => void +>{ x: 10, y: { a: 2 }, z: [1,2] } : { x: number; y: { a: number; }; z: number[]; } +>x : number +>y : { a: number; } +>{ a: 2 } : { a: number; } +>a : number +>z : number[] +>[1,2] : number[] + + diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js b/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js deleted file mode 100644 index 00e65b816b05e..0000000000000 --- a/tests/baselines/reference/declarationEmitDestructuringArrayPattern7.js +++ /dev/null @@ -1,20 +0,0 @@ -//// [declarationEmitDestructuringArrayPattern7.ts] -function bar([x, z, ...w]) { } -function foo([x, ...y] = [1, "string", true]) { } - -//// [declarationEmitDestructuringArrayPattern7.js] -function bar(_a) { - var x = _a[0], z = _a[1], w = _a.slice(2); -} -function foo(_a) { - var _b = _a === void 0 ? [ - 1, - "string", - true - ] : _a, x = _b[0], y = _b.slice(1); -} - - -//// [declarationEmitDestructuringArrayPattern7.d.ts] -declare function bar([x, z, w]: any[]): void; -declare function foo([x, y]?: (string | number | boolean)[]): void; diff --git a/tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts b/tests/cases/compiler/declarationEmitDestructuring1.ts similarity index 100% rename from tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts rename to tests/cases/compiler/declarationEmitDestructuring1.ts diff --git a/tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts b/tests/cases/compiler/declarationEmitDestructuring2.ts similarity index 100% rename from tests/cases/compiler/declarationEmitDestructuringArrayPattern6.ts rename to tests/cases/compiler/declarationEmitDestructuring2.ts diff --git a/tests/cases/compiler/declarationEmitDestructuring3.ts b/tests/cases/compiler/declarationEmitDestructuring3.ts new file mode 100644 index 0000000000000..78aeddb0e11bf --- /dev/null +++ b/tests/cases/compiler/declarationEmitDestructuring3.ts @@ -0,0 +1,4 @@ +// @declaration: true +function bar([x, z, ...w]) { } +function foo([x, ...y] = [1, "string", true]) { } + diff --git a/tests/cases/compiler/declarationEmitDestructuring4.ts b/tests/cases/compiler/declarationEmitDestructuring4.ts new file mode 100644 index 0000000000000..b961be2b39c43 --- /dev/null +++ b/tests/cases/compiler/declarationEmitDestructuring4.ts @@ -0,0 +1,12 @@ +// @declaration: true +// For an array binding pattern with empty elements, +// we will not make any modification and will emit +// the similar binding pattern users' have written +function baz([]) { } +function baz1([] = [1,2,3]) { } +function baz2([[]] = [[1,2,3]]) { } + +function baz3({}) { } +function baz4({} = { x: 10 }) { } +function baz5({} = { x: 10, y: { a: 2 }, z: [1,2] }) { } + diff --git a/tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts b/tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts deleted file mode 100644 index 321c6fd420275..0000000000000 --- a/tests/cases/compiler/declarationEmitDestructuringArrayPattern7.ts +++ /dev/null @@ -1,3 +0,0 @@ -// @declaration: true -function bar([x, z, ...w]) { } -function foo([x, ...y] = [1, "string", true]) { } \ No newline at end of file From 6695981583fa5f4f2ad184d64fae630808266c50 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 23 Mar 2015 17:59:02 -0700 Subject: [PATCH 05/10] Address code review --- src/compiler/declarationEmitter.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 93e0fa3129713..8431756c917d0 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -1322,7 +1322,7 @@ module ts { } : undefined; } - function getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccesibilityResult: SymbolAccessiblityResult) { + function getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccesibilityResult: SymbolAccessiblityResult): DiagnosticMessage { switch (node.parent.kind) { case SyntaxKind.Constructor: return symbolAccesibilityResult.errorModuleName ? @@ -1422,15 +1422,19 @@ module ts { // Example: // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; - // original with rest: function foo([a ...c]) {} - // emit : declare function foo([a, c]): void; + // original with rest: function foo([a, ...c]) {} + // emit : declare function foo([a, ...c]): void; emitBindingPattern(bindingElement.name); } else { + Debug.assert(bindingElement.name.kind === SyntaxKind.Identifier); // If the node is just an identifier, we will simply emit the text associated with the node's name // Example: // original: function foo({y = 10, x}) {} // emit : declare function foo({y, x}: {number, any}): void; + if (bindingElement.dotDotDotToken) { + write("..."); + } writeTextOfNode(currentSourceFile, bindingElement.name); } } From 1c2eae6b72c8cc9646fda407bbaa6344bcf4e997 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 24 Mar 2015 10:11:29 -0700 Subject: [PATCH 06/10] Update test cases --- .../declarationEmitDestructuring4.js | 27 +++++-------------- .../declarationEmitDestructuring4.types | 13 +++------ .../compiler/declarationEmitDestructuring4.ts | 1 - 3 files changed, 10 insertions(+), 31 deletions(-) diff --git a/tests/baselines/reference/declarationEmitDestructuring4.js b/tests/baselines/reference/declarationEmitDestructuring4.js index 5b43fbfc85e07..81d1707b8e625 100644 --- a/tests/baselines/reference/declarationEmitDestructuring4.js +++ b/tests/baselines/reference/declarationEmitDestructuring4.js @@ -1,14 +1,20 @@ //// [declarationEmitDestructuring4.ts] +// For an array binding pattern with empty elements, +// we will not make any modification and will emit +// the similar binding pattern users' have written function baz([]) { } function baz1([] = [1,2,3]) { } function baz2([[]] = [[1,2,3]]) { } + function baz3({}) { } function baz4({} = { x: 10 }) { } -function baz5({} = { x: 10, y: { a: 2 }, z: [1,2] }) { } //// [declarationEmitDestructuring4.js] +// For an array binding pattern with empty elements, +// we will not make any modification and will emit +// the similar binding pattern users' have written function baz(_a) { var ; } @@ -36,18 +42,6 @@ function baz4(_a) { x: 10 } : _a; } -function baz5(_a) { - var _b = _a === void 0 ? { - x: 10, - y: { - a: 2 - }, - z: [ - 1, - 2 - ] - } : _a; -} //// [declarationEmitDestructuring4.d.ts] @@ -58,10 +52,3 @@ declare function baz3({}: {}): void; declare function baz4({}?: { x: number; }): void; -declare function baz5({}?: { - x: number; - y: { - a: number; - }; - z: number[]; -}): void; diff --git a/tests/baselines/reference/declarationEmitDestructuring4.types b/tests/baselines/reference/declarationEmitDestructuring4.types index 6602fbdcb77d2..6a90eda1702b9 100644 --- a/tests/baselines/reference/declarationEmitDestructuring4.types +++ b/tests/baselines/reference/declarationEmitDestructuring4.types @@ -1,4 +1,7 @@ === tests/cases/compiler/declarationEmitDestructuring4.ts === +// For an array binding pattern with empty elements, +// we will not make any modification and will emit +// the similar binding pattern users' have written function baz([]) { } >baz : ([]: any[]) => void @@ -19,14 +22,4 @@ function baz4({} = { x: 10 }) { } >{ x: 10 } : { x: number; } >x : number -function baz5({} = { x: 10, y: { a: 2 }, z: [1,2] }) { } ->baz5 : ({}?: { x: number; y: { a: number; }; z: number[]; }) => void ->{ x: 10, y: { a: 2 }, z: [1,2] } : { x: number; y: { a: number; }; z: number[]; } ->x : number ->y : { a: number; } ->{ a: 2 } : { a: number; } ->a : number ->z : number[] ->[1,2] : number[] - diff --git a/tests/cases/compiler/declarationEmitDestructuring4.ts b/tests/cases/compiler/declarationEmitDestructuring4.ts index b961be2b39c43..bd1a57af12e61 100644 --- a/tests/cases/compiler/declarationEmitDestructuring4.ts +++ b/tests/cases/compiler/declarationEmitDestructuring4.ts @@ -8,5 +8,4 @@ function baz2([[]] = [[1,2,3]]) { } function baz3({}) { } function baz4({} = { x: 10 }) { } -function baz5({} = { x: 10, y: { a: 2 }, z: [1,2] }) { } From 1f055b95aa704e3881d9ed8a2e8dd4f915877f71 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 30 Mar 2015 13:25:46 -0700 Subject: [PATCH 07/10] Emit ommittedExpression in binding pattern --- src/compiler/declarationEmitter.ts | 73 +++++++++++-------- .../declarationEmitDestructuring5.js | 37 ++++++++++ .../declarationEmitDestructuring5.types | 22 ++++++ .../compiler/declarationEmitDestructuring5.ts | 6 ++ 4 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitDestructuring5.js create mode 100644 tests/baselines/reference/declarationEmitDestructuring5.types create mode 100644 tests/cases/compiler/declarationEmitDestructuring5.ts diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 8431756c917d0..ee8b7f2ac392b 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -1387,7 +1387,11 @@ module ts { } else if (bindingPattern.kind === SyntaxKind.ArrayBindingPattern) { write("["); - emitCommaList(bindingPattern.elements, emitBindingElement); + let elements = bindingPattern.elements; + emitCommaList(elements, emitBindingElement); + if (elements && elements.hasTrailingComma) { + write(", "); + } write("]"); } } @@ -1402,40 +1406,51 @@ module ts { } : undefined; } - if (bindingElement.propertyName) { - // bindingElement has propertyName property in the following case: - // { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y" - // We have to explicitly emit the propertyName before descending into its binding elements. + if (bindingElement.kind === SyntaxKind.OmittedExpression) { + // If bindingElement is an omittedExpression (i.e. containing elision), + // we will emit blank space (although this may differ from users' original code, + // it allows emitSeparatedList to write separator appropriately) // Example: - // original: function foo({y: [a,b,c]}) {} - // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; - writeTextOfNode(currentSourceFile, bindingElement.propertyName); - write(": "); - - // If bindingElement has propertyName property, then its name must be another bindingPattern of SyntaxKind.ObjectBindingPattern - emitBindingPattern(bindingElement.name); + // original: function foo([, x, ,]) {} + // emit : function foo([ , x, , ]) {} + write(" "); } - else if (bindingElement.name) { - if (isBindingPattern(bindingElement.name)) { - // If it is a nested binding pattern, we will recursively descend into each element and emit each one separately. - // In the case of rest element, we will omit rest element. + else if (bindingElement.kind === SyntaxKind.BindingElement) { + if (bindingElement.propertyName) { + // bindingElement has propertyName property in the following case: + // { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y" + // We have to explicitly emit the propertyName before descending into its binding elements. // Example: - // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} - // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; - // original with rest: function foo([a, ...c]) {} - // emit : declare function foo([a, ...c]): void; + // original: function foo({y: [a,b,c]}) {} + // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; + writeTextOfNode(currentSourceFile, bindingElement.propertyName); + write(": "); + + // If bindingElement has propertyName property, then its name must be another bindingPattern of SyntaxKind.ObjectBindingPattern emitBindingPattern(bindingElement.name); } - else { - Debug.assert(bindingElement.name.kind === SyntaxKind.Identifier); - // If the node is just an identifier, we will simply emit the text associated with the node's name - // Example: - // original: function foo({y = 10, x}) {} - // emit : declare function foo({y, x}: {number, any}): void; - if (bindingElement.dotDotDotToken) { - write("..."); + else if (bindingElement.name) { + if (isBindingPattern(bindingElement.name)) { + // If it is a nested binding pattern, we will recursively descend into each element and emit each one separately. + // In the case of rest element, we will omit rest element. + // Example: + // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} + // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; + // original with rest: function foo([a, ...c]) {} + // emit : declare function foo([a, ...c]): void; + emitBindingPattern(bindingElement.name); + } + else { + Debug.assert(bindingElement.name.kind === SyntaxKind.Identifier); + // If the node is just an identifier, we will simply emit the text associated with the node's name + // Example: + // original: function foo({y = 10, x}) {} + // emit : declare function foo({y, x}: {number, any}): void; + if (bindingElement.dotDotDotToken) { + write("..."); + } + writeTextOfNode(currentSourceFile, bindingElement.name); } - writeTextOfNode(currentSourceFile, bindingElement.name); } } } diff --git a/tests/baselines/reference/declarationEmitDestructuring5.js b/tests/baselines/reference/declarationEmitDestructuring5.js new file mode 100644 index 0000000000000..f0602206a147a --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuring5.js @@ -0,0 +1,37 @@ +//// [declarationEmitDestructuring5.ts] +function baz([, z, , ]) { } +function foo([, b, ]: [any, any]): void { } +function bar([z, , , ]) { } +function bar1([z, , , ] = [1, 3, 4, 6, 7]) { } +function bar2([,,z, , , ]) { } + +//// [declarationEmitDestructuring5.js] +function baz(_a) { + var z = _a[1]; +} +function foo(_a) { + var b = _a[1]; +} +function bar(_a) { + var z = _a[0]; +} +function bar1(_a) { + var _b = _a === void 0 ? [ + 1, + 3, + 4, + 6, + 7 + ] : _a, z = _b[0]; +} +function bar2(_a) { + var z = _a[2]; +} + + +//// [declarationEmitDestructuring5.d.ts] +declare function baz([ , z, , ]: [any, any, any]): void; +declare function foo([ , b, ]: [any, any]): void; +declare function bar([z, , , ]: [any, any, any]): void; +declare function bar1([z, , , ]?: [number, number, number, number, number]): void; +declare function bar2([ , , z, , , ]: [any, any, any, any, any]): void; diff --git a/tests/baselines/reference/declarationEmitDestructuring5.types b/tests/baselines/reference/declarationEmitDestructuring5.types new file mode 100644 index 0000000000000..375440bea0be3 --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuring5.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/declarationEmitDestructuring5.ts === +function baz([, z, , ]) { } +>baz : ([, z, , ]: [any, any, any]) => void +>z : any + +function foo([, b, ]: [any, any]): void { } +>foo : ([, b, ]: [any, any]) => void +>b : any + +function bar([z, , , ]) { } +>bar : ([z, , , ]: [any, any, any]) => void +>z : any + +function bar1([z, , , ] = [1, 3, 4, 6, 7]) { } +>bar1 : ([z, , , ]?: [number, number, number, number, number]) => void +>z : number +>[1, 3, 4, 6, 7] : [number, number, number, number, number] + +function bar2([,,z, , , ]) { } +>bar2 : ([,,z, , , ]: [any, any, any, any, any]) => void +>z : any + diff --git a/tests/cases/compiler/declarationEmitDestructuring5.ts b/tests/cases/compiler/declarationEmitDestructuring5.ts new file mode 100644 index 0000000000000..a02a21b5a50f4 --- /dev/null +++ b/tests/cases/compiler/declarationEmitDestructuring5.ts @@ -0,0 +1,6 @@ +// @declaration: true +function baz([, z, , ]) { } +function foo([, b, ]: [any, any]): void { } +function bar([z, , , ]) { } +function bar1([z, , , ] = [1, 3, 4, 6, 7]) { } +function bar2([,,z, , , ]) { } \ No newline at end of file From 8320bca4d002e9081282fb689676470c3b34be82 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 30 Mar 2015 14:41:07 -0700 Subject: [PATCH 08/10] Fix emit trailing comma for array binding pattern for varaible declaration --- src/compiler/declarationEmitter.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index ee8b7f2ac392b..3ba1cb0a1a382 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -993,7 +993,18 @@ module ts { } function emitBindingPattern(bindingPattern: BindingPattern) { - emitCommaList(bindingPattern.elements, emitBindingElement); + // Only select non-omitted expression from the bindingPattern's elements. + // We have to do this to avoid emitting trailing commas. + // For example: + // original: var [, c,,] = [ 2,3,4] + // emitted: declare var c: number; // instead of declare var c:number, ; + let elements: Node[] = []; + for (let element of bindingPattern.elements) { + if (element.kind !== SyntaxKind.OmittedExpression){ + elements.push(element); + } + } + emitCommaList(elements, emitBindingElement); } function emitBindingElement(bindingElement: BindingElement) { From 129b8ad8b041f34e5e06c09f0b94793d987133a0 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 30 Mar 2015 16:28:10 -0700 Subject: [PATCH 09/10] Update baselines from merging with master --- .../declarationEmitDestructuring2.js | 28 ++----------------- .../declarationEmitDestructuring3.js | 6 +--- .../declarationEmitDestructuring4.js | 18 ++---------- .../declarationEmitDestructuring5.js | 8 +----- 4 files changed, 8 insertions(+), 52 deletions(-) diff --git a/tests/baselines/reference/declarationEmitDestructuring2.js b/tests/baselines/reference/declarationEmitDestructuring2.js index f6d3d17abe1c4..eeabbf1ff372f 100644 --- a/tests/baselines/reference/declarationEmitDestructuring2.js +++ b/tests/baselines/reference/declarationEmitDestructuring2.js @@ -6,38 +6,16 @@ function h1([a, [b], [[c]], {x = 10, y = [1, 2, 3], z: {a1, b1}}]){ } //// [declarationEmitDestructuring2.js] function f(_a) { - var _b = _a === void 0 ? { - x: 10, - y: [ - 2, - 4, - 6, - 8 - ] - } : _a, _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, _e = _d === void 0 ? [ - 1, - 2, - 3, - 4 - ] : _d, a = _e[0], b = _e[1], c = _e[2], d = _e[3]; + var _b = _a === void 0 ? { x: 10, y: [2, 4, 6, 8] } : _a, _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, _e = _d === void 0 ? [1, 2, 3, 4] : _d, a = _e[0], b = _e[1], c = _e[2], d = _e[3]; } function g(_a) { - var _b = _a === void 0 ? [ - 1, - 2, - 3, - 4 - ] : _a, a = _b[0], b = _b[1], c = _b[2], d = _b[3]; + var _b = _a === void 0 ? [1, 2, 3, 4] : _a, a = _b[0], b = _b[1], c = _b[2], d = _b[3]; } function h(_a) { var a = _a[0], b = _a[1][0], c = _a[2][0][0], _b = _a[3], _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, a = _d[0], b = _d[1], c = _d[2], _e = _b.z, a1 = _e.a1, b1 = _e.b1; } function h1(_a) { - var a = _a[0], b = _a[1][0], c = _a[2][0][0], _b = _a[3], _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, y = _d === void 0 ? [ - 1, - 2, - 3 - ] : _d, _e = _b.z, a1 = _e.a1, b1 = _e.b1; + var a = _a[0], b = _a[1][0], c = _a[2][0][0], _b = _a[3], _c = _b.x, x = _c === void 0 ? 10 : _c, _d = _b.y, y = _d === void 0 ? [1, 2, 3] : _d, _e = _b.z, a1 = _e.a1, b1 = _e.b1; } diff --git a/tests/baselines/reference/declarationEmitDestructuring3.js b/tests/baselines/reference/declarationEmitDestructuring3.js index 66f6b071a872a..b21c63203c894 100644 --- a/tests/baselines/reference/declarationEmitDestructuring3.js +++ b/tests/baselines/reference/declarationEmitDestructuring3.js @@ -9,11 +9,7 @@ function bar(_a) { var x = _a[0], z = _a[1], w = _a.slice(2); } function foo(_a) { - var _b = _a === void 0 ? [ - 1, - "string", - true - ] : _a, x = _b[0], y = _b.slice(1); + var _b = _a === void 0 ? [1, "string", true] : _a, x = _b[0], y = _b.slice(1); } diff --git a/tests/baselines/reference/declarationEmitDestructuring4.js b/tests/baselines/reference/declarationEmitDestructuring4.js index 81d1707b8e625..f5d9df0917589 100644 --- a/tests/baselines/reference/declarationEmitDestructuring4.js +++ b/tests/baselines/reference/declarationEmitDestructuring4.js @@ -19,28 +19,16 @@ function baz(_a) { var ; } function baz1(_a) { - var _b = _a === void 0 ? [ - 1, - 2, - 3 - ] : _a; + var _b = _a === void 0 ? [1, 2, 3] : _a; } function baz2(_a) { - var _b = (_a === void 0 ? [ - [ - 1, - 2, - 3 - ] - ] : _a)[0]; + var _b = (_a === void 0 ? [[1, 2, 3]] : _a)[0]; } function baz3(_a) { var ; } function baz4(_a) { - var _b = _a === void 0 ? { - x: 10 - } : _a; + var _b = _a === void 0 ? { x: 10 } : _a; } diff --git a/tests/baselines/reference/declarationEmitDestructuring5.js b/tests/baselines/reference/declarationEmitDestructuring5.js index f0602206a147a..32c9b5667d180 100644 --- a/tests/baselines/reference/declarationEmitDestructuring5.js +++ b/tests/baselines/reference/declarationEmitDestructuring5.js @@ -16,13 +16,7 @@ function bar(_a) { var z = _a[0]; } function bar1(_a) { - var _b = _a === void 0 ? [ - 1, - 3, - 4, - 6, - 7 - ] : _a, z = _b[0]; + var _b = _a === void 0 ? [1, 3, 4, 6, 7] : _a, z = _b[0]; } function bar2(_a) { var z = _a[2]; From c1d9cfe6408fb5954133ac38bcb867298f2a74bb Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 30 Mar 2015 16:45:20 -0700 Subject: [PATCH 10/10] Add test for emit destructuring invariable declaration with omitted expression --- .../declarationEmitDestructuringArrayPattern5.js | 15 +++++++++++++++ ...eclarationEmitDestructuringArrayPattern5.types | 14 ++++++++++++++ .../declarationEmitDestructuringArrayPattern5.ts | 4 ++++ 3 files changed, 33 insertions(+) create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js create mode 100644 tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types create mode 100644 tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js new file mode 100644 index 0000000000000..820864cc25af0 --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.js @@ -0,0 +1,15 @@ +//// [declarationEmitDestructuringArrayPattern5.ts] +var [, , z] = [1, 2, 4]; +var [, a, , ] = [3, 4, 5]; +var [, , [, b, ]] = [3,5,[0, 1]]; + +//// [declarationEmitDestructuringArrayPattern5.js] +var _a = [1, 2, 4], z = _a[2]; +var _b = [3, 4, 5], a = _b[1]; +var _c = [3, 5, [0, 1]], _d = _c[2], b = _d[1]; + + +//// [declarationEmitDestructuringArrayPattern5.d.ts] +declare var z: number; +declare var a: number; +declare var b: number; diff --git a/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types new file mode 100644 index 0000000000000..6352917682ebd --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuringArrayPattern5.types @@ -0,0 +1,14 @@ +=== tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts === +var [, , z] = [1, 2, 4]; +>z : number +>[1, 2, 4] : [number, number, number] + +var [, a, , ] = [3, 4, 5]; +>a : number +>[3, 4, 5] : [number, number, number] + +var [, , [, b, ]] = [3,5,[0, 1]]; +>b : number +>[3,5,[0, 1]] : [number, number, [number, number]] +>[0, 1] : [number, number] + diff --git a/tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts b/tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts new file mode 100644 index 0000000000000..eeb5dd4bdca0c --- /dev/null +++ b/tests/cases/compiler/declarationEmitDestructuringArrayPattern5.ts @@ -0,0 +1,4 @@ +// @declaration: true +var [, , z] = [1, 2, 4]; +var [, a, , ] = [3, 4, 5]; +var [, , [, b, ]] = [3,5,[0, 1]]; \ No newline at end of file