Skip to content

Allow destructuring in ambient #2429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12173,9 +12173,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;
Expand Down
128 changes: 108 additions & 20 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1291,7 +1302,10 @@ module ts {
write("...");
}
if (isBindingPattern(node.name)) {
write("_" + indexOf((<FunctionLikeDeclaration>node.parent).parameters, node));
// 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(<BindingPattern>node.name);
}
else {
writeTextOfNode(currentSourceFile, node.name);
Expand All @@ -1311,72 +1325,146 @@ 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): DiagnosticMessage {
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 :
Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_private_name_1;
}
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("[");
let elements = bindingPattern.elements;
emitCommaList(elements, emitBindingElement);
if (elements && elements.hasTrailingComma) {
write(", ");
}
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.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([, x, ,]) {}
// emit : function foo([ , x, , ]) {}
write(" ");
}
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({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(<BindingPattern>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(<BindingPattern>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);
}
}
}
}
}

function emitNode(node: Node) {
Expand Down
37 changes: 37 additions & 0 deletions tests/baselines/reference/declarationEmitDestructuring1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [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 { }


//// [declarationEmitDestructuring1.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;
}


//// [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}: {
a1: number;
b1: boolean;
c1: string;
}): void;
declare function baz({a2, b2: {b1, c1}}: {
a2: number;
b2: {
b1: boolean;
c1: string;
};
}): void;
33 changes: 33 additions & 0 deletions tests/baselines/reference/declarationEmitDestructuring1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/declarationEmitDestructuring1.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

43 changes: 43 additions & 0 deletions tests/baselines/reference/declarationEmitDestructuring2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// [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}}]){ }

//// [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];
}
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;
}


//// [declarationEmitDestructuring2.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;
49 changes: 49 additions & 0 deletions tests/baselines/reference/declarationEmitDestructuring2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== 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
>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

18 changes: 18 additions & 0 deletions tests/baselines/reference/declarationEmitDestructuring3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [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;
Loading