Skip to content

Commit 51e8f7d

Browse files
committed
Merge pull request #7555 from Microsoft/destructuring-variable-declarations
Destructuring in variable declarations when module kind is not ES6
2 parents 223730d + c9ef8be commit 51e8f7d

33 files changed

+375
-4
lines changed

src/compiler/emitter.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,12 +4218,31 @@ 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+
// In System modules top level variables are hoisted
4232+
// so variable declarations with destructuring are turned into destructuring assignments.
4233+
// As a result, they will need parentheses to disambiguate object binding assignments from blocks.
4234+
write("(");
4235+
}
4236+
42254237
emit(node.name);
42264238
emitOptional(" = ", node.initializer);
4239+
4240+
if (isTopLevelDeclarationInSystemModule) {
4241+
write(")");
4242+
}
4243+
}
4244+
else {
4245+
emitDestructuring(node, /*isAssignmentExpressionStatement*/ false);
42274246
}
42284247
}
42294248
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+

0 commit comments

Comments
 (0)