use downlevel destructuring for exported variables for target=ES6 if module kind is not ES6

This commit is contained in:
Vladimir Matveev 2016-03-16 16:40:35 -07:00
parent 1156141b5e
commit 3ede567fbc
33 changed files with 373 additions and 4 deletions

View File

@ -4218,12 +4218,29 @@ 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) {
// is system modules top level variables are hoisted
write("(");
}
emit(node.name);
emitOptional(" = ", node.initializer);
if (isTopLevelDeclarationInSystemModule) {
write(")");
}
}
else {
emitDestructuring(node, /*isAssignmentExpressionStatement*/ false);
}
}
else {

View File

@ -0,0 +1,13 @@
//// [destructuringInVariableDeclarations1.ts]
export let { toString } = 1;
{
let { toFixed } = 1;
}
//// [destructuringInVariableDeclarations1.js]
"use strict";
exports.toString = (1).toString;
{
let { toFixed } = 1;
}

View File

@ -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))
}

View File

@ -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
}

View File

@ -0,0 +1,14 @@
//// [destructuringInVariableDeclarations2.ts]
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};
//// [destructuringInVariableDeclarations2.js]
"use strict";
let { toString } = 1;
{
let { toFixed } = 1;
}

View File

@ -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 {};

View File

@ -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 {};

View File

@ -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;
}
});

View File

@ -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))
}

View File

@ -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
}

View File

@ -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;
}
});

View File

@ -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 {};

View File

@ -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 {};

View File

@ -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;
}
});

View File

@ -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))
}

View File

@ -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
}

View File

@ -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;
}
});

View File

@ -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 {};

View File

@ -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 {};

View File

@ -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;
}
}
}
});

View File

@ -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))
}

View File

@ -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
}

View File

@ -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;
}
}
}
});

View File

@ -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 {};

View File

@ -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 {};

View File

@ -0,0 +1,6 @@
// @target: es6
// @module: commonjs
export let { toString } = 1;
{
let { toFixed } = 1;
}

View File

@ -0,0 +1,7 @@
// @target: es6
// @module: commonjs
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};

View File

@ -0,0 +1,6 @@
// @target: es6
// @module: amd
export let { toString } = 1;
{
let { toFixed } = 1;
}

View File

@ -0,0 +1,7 @@
// @target: es6
// @module: amd
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};

View File

@ -0,0 +1,6 @@
// @target: es6
// @module: umd
export let { toString } = 1;
{
let { toFixed } = 1;
}

View File

@ -0,0 +1,7 @@
// @target: es6
// @module: umd
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};

View File

@ -0,0 +1,6 @@
// @target: es6
// @module: system
export let { toString } = 1;
{
let { toFixed } = 1;
}

View File

@ -0,0 +1,7 @@
// @target: es6
// @module: system
let { toString } = 1;
{
let { toFixed } = 1;
}
export {};