From 5979dacf4f0278278b42749aa2ae7f146326bdac Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 23 Mar 2015 11:30:51 -0700 Subject: [PATCH] Correctly emit bidning pattern with initializer and rest --- src/compiler/declarationEmitter.ts | 99 +++++++++++++++---- ...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, 237 insertions(+), 25 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 6509ae6e2c..93e0fa3129 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 0000000000..803ea9fe01 --- /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 0000000000..6112094cbe --- /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 0000000000..00e65b816b --- /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 0000000000..8dc51bf5c7 --- /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 d44c9d4236..a3cdee3122 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 841508c331..a98620c852 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 9ffd25a7fa..5c7f4d2cec 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 0000000000..34441cd6c7 --- /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 0000000000..321c6fd420 --- /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