Skip to content

Commit 1f791e2

Browse files
authored
Merge pull request #11131 from Microsoft/fix11037
Fix some issues with module ES6/target ES5
2 parents 992b385 + 7f98d3d commit 1f791e2

8 files changed

+61
-17
lines changed

src/compiler/transformers/destructuring.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,22 @@ namespace ts {
126126
context: TransformationContext,
127127
node: VariableDeclaration,
128128
value?: Expression,
129-
visitor?: (node: Node) => VisitResult<Node>) {
129+
visitor?: (node: Node) => VisitResult<Node>,
130+
recordTempVariable?: (node: Identifier) => void) {
130131
const declarations: VariableDeclaration[] = [];
131132

133+
let pendingAssignments: Expression[];
132134
flattenDestructuring(context, node, value, node, emitAssignment, emitTempVariableAssignment, visitor);
133135

134136
return declarations;
135137

136138
function emitAssignment(name: Identifier, value: Expression, location: TextRange, original: Node) {
139+
if (pendingAssignments) {
140+
pendingAssignments.push(value);
141+
value = inlineExpressions(pendingAssignments);
142+
pendingAssignments = undefined;
143+
}
144+
137145
const declaration = createVariableDeclaration(name, /*type*/ undefined, value, location);
138146
declaration.original = original;
139147

@@ -146,8 +154,19 @@ namespace ts {
146154
}
147155

148156
function emitTempVariableAssignment(value: Expression, location: TextRange) {
149-
const name = createTempVariable(/*recordTempVariable*/ undefined);
150-
emitAssignment(name, value, location, /*original*/ undefined);
157+
const name = createTempVariable(recordTempVariable);
158+
if (recordTempVariable) {
159+
const assignment = createAssignment(name, value, location);
160+
if (pendingAssignments) {
161+
pendingAssignments.push(assignment);
162+
}
163+
else {
164+
pendingAssignments = [assignment];
165+
}
166+
}
167+
else {
168+
emitAssignment(name, value, location, /*original*/ undefined);
169+
}
151170
return name;
152171
}
153172
}

src/compiler/transformers/es6.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ namespace ts {
163163
let currentText: string;
164164
let currentParent: Node;
165165
let currentNode: Node;
166+
let enclosingVariableStatement: VariableStatement;
166167
let enclosingBlockScopeContainer: Node;
167168
let enclosingBlockScopeContainerParent: Node;
168169
let containingNonArrowFunction: FunctionLikeDeclaration | ClassElement;
@@ -210,6 +211,7 @@ namespace ts {
210211
const savedSuperScopeContainer = superScopeContainer;
211212
const savedCurrentParent = currentParent;
212213
const savedCurrentNode = currentNode;
214+
const savedEnclosingVariableStatement = enclosingVariableStatement;
213215
const savedEnclosingBlockScopeContainer = enclosingBlockScopeContainer;
214216
const savedEnclosingBlockScopeContainerParent = enclosingBlockScopeContainerParent;
215217

@@ -227,6 +229,7 @@ namespace ts {
227229
superScopeContainer = savedSuperScopeContainer;
228230
currentParent = savedCurrentParent;
229231
currentNode = savedCurrentNode;
232+
enclosingVariableStatement = savedEnclosingVariableStatement;
230233
enclosingBlockScopeContainer = savedEnclosingBlockScopeContainer;
231234
enclosingBlockScopeContainerParent = savedEnclosingBlockScopeContainerParent;
232235
return visited;
@@ -320,7 +323,7 @@ namespace ts {
320323
return visitFunctionExpression(<FunctionExpression>node);
321324

322325
case SyntaxKind.VariableDeclaration:
323-
return visitVariableDeclaration(<VariableDeclaration>node, /*offset*/ undefined);
326+
return visitVariableDeclaration(<VariableDeclaration>node);
324327

325328
case SyntaxKind.Identifier:
326329
return visitIdentifier(<Identifier>node);
@@ -432,6 +435,25 @@ namespace ts {
432435
}
433436
break;
434437
}
438+
439+
// keep track of the enclosing variable statement when in the context of
440+
// variable statements, variable declarations, binding elements, and binding
441+
// patterns.
442+
switch (currentParent.kind) {
443+
case SyntaxKind.VariableStatement:
444+
enclosingVariableStatement = <VariableStatement>currentParent;
445+
break;
446+
447+
case SyntaxKind.VariableDeclarationList:
448+
case SyntaxKind.VariableDeclaration:
449+
case SyntaxKind.BindingElement:
450+
case SyntaxKind.ObjectBindingPattern:
451+
case SyntaxKind.ArrayBindingPattern:
452+
break;
453+
454+
default:
455+
enclosingVariableStatement = undefined;
456+
}
435457
}
436458
}
437459

@@ -1334,7 +1356,7 @@ namespace ts {
13341356
return setOriginalNode(
13351357
createFunctionDeclaration(
13361358
/*decorators*/ undefined,
1337-
/*modifiers*/ undefined,
1359+
node.modifiers,
13381360
node.asteriskToken,
13391361
node.name,
13401362
/*typeParameters*/ undefined,
@@ -1663,13 +1685,13 @@ namespace ts {
16631685
*
16641686
* @param node A VariableDeclaration node.
16651687
*/
1666-
function visitVariableDeclarationInLetDeclarationList(node: VariableDeclaration, offset: number) {
1688+
function visitVariableDeclarationInLetDeclarationList(node: VariableDeclaration) {
16671689
// For binding pattern names that lack initializers there is no point to emit
16681690
// explicit initializer since downlevel codegen for destructuring will fail
16691691
// in the absence of initializer so all binding elements will say uninitialized
16701692
const name = node.name;
16711693
if (isBindingPattern(name)) {
1672-
return visitVariableDeclaration(node, offset);
1694+
return visitVariableDeclaration(node);
16731695
}
16741696

16751697
if (!node.initializer && shouldEmitExplicitInitializerForLetDeclaration(node)) {
@@ -1686,10 +1708,13 @@ namespace ts {
16861708
*
16871709
* @param node A VariableDeclaration node.
16881710
*/
1689-
function visitVariableDeclaration(node: VariableDeclaration, offset: number): VisitResult<VariableDeclaration> {
1711+
function visitVariableDeclaration(node: VariableDeclaration): VisitResult<VariableDeclaration> {
16901712
// If we are here it is because the name contains a binding pattern.
16911713
if (isBindingPattern(node.name)) {
1692-
return flattenVariableDestructuring(context, node, /*value*/ undefined, visitor);
1714+
const recordTempVariablesInLine = !enclosingVariableStatement
1715+
|| !hasModifier(enclosingVariableStatement, ModifierFlags.Export);
1716+
return flattenVariableDestructuring(context, node, /*value*/ undefined, visitor,
1717+
recordTempVariablesInLine ? undefined : hoistVariableDeclaration);
16931718
}
16941719

16951720
return visitEachChild(node, visitor, context);

tests/baselines/reference/es6modulekindWithES5Target6.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export default function f3(d = 0) {
1111

1212

1313
//// [es6modulekindWithES5Target6.js]
14-
function f1(d) {
14+
export function f1(d) {
1515
if (d === void 0) { d = 0; }
1616
}
17-
function f2() {
17+
export function f2() {
1818
var arg = [];
1919
for (var _i = 0; _i < arguments.length; _i++) {
2020
arg[_i - 0] = arguments[_i];
2121
}
2222
}
23-
function f3(d) {
23+
export default function f3(d) {
2424
if (d === void 0) { d = 0; }
2525
}

tests/baselines/reference/functionsWithModifiersInBlocks1.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
//// [functionsWithModifiersInBlocks1.js]
99
{
10-
function f() { }
10+
export function f() { }
1111
}

tests/baselines/reference/moduleElementsInWrongContext.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
return C;
4646
}());
4747
export default C;
48-
function bee() { }
48+
export function bee() { }
4949
import I2 = require("foo");
5050
import * as Foo from "ambient";
5151
import bar from "ambient";

tests/baselines/reference/moduleElementsInWrongContext2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function blah() {
4545
return C;
4646
}());
4747
export default C;
48-
function bee() { }
48+
export function bee() { }
4949
import I2 = require("foo");
5050
import * as Foo from "ambient";
5151
import bar from "ambient";

tests/baselines/reference/parserModifierOnStatementInBlock3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function foo() {
88
//// [parserModifierOnStatementInBlock3.js]
99
"use strict";
1010
function foo() {
11-
function bar() {
11+
export function bar() {
1212
}
1313
}
1414
exports.foo = foo;

tests/baselines/reference/parserModifierOnStatementInBlock4.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
//// [parserModifierOnStatementInBlock4.js]
99
{
10-
function bar() {
10+
export function bar() {
1111
}
1212
}

0 commit comments

Comments
 (0)