Skip to content

Commit 63b1540

Browse files
committed
Merge branch 'streamlineDestructuring' into emitHelper
2 parents ec95f4f + 3a67174 commit 63b1540

File tree

7 files changed

+43
-29
lines changed

7 files changed

+43
-29
lines changed

src/compiler/transformer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ namespace ts {
265265
/** Resumes a suspended lexical environment, usually before visiting a function body. */
266266
function resumeLexicalEnvironment(): void {
267267
Debug.assert(!scopeModificationDisabled, "Cannot resume a lexical environment during the print phase.");
268-
Debug.assert(lexicalEnvironmentSuspended, "Lexical environment is not suspended suspended.");
268+
Debug.assert(lexicalEnvironmentSuspended, "Lexical environment is not suspended.");
269269
lexicalEnvironmentSuspended = false;
270270
}
271271

src/compiler/transformers/es2015.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,14 @@ namespace ts {
840840
* @param hasSynthesizedSuper A value indicating whether the constructor starts with a
841841
* synthesized `super` call.
842842
*/
843-
function transformConstructorParameters(constructor: ConstructorDeclaration, hasSynthesizedSuper: boolean): ParameterDeclaration[] {
843+
function transformConstructorParameters(constructor: ConstructorDeclaration, hasSynthesizedSuper: boolean) {
844844
// If the TypeScript transformer needed to synthesize a constructor for property
845845
// initializers, it would have also added a synthetic `...args` parameter and
846846
// `super` call.
847847
// If this is the case, we do not include the synthetic `...args` parameter and
848848
// will instead use the `arguments` object in ES5/3.
849849
return visitParameterList(constructor && !hasSynthesizedSuper && constructor.parameters, visitor, context)
850-
|| [];
850+
|| <ParameterDeclaration[]>[];
851851
}
852852

853853
/**

src/compiler/transformers/es2017.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,10 @@ namespace ts {
6161
}
6262

6363
function visitor(node: Node): VisitResult<Node> {
64-
if (node.transformFlags & TransformFlags.ContainsES2017) {
65-
return visitorWorker(node);
64+
if ((node.transformFlags & TransformFlags.ContainsES2017) === 0) {
65+
return node;
6666
}
67-
return node;
68-
}
6967

70-
function visitorWorker(node: Node): VisitResult<Node> {
7168
switch (node.kind) {
7269
case SyntaxKind.AsyncKeyword:
7370
// ES2017 async modifier should be elided for targets < ES2017
@@ -211,6 +208,8 @@ namespace ts {
211208
function transformAsyncFunctionBody(node: MethodDeclaration | AccessorDeclaration | FunctionDeclaration | FunctionExpression): FunctionBody;
212209
function transformAsyncFunctionBody(node: ArrowFunction): ConciseBody;
213210
function transformAsyncFunctionBody(node: FunctionLikeDeclaration): ConciseBody {
211+
resumeLexicalEnvironment();
212+
214213
const original = getOriginalNode(node, isFunctionLike);
215214
const nodeType = original.type;
216215
const promiseConstructor = languageVersion < ScriptTarget.ES2015 ? getPromiseConstructor(nodeType) : undefined;
@@ -222,7 +221,6 @@ namespace ts {
222221
// `this` and `arguments` objects to `__awaiter`. The generator function
223222
// passed to `__awaiter` is executed inside of the callback to the
224223
// promise constructor.
225-
resumeLexicalEnvironment();
226224

227225
if (!isArrowFunction) {
228226
const statements: Statement[] = [];

src/compiler/transformers/esnext.ts

+5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
namespace ts {
66
export function transformESNext(context: TransformationContext) {
77
const {
8+
resumeLexicalEnvironment,
89
endLexicalEnvironment
910
} = context;
1011
return transformSourceFile;
1112

1213
function transformSourceFile(node: SourceFile) {
14+
if (isDeclarationFile(node)) {
15+
return node;
16+
}
1317

1418
const visited = visitEachChild(node, visitor, context);
1519
addEmitHelpers(visited, context.readEmitHelpers());
@@ -346,6 +350,7 @@ namespace ts {
346350
function transformFunctionBody(node: FunctionDeclaration | FunctionExpression | ConstructorDeclaration | MethodDeclaration | AccessorDeclaration): FunctionBody;
347351
function transformFunctionBody(node: ArrowFunction): ConciseBody;
348352
function transformFunctionBody(node: FunctionLikeDeclaration): ConciseBody {
353+
resumeLexicalEnvironment();
349354
let leadingStatements: Statement[];
350355
for (const parameter of node.parameters) {
351356
if (parameter.transformFlags & TransformFlags.ContainsObjectRest) {

src/compiler/transformers/generators.ts

+25-13
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ namespace ts {
227227

228228
export function transformGenerators(context: TransformationContext) {
229229
const {
230-
startLexicalEnvironment,
230+
resumeLexicalEnvironment,
231231
endLexicalEnvironment,
232232
hoistFunctionDeclaration,
233233
hoistVariableDeclaration,
@@ -450,11 +450,11 @@ namespace ts {
450450
node = setOriginalNode(
451451
createFunctionDeclaration(
452452
/*decorators*/ undefined,
453-
/*modifiers*/ undefined,
453+
node.modifiers,
454454
/*asteriskToken*/ undefined,
455455
node.name,
456456
/*typeParameters*/ undefined,
457-
node.parameters,
457+
visitParameterList(node.parameters, visitor, context),
458458
/*type*/ undefined,
459459
transformGeneratorFunctionBody(node.body),
460460
/*location*/ node
@@ -501,7 +501,7 @@ namespace ts {
501501
/*asteriskToken*/ undefined,
502502
node.name,
503503
/*typeParameters*/ undefined,
504-
node.parameters,
504+
visitParameterList(node.parameters, visitor, context),
505505
/*type*/ undefined,
506506
transformGeneratorFunctionBody(node.body),
507507
/*location*/ node
@@ -579,7 +579,7 @@ namespace ts {
579579
state = createTempVariable(/*recordTempVariable*/ undefined);
580580

581581
// Build the generator
582-
startLexicalEnvironment();
582+
resumeLexicalEnvironment();
583583

584584
const statementOffset = addPrologueDirectives(statements, body.statements, /*ensureUseStrict*/ false, visitor);
585585

@@ -947,7 +947,7 @@ namespace ts {
947947
* @param node The node to visit.
948948
*/
949949
function visitArrayLiteralExpression(node: ArrayLiteralExpression) {
950-
return visitElements(node.elements, node.multiLine);
950+
return visitElements(node.elements, /*leadingElement*/ undefined, /*location*/ undefined, node.multiLine);
951951
}
952952

953953
/**
@@ -957,7 +957,7 @@ namespace ts {
957957
* @param elements The elements to visit.
958958
* @param multiLine Whether array literals created should be emitted on multiple lines.
959959
*/
960-
function visitElements(elements: NodeArray<Expression>, _multiLine?: boolean) {
960+
function visitElements(elements: NodeArray<Expression>, leadingElement?: Expression, location?: TextRange, multiLine?: boolean) {
961961
// [source]
962962
// ar = [1, yield, 2];
963963
//
@@ -972,18 +972,22 @@ namespace ts {
972972
const temp = declareLocal();
973973
let hasAssignedTemp = false;
974974
if (numInitialElements > 0) {
975+
const initialElements = visitNodes(elements, visitor, isExpression, 0, numInitialElements);
975976
emitAssignment(temp,
976977
createArrayLiteral(
977-
visitNodes(elements, visitor, isExpression, 0, numInitialElements)
978+
leadingElement
979+
? [leadingElement, ...initialElements]
980+
: initialElements
978981
)
979982
);
983+
leadingElement = undefined;
980984
hasAssignedTemp = true;
981985
}
982986

983987
const expressions = reduceLeft(elements, reduceElement, <Expression[]>[], numInitialElements);
984988
return hasAssignedTemp
985-
? createArrayConcat(temp, [createArrayLiteral(expressions)])
986-
: createArrayLiteral(expressions);
989+
? createArrayConcat(temp, [createArrayLiteral(expressions, /*location*/ undefined, multiLine)])
990+
: createArrayLiteral(leadingElement ? [leadingElement, ...expressions] : expressions, location, multiLine);
987991

988992
function reduceElement(expressions: Expression[], element: Expression) {
989993
if (containsYield(element) && expressions.length > 0) {
@@ -992,11 +996,16 @@ namespace ts {
992996
hasAssignedTemp
993997
? createArrayConcat(
994998
temp,
995-
[createArrayLiteral(expressions)]
999+
[createArrayLiteral(expressions, /*location*/ undefined, multiLine)]
1000+
)
1001+
: createArrayLiteral(
1002+
leadingElement ? [leadingElement, ...expressions] : expressions,
1003+
/*location*/ undefined,
1004+
multiLine
9961005
)
997-
: createArrayLiteral(expressions)
9981006
);
9991007
hasAssignedTemp = true;
1008+
leadingElement = undefined;
10001009
expressions = [];
10011010
}
10021011

@@ -1132,7 +1141,10 @@ namespace ts {
11321141
createFunctionApply(
11331142
cacheExpression(visitNode(target, visitor, isExpression)),
11341143
thisArg,
1135-
visitElements(node.arguments)
1144+
visitElements(
1145+
node.arguments,
1146+
/*leadingElement*/ createVoidZero()
1147+
)
11361148
),
11371149
/*typeArguments*/ undefined,
11381150
[],

src/compiler/transformers/ts.ts

-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,6 @@ namespace ts {
862862
const statements: Statement[] = [];
863863
let indexOfFirstStatement = 0;
864864

865-
// The body of a constructor is a new lexical environment
866865
resumeLexicalEnvironment();
867866

868867
if (constructor) {

tests/baselines/reference/es5-asyncFunctionNewExpressions.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function newExpression2() {
119119
_a = x.bind;
120120
return [4 /*yield*/, y];
121121
case 1:
122-
new (_a.apply(x, [_c.sent(), z]))();
122+
new (_a.apply(x, [void 0, _c.sent(), z]))();
123123
return [2 /*return*/];
124124
}
125125
});
@@ -132,7 +132,7 @@ function newExpression3() {
132132
switch (_c.label) {
133133
case 0:
134134
_a = x.bind;
135-
_b = [y];
135+
_b = [void 0, y];
136136
return [4 /*yield*/, z];
137137
case 1:
138138
new (_a.apply(x, _b.concat([_c.sent()])))();
@@ -280,7 +280,7 @@ function newExpression13() {
280280
_b = (_a = x.a).bind;
281281
return [4 /*yield*/, y];
282282
case 1:
283-
new (_b.apply(_a, [_d.sent(), z]))();
283+
new (_b.apply(_a, [void 0, _d.sent(), z]))();
284284
return [2 /*return*/];
285285
}
286286
});
@@ -293,7 +293,7 @@ function newExpression14() {
293293
switch (_d.label) {
294294
case 0:
295295
_b = (_a = x.a).bind;
296-
_c = [y];
296+
_c = [void 0, y];
297297
return [4 /*yield*/, z];
298298
case 1:
299299
new (_b.apply(_a, _c.concat([_d.sent()])))();
@@ -362,7 +362,7 @@ function newExpression19() {
362362
_b = (_a = x[a]).bind;
363363
return [4 /*yield*/, y];
364364
case 1:
365-
new (_b.apply(_a, [_d.sent(), z]))();
365+
new (_b.apply(_a, [void 0, _d.sent(), z]))();
366366
return [2 /*return*/];
367367
}
368368
});
@@ -375,7 +375,7 @@ function newExpression20() {
375375
switch (_d.label) {
376376
case 0:
377377
_b = (_a = x[a]).bind;
378-
_c = [y];
378+
_c = [void 0, y];
379379
return [4 /*yield*/, z];
380380
case 1:
381381
new (_b.apply(_a, _c.concat([_d.sent()])))();

0 commit comments

Comments
 (0)