Skip to content

Commit 3ede567

Browse files
committed
use downlevel destructuring for exported variables for target=ES6 if module kind is not ES6
1 parent 1156141 commit 3ede567

33 files changed

+373
-4
lines changed

src/compiler/emitter.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,12 +4218,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
42184218

42194219
function emitVariableDeclaration(node: VariableDeclaration) {
42204220
if (isBindingPattern(node.name)) {
4221-
if (languageVersion < ScriptTarget.ES6) {
4222-
emitDestructuring(node, /*isAssignmentExpressionStatement*/ false);
4223-
}
4224-
else {
4221+
const isExported = getCombinedNodeFlags(node) & NodeFlags.Export;
4222+
if (languageVersion >= ScriptTarget.ES6 && (!isExported || modulekind === ModuleKind.ES6)) {
4223+
// emit ES6 destructuring only if target module is ES6 or variable is not exported
4224+
// exported variables in CJS\AMD are prefixed with 'exports.' so result javascript { exports.toString } = 1; is illegal
4225+
4226+
const isTopLevelDeclarationInSystemModule =
4227+
modulekind === ModuleKind.System &&
4228+
shouldHoistVariable(node, /*checkIfSourceFileLevelDecl*/true);
4229+
4230+
if (isTopLevelDeclarationInSystemModule) {
4231+
// is system modules top level variables are hoisted
4232+
write("(");
4233+
}
4234+
42254235
emit(node.name);
42264236
emitOptional(" = ", node.initializer);
4237+
4238+
if (isTopLevelDeclarationInSystemModule) {
4239+
write(")");
4240+
}
4241+
}
4242+
else {
4243+
emitDestructuring(node, /*isAssignmentExpressionStatement*/ false);
42274244
}
42284245
}
42294246
else {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [destructuringInVariableDeclarations1.ts]
2+
export let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
7+
8+
//// [destructuringInVariableDeclarations1.js]
9+
"use strict";
10+
exports.toString = (1).toString;
11+
{
12+
let { toFixed } = 1;
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations1.ts ===
2+
export let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations1.ts, 0, 12))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations1.ts, 2, 9))
7+
}
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations1.ts ===
2+
export let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [destructuringInVariableDeclarations2.ts]
2+
let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
export {};
7+
8+
9+
//// [destructuringInVariableDeclarations2.js]
10+
"use strict";
11+
let { toString } = 1;
12+
{
13+
let { toFixed } = 1;
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations2.ts ===
2+
let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations2.ts, 0, 5))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations2.ts, 2, 9))
7+
}
8+
export {};
9+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations2.ts ===
2+
let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
export {};
11+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [destructuringInVariableDeclarations3.ts]
2+
export let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
7+
8+
//// [destructuringInVariableDeclarations3.js]
9+
define(["require", "exports"], function (require, exports) {
10+
"use strict";
11+
exports.toString = (1).toString;
12+
{
13+
let { toFixed } = 1;
14+
}
15+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations3.ts ===
2+
export let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations3.ts, 0, 12))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations3.ts, 2, 9))
7+
}
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations3.ts ===
2+
export let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [destructuringInVariableDeclarations4.ts]
2+
let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
export {};
7+
8+
9+
//// [destructuringInVariableDeclarations4.js]
10+
define(["require", "exports"], function (require, exports) {
11+
"use strict";
12+
let { toString } = 1;
13+
{
14+
let { toFixed } = 1;
15+
}
16+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations4.ts ===
2+
let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations4.ts, 0, 5))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations4.ts, 2, 9))
7+
}
8+
export {};
9+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations4.ts ===
2+
let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
export {};
11+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [destructuringInVariableDeclarations5.ts]
2+
export let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
7+
8+
//// [destructuringInVariableDeclarations5.js]
9+
(function (factory) {
10+
if (typeof module === 'object' && typeof module.exports === 'object') {
11+
var v = factory(require, exports); if (v !== undefined) module.exports = v;
12+
}
13+
else if (typeof define === 'function' && define.amd) {
14+
define(["require", "exports"], factory);
15+
}
16+
})(function (require, exports) {
17+
"use strict";
18+
exports.toString = (1).toString;
19+
{
20+
let { toFixed } = 1;
21+
}
22+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations5.ts ===
2+
export let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations5.ts, 0, 12))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations5.ts, 2, 9))
7+
}
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations5.ts ===
2+
export let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [destructuringInVariableDeclarations6.ts]
2+
let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
export {};
7+
8+
9+
//// [destructuringInVariableDeclarations6.js]
10+
(function (factory) {
11+
if (typeof module === 'object' && typeof module.exports === 'object') {
12+
var v = factory(require, exports); if (v !== undefined) module.exports = v;
13+
}
14+
else if (typeof define === 'function' && define.amd) {
15+
define(["require", "exports"], factory);
16+
}
17+
})(function (require, exports) {
18+
"use strict";
19+
let { toString } = 1;
20+
{
21+
let { toFixed } = 1;
22+
}
23+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations6.ts ===
2+
let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations6.ts, 0, 5))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations6.ts, 2, 9))
7+
}
8+
export {};
9+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations6.ts ===
2+
let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
export {};
11+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [destructuringInVariableDeclarations7.ts]
2+
export let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
7+
8+
//// [destructuringInVariableDeclarations7.js]
9+
System.register([], function(exports_1, context_1) {
10+
"use strict";
11+
var __moduleName = context_1 && context_1.id;
12+
var toString;
13+
return {
14+
setters:[],
15+
execute: function() {
16+
exports_1("toString", toString = (1).toString);
17+
{
18+
let { toFixed } = 1;
19+
}
20+
}
21+
}
22+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations7.ts ===
2+
export let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations7.ts, 0, 12))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations7.ts, 2, 9))
7+
}
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations7.ts ===
2+
export let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [destructuringInVariableDeclarations8.ts]
2+
let { toString } = 1;
3+
{
4+
let { toFixed } = 1;
5+
}
6+
export {};
7+
8+
9+
//// [destructuringInVariableDeclarations8.js]
10+
System.register([], function(exports_1, context_1) {
11+
"use strict";
12+
var __moduleName = context_1 && context_1.id;
13+
var toString;
14+
return {
15+
setters:[],
16+
execute: function() {
17+
({ toString } = 1);
18+
{
19+
let { toFixed } = 1;
20+
}
21+
}
22+
}
23+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations8.ts ===
2+
let { toString } = 1;
3+
>toString : Symbol(toString, Decl(destructuringInVariableDeclarations8.ts, 0, 5))
4+
{
5+
let { toFixed } = 1;
6+
>toFixed : Symbol(toFixed, Decl(destructuringInVariableDeclarations8.ts, 2, 9))
7+
}
8+
export {};
9+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/destructuringInVariableDeclarations8.ts ===
2+
let { toString } = 1;
3+
>toString : (radix?: number) => string
4+
>1 : number
5+
{
6+
let { toFixed } = 1;
7+
>toFixed : (fractionDigits?: number) => string
8+
>1 : number
9+
}
10+
export {};
11+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: es6
2+
// @module: commonjs
3+
export let { toString } = 1;
4+
{
5+
let { toFixed } = 1;
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @target: es6
2+
// @module: commonjs
3+
let { toString } = 1;
4+
{
5+
let { toFixed } = 1;
6+
}
7+
export {};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: es6
2+
// @module: amd
3+
export let { toString } = 1;
4+
{
5+
let { toFixed } = 1;
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @target: es6
2+
// @module: amd
3+
let { toString } = 1;
4+
{
5+
let { toFixed } = 1;
6+
}
7+
export {};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: es6
2+
// @module: umd
3+
export let { toString } = 1;
4+
{
5+
let { toFixed } = 1;
6+
}

0 commit comments

Comments
 (0)