Skip to content

Commit

Permalink
Merge pull request #7555 from Microsoft/destructuring-variable-declar…
Browse files Browse the repository at this point in the history
…ations

Destructuring in variable declarations when module kind is not ES6
  • Loading branch information
vladima committed Mar 17, 2016
2 parents 223730d + c9ef8be commit 51e8f7d
Show file tree
Hide file tree
Showing 33 changed files with 375 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4218,12 +4218,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge

function emitVariableDeclaration(node: VariableDeclaration) {
if (isBindingPattern(node.name)) {
if (languageVersion < ScriptTarget.ES6) {
emitDestructuring(node, /*isAssignmentExpressionStatement*/ false);
}
else {
const isExported = getCombinedNodeFlags(node) & NodeFlags.Export;
if (languageVersion >= ScriptTarget.ES6 && (!isExported || modulekind === ModuleKind.ES6)) {
// emit ES6 destructuring only if target module is ES6 or variable is not exported
// exported variables in CJS/AMD are prefixed with 'exports.' so result javascript { exports.toString } = 1; is illegal

const isTopLevelDeclarationInSystemModule =
modulekind === ModuleKind.System &&
shouldHoistVariable(node, /*checkIfSourceFileLevelDecl*/true);

if (isTopLevelDeclarationInSystemModule) {
// In System modules top level variables are hoisted
// so variable declarations with destructuring are turned into destructuring assignments.
// As a result, they will need parentheses to disambiguate object binding assignments from blocks.
write("(");
}

emit(node.name);
emitOptional(" = ", node.initializer);

if (isTopLevelDeclarationInSystemModule) {
write(")");
}
}
else {
emitDestructuring(node, /*isAssignmentExpressionStatement*/ false);
}
}
else {
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [destructuringInVariableDeclarations1.ts]
export let { toString } = 1;
{
let { toFixed } = 1;
}


//// [destructuringInVariableDeclarations1.js]
"use strict";
exports.toString = (1).toString;
{
let { toFixed } = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/destructuringInVariableDeclarations1.ts ===
export let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations1.ts, 0, 12))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations1.ts, 2, 9))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/destructuringInVariableDeclarations1.ts ===
export let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}

14 changes: 14 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [destructuringInVariableDeclarations2.ts]
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};


//// [destructuringInVariableDeclarations2.js]
"use strict";
let { toString } = 1;
{
let { toFixed } = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/destructuringInVariableDeclarations2.ts ===
let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations2.ts, 0, 5))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations2.ts, 2, 9))
}
export {};

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/destructuringInVariableDeclarations2.ts ===
let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}
export {};

15 changes: 15 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [destructuringInVariableDeclarations3.ts]
export let { toString } = 1;
{
let { toFixed } = 1;
}


//// [destructuringInVariableDeclarations3.js]
define(["require", "exports"], function (require, exports) {
"use strict";
exports.toString = (1).toString;
{
let { toFixed } = 1;
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/destructuringInVariableDeclarations3.ts ===
export let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations3.ts, 0, 12))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations3.ts, 2, 9))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/destructuringInVariableDeclarations3.ts ===
export let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}

16 changes: 16 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [destructuringInVariableDeclarations4.ts]
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};


//// [destructuringInVariableDeclarations4.js]
define(["require", "exports"], function (require, exports) {
"use strict";
let { toString } = 1;
{
let { toFixed } = 1;
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/destructuringInVariableDeclarations4.ts ===
let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations4.ts, 0, 5))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations4.ts, 2, 9))
}
export {};

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/destructuringInVariableDeclarations4.ts ===
let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}
export {};

22 changes: 22 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [destructuringInVariableDeclarations5.ts]
export let { toString } = 1;
{
let { toFixed } = 1;
}


//// [destructuringInVariableDeclarations5.js]
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
exports.toString = (1).toString;
{
let { toFixed } = 1;
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/destructuringInVariableDeclarations5.ts ===
export let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations5.ts, 0, 12))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations5.ts, 2, 9))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/destructuringInVariableDeclarations5.ts ===
export let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [destructuringInVariableDeclarations6.ts]
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};


//// [destructuringInVariableDeclarations6.js]
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
let { toString } = 1;
{
let { toFixed } = 1;
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/destructuringInVariableDeclarations6.ts ===
let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations6.ts, 0, 5))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations6.ts, 2, 9))
}
export {};

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/destructuringInVariableDeclarations6.ts ===
let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}
export {};

22 changes: 22 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [destructuringInVariableDeclarations7.ts]
export let { toString } = 1;
{
let { toFixed } = 1;
}


//// [destructuringInVariableDeclarations7.js]
System.register([], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var toString;
return {
setters:[],
execute: function() {
exports_1("toString", toString = (1).toString);
{
let { toFixed } = 1;
}
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/destructuringInVariableDeclarations7.ts ===
export let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations7.ts, 0, 12))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations7.ts, 2, 9))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/destructuringInVariableDeclarations7.ts ===
export let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/destructuringInVariableDeclarations8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [destructuringInVariableDeclarations8.ts]
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};


//// [destructuringInVariableDeclarations8.js]
System.register([], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var toString;
return {
setters:[],
execute: function() {
({ toString } = 1);
{
let { toFixed } = 1;
}
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/destructuringInVariableDeclarations8.ts ===
let { toString } = 1;
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations8.ts, 0, 5))
{
let { toFixed } = 1;
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations8.ts, 2, 9))
}
export {};

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/destructuringInVariableDeclarations8.ts ===
let { toString } = 1;
>toString : (radix?: number) => string
>1 : number
{
let { toFixed } = 1;
>toFixed : (fractionDigits?: number) => string
>1 : number
}
export {};

6 changes: 6 additions & 0 deletions tests/cases/compiler/destructuringInVariableDeclarations1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: es6
// @module: commonjs
export let { toString } = 1;
{
let { toFixed } = 1;
}
7 changes: 7 additions & 0 deletions tests/cases/compiler/destructuringInVariableDeclarations2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @target: es6
// @module: commonjs
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};
6 changes: 6 additions & 0 deletions tests/cases/compiler/destructuringInVariableDeclarations3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: es6
// @module: amd
export let { toString } = 1;
{
let { toFixed } = 1;
}
7 changes: 7 additions & 0 deletions tests/cases/compiler/destructuringInVariableDeclarations4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @target: es6
// @module: amd
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};
6 changes: 6 additions & 0 deletions tests/cases/compiler/destructuringInVariableDeclarations5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: es6
// @module: umd
export let { toString } = 1;
{
let { toFixed } = 1;
}
Loading

0 comments on commit 51e8f7d

Please sign in to comment.