Skip to content

Fix emit destructuring in declaration file #2401

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

Closed
wants to merge 2 commits into from
Closed
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
55 changes: 43 additions & 12 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,22 +1039,53 @@ module ts {
}

function emitVariableDeclaration(node: VariableDeclaration) {
function emitBindingElement(bindingElement: BindingElement) {
if (bindingElement.name.kind === SyntaxKind.ObjectBindingPattern || bindingElement.name.kind === SyntaxKind.ArrayBindingPattern) {
let elements = (<BindingPattern>bindingElement.name).elements;
for (let i = 0, n = elements.length; i < n; i++) {
if (i) {
write(", ");
}
let element = elements[i];
emitBindingElement(element);
}
}
else {
writeTextOfNode(currentSourceFile, bindingElement.name);
// By given undefined for type, writeTypeOfDeclaration will call resolver to get type of the symbol
writeTypeOfDeclaration(bindingElement, /*type*/ undefined, getVariableDeclarationTypeVisibilityError);
}
}

// If we are emitting property it isn't moduleElement and hence we already know it needs to be emitted
// so there is no check needed to see if declaration is visible
if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) {
// If this node is a computed name, it can only be a symbol, because we've already skipped
// it if it's not a well known symbol. In that case, the text of the name will be exactly
// what we want, namely the name expression enclosed in brackets.
writeTextOfNode(currentSourceFile, node.name);
// If optional property emit ?
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && hasQuestionToken(node)) {
write("?");
}
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && node.parent.kind === SyntaxKind.TypeLiteral) {
emitTypeOfVariableDeclarationFromTypeLiteral(node);
// If the node.name is an object-binding-pattern or array-binding-pattern,
// decent into each element and emit each element as separate variable declarations
// For example:
// TypeScript:
// var {a,b} = {a:"string", b:10}
// .d.ts:
// declare var a: string, b: number;
if (node.name.kind === SyntaxKind.ObjectBindingPattern || node.name.kind === SyntaxKind.ArrayBindingPattern) {
emitBindingElement(<BindingElement>node);
}
else if (!(node.flags & NodeFlags.Private)) {
writeTypeOfDeclaration(node, node.type, getVariableDeclarationTypeVisibilityError);
else {
// If this node is a computed name, it can only be a symbol, because we've already skipped
// it if it's not a well known symbol. In that case, the text of the name will be exactly
// what we want, namely the name expression enclosed in brackets.
writeTextOfNode(currentSourceFile, node.name);

// If optional property emit ?
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && hasQuestionToken(node)) {
write("?");
}
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && node.parent.kind === SyntaxKind.TypeLiteral) {
emitTypeOfVariableDeclarationFromTypeLiteral(node);
}
else if (!(node.flags & NodeFlags.Private)) {
writeTypeOfDeclaration(node, node.type, getVariableDeclarationTypeVisibilityError);
}
}
}

Expand Down
47 changes: 47 additions & 0 deletions tests/baselines/reference/emitDestructuringInDeclarationFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [emitDestructuringInDeclarationFile.ts]
var {a, b} = { a:10, b:"10"};
var {c, d}: {c:string, d:string} = { c:"true", d:"false" };
let {k, j} = { k:true, j:{a:10}};

var a1 = ["string", 10];
var [x] = a1;
var [x, y] = a1;
var [x, y, z] = a1;
let [x1] = a1;

//// [emitDestructuringInDeclarationFile.js]
var _a = {
a: 10,
b: "10"
}, a = _a.a, b = _a.b;
var _b = {
c: "true",
d: "false"
}, c = _b.c, d = _b.d;
var _c = {
k: true,
j: {
a: 10
}
}, k = _c.k, j = _c.j;
var a1 = [
"string",
10
];
var x = a1[0];
var x = a1[0], y = a1[1];
var x = a1[0], y = a1[1], z = a1[2];
var x1 = a1[0];


//// [emitDestructuringInDeclarationFile.d.ts]
declare var a: number, b: string;
declare var c: string, d: string;
declare let k: boolean, j: {
a: number;
};
declare var a1: (string | number)[];
declare var x: string | number;
declare var x: string | number, y: string | number;
declare var x: string | number, y: string | number, z: string | number;
declare let x1: string | number;
49 changes: 49 additions & 0 deletions tests/baselines/reference/emitDestructuringInDeclarationFile.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile.ts ===
var {a, b} = { a:10, b:"10"};
>a : number
>b : string
>{ a:10, b:"10"} : { a: number; b: string; }
>a : number
>b : string

var {c, d}: {c:string, d:string} = { c:"true", d:"false" };
>c : string
>d : string
>c : string
>d : string
>{ c:"true", d:"false" } : { c: string; d: string; }
>c : string
>d : string

let {k, j} = { k:true, j:{a:10}};
>k : boolean
>j : { a: number; }
>{ k:true, j:{a:10}} : { k: boolean; j: { a: number; }; }
>k : boolean
>j : { a: number; }
>{a:10} : { a: number; }
>a : number

var a1 = ["string", 10];
>a1 : (string | number)[]
>["string", 10] : (string | number)[]

var [x] = a1;
>x : string | number
>a1 : (string | number)[]

var [x, y] = a1;
>x : string | number
>y : string | number
>a1 : (string | number)[]

var [x, y, z] = a1;
>x : string | number
>y : string | number
>z : string | number
>a1 : (string | number)[]

let [x1] = a1;
>x1 : string | number
>a1 : (string | number)[]

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile1.ts(6,16): error TS1182: A destructuring declaration must have an initializer.


==== tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile1.ts (1 errors) ====
module M {
export class c { }
export var {b} = {
b: new c()
};
export var {a}: { a: c };
~~~
!!! error TS1182: A destructuring declaration must have an initializer.
}

33 changes: 33 additions & 0 deletions tests/baselines/reference/emitDestructuringInDeclarationFile1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [emitDestructuringInDeclarationFile1.ts]
module M {
export class c { }
export var {b} = {
b: new c()
};
export var {a}: { a: c };
}


//// [emitDestructuringInDeclarationFile1.js]
var M;
(function (M) {
var c = (function () {
function c() {
}
return c;
})();
M.c = c;
M.b = ({
b: new c()
}).b;
M.a = (void 0).a;
})(M || (M = {}));


//// [emitDestructuringInDeclarationFile1.d.ts]
declare module M {
class c {
}
var b: c;
var a: c;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile2.ts(3,16): error TS4025: Exported variable '{b}' has or is using private name 'c'.
tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile2.ts(6,16): error TS1182: A destructuring declaration must have an initializer.
tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile2.ts(6,16): error TS4025: Exported variable '{a}' has or is using private name 'c'.


==== tests/cases/conformance/es6/destructuring/emitDestructuringInDeclarationFile2.ts (3 errors) ====
module M {
class c { }
export var {b} = {
~~~
!!! error TS4025: Exported variable '{b}' has or is using private name 'c'.
b: new c()
};
export var {a}: { a: c };
~~~
!!! error TS1182: A destructuring declaration must have an initializer.
~~~
!!! error TS4025: Exported variable '{a}' has or is using private name 'c'.
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/emitDestructuringInDeclarationFile2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [emitDestructuringInDeclarationFile2.ts]
module M {
class c { }
export var {b} = {
b: new c()
};
export var {a}: { a: c };
}


//// [emitDestructuringInDeclarationFile2.js]
var M;
(function (M) {
var c = (function () {
function c() {
}
return c;
})();
M.b = ({
b: new c()
}).b;
M.a = (void 0).a;
})(M || (M = {}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [emitDestructuringWithInitializerInDeclarationFile.ts]
class C { }
class D extends C { }
var {s = '10', c} = { s: 'bar', c: true };
var [s1 = '10', c1] = ['hello', 'word'];
var {s2 = { prop1: new C(), prop2: new C() }, c2} = { s2: { prop1: new D(), prop2: new D() }, c2: false };
var [[s3 = [], c3]] = [[[10,20], "blah"]];

//// [emitDestructuringWithInitializerInDeclarationFile.js]
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var C = (function () {
function C() {
}
return C;
})();
var D = (function (_super) {
__extends(D, _super);
function D() {
_super.apply(this, arguments);
}
return D;
})(C);
var _a = {
s: 'bar',
c: true
}, _b = _a.s, s = _b === void 0 ? '10' : _b, c = _a.c;
var _c = [
'hello',
'word'
], _d = _c[0], s1 = _d === void 0 ? '10' : _d, c1 = _c[1];
var _e = {
s2: {
prop1: new D(),
prop2: new D()
},
c2: false
}, _f = _e.s2, s2 = _f === void 0 ? {
prop1: new C(),
prop2: new C()
} : _f, c2 = _e.c2;
var _g = ([
[
[
10,
20
],
"blah"
]
])[0], _h = _g[0], s3 = _h === void 0 ? [] : _h, c3 = _g[1];


//// [emitDestructuringWithInitializerInDeclarationFile.d.ts]
declare class C {
}
declare class D extends C {
}
declare var s: string, c: boolean;
declare var s1: string, c1: string;
declare var s2: {
prop1: D;
prop2: D;
}, c2: boolean;
declare var s3: number[], c3: string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/conformance/es6/destructuring/emitDestructuringWithInitializerInDeclarationFile.ts ===
class C { }
>C : C

class D extends C { }
>D : D
>C : C

var {s = '10', c} = { s: 'bar', c: true };
>s : string
>c : boolean
>{ s: 'bar', c: true } : { s: string; c: boolean; }
>s : string
>c : boolean

var [s1 = '10', c1] = ['hello', 'word'];
>s1 : string
>c1 : string
>['hello', 'word'] : [string, string]

var {s2 = { prop1: new C(), prop2: new C() }, c2} = { s2: { prop1: new D(), prop2: new D() }, c2: false };
>s2 : { prop1: D; prop2: D; }
>{ prop1: new C(), prop2: new C() } : { prop1: C; prop2: C; }
>prop1 : C
>new C() : C
>C : typeof C
>prop2 : C
>new C() : C
>C : typeof C
>c2 : boolean
>{ s2: { prop1: new D(), prop2: new D() }, c2: false } : { s2: { prop1: D; prop2: D; }; c2: boolean; }
>s2 : { prop1: D; prop2: D; }
>{ prop1: new D(), prop2: new D() } : { prop1: D; prop2: D; }
>prop1 : D
>new D() : D
>D : typeof D
>prop2 : D
>new D() : D
>D : typeof D
>c2 : boolean

var [[s3 = [], c3]] = [[[10,20], "blah"]];
>s3 : number[]
>[] : undefined[]
>c3 : string
>[[[10,20], "blah"]] : [[number[], string]]
>[[10,20], "blah"] : [number[], string]
>[10,20] : number[]

Loading