Skip to content

Commit d119167

Browse files
committed
Add more commonly used nodes, reduce less frequently used nodes.
1 parent ec9c4d2 commit d119167

File tree

8 files changed

+646
-403
lines changed

8 files changed

+646
-403
lines changed

src/compiler/factory.ts

Lines changed: 242 additions & 89 deletions
Large diffs are not rendered by default.

src/compiler/transformers/es6.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ namespace ts {
11961196
*/
11971197
function transformAccessorsToStatement(receiver: LeftHandSideExpression, accessors: AllAccessorDeclarations): Statement {
11981198
const statement = createStatement(
1199-
transformAccessorsToExpression(receiver, accessors),
1199+
transformAccessorsToExpression(receiver, accessors, /*startsOnNewLine*/ false),
12001200
/*location*/ getSourceMapRange(accessors.firstAccessor)
12011201
);
12021202

@@ -1213,7 +1213,7 @@ namespace ts {
12131213
*
12141214
* @param receiver The receiver for the member.
12151215
*/
1216-
function transformAccessorsToExpression(receiver: LeftHandSideExpression, { firstAccessor, getAccessor, setAccessor }: AllAccessorDeclarations): Expression {
1216+
function transformAccessorsToExpression(receiver: LeftHandSideExpression, { firstAccessor, getAccessor, setAccessor }: AllAccessorDeclarations, startsOnNewLine: boolean): Expression {
12171217
// To align with source maps in the old emitter, the receiver and property name
12181218
// arguments are both mapped contiguously to the accessor name.
12191219
const target = getMutableClone(receiver);
@@ -1246,7 +1246,7 @@ namespace ts {
12461246
createPropertyAssignment("configurable", createLiteral(true))
12471247
);
12481248

1249-
return createCall(
1249+
const call = createCall(
12501250
createPropertyAccess(createIdentifier("Object"), "defineProperty"),
12511251
/*typeArguments*/ undefined,
12521252
[
@@ -1255,6 +1255,10 @@ namespace ts {
12551255
createObjectLiteral(properties, /*location*/ undefined, /*multiLine*/ true)
12561256
]
12571257
);
1258+
if (startsOnNewLine) {
1259+
call.startsOnNewLine = true;
1260+
}
1261+
return call;
12581262
}
12591263

12601264
/**
@@ -1895,26 +1899,28 @@ namespace ts {
18951899

18961900
// Write out the first non-computed properties, then emit the rest through indexing on the temp variable.
18971901
const expressions: Expression[] = [];
1898-
addNode(expressions,
1899-
createAssignment(
1900-
temp,
1901-
setNodeEmitFlags(
1902-
createObjectLiteral(
1903-
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties),
1904-
/*location*/ undefined,
1905-
node.multiLine
1906-
),
1907-
NodeEmitFlags.Indented
1908-
)
1909-
),
1910-
node.multiLine
1902+
expressions.push(
1903+
startOnNewLine(
1904+
createAssignment(
1905+
temp,
1906+
setNodeEmitFlags(
1907+
createObjectLiteral(
1908+
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties),
1909+
/*location*/ undefined,
1910+
node.multiLine
1911+
),
1912+
NodeEmitFlags.Indented
1913+
)
1914+
),
1915+
node.multiLine
1916+
)
19111917
);
19121918

19131919
addObjectLiteralMembers(expressions, node, temp, numInitialProperties);
19141920

19151921
// We need to clone the temporary identifier so that we can write it on a
19161922
// new line
1917-
addNode(expressions, getMutableClone(temp), node.multiLine);
1923+
expressions.push(startOnNewLine(getMutableClone(temp), node.multiLine));
19181924
return inlineExpressions(expressions);
19191925
}
19201926

@@ -2313,21 +2319,21 @@ namespace ts {
23132319
case SyntaxKind.SetAccessor:
23142320
const accessors = getAllAccessorDeclarations(node.properties, <AccessorDeclaration>property);
23152321
if (property === accessors.firstAccessor) {
2316-
addNode(expressions, transformAccessorsToExpression(receiver, accessors), node.multiLine);
2322+
expressions.push(transformAccessorsToExpression(receiver, accessors, node.multiLine));
23172323
}
23182324

23192325
break;
23202326

23212327
case SyntaxKind.PropertyAssignment:
2322-
addNode(expressions, transformPropertyAssignmentToExpression(node, <PropertyAssignment>property, receiver), node.multiLine);
2328+
expressions.push(transformPropertyAssignmentToExpression(node, <PropertyAssignment>property, receiver, node.multiLine));
23232329
break;
23242330

23252331
case SyntaxKind.ShorthandPropertyAssignment:
2326-
addNode(expressions, transformShorthandPropertyAssignmentToExpression(node, <ShorthandPropertyAssignment>property, receiver), node.multiLine);
2332+
expressions.push(transformShorthandPropertyAssignmentToExpression(node, <ShorthandPropertyAssignment>property, receiver, node.multiLine));
23272333
break;
23282334

23292335
case SyntaxKind.MethodDeclaration:
2330-
addNode(expressions, transformObjectLiteralMethodDeclarationToExpression(node, <MethodDeclaration>property, receiver), node.multiLine);
2336+
expressions.push(transformObjectLiteralMethodDeclarationToExpression(node, <MethodDeclaration>property, receiver, node.multiLine));
23312337
break;
23322338

23332339
default:
@@ -2344,15 +2350,19 @@ namespace ts {
23442350
* @param property The PropertyAssignment node.
23452351
* @param receiver The receiver for the assignment.
23462352
*/
2347-
function transformPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: PropertyAssignment, receiver: Expression) {
2348-
return createAssignment(
2353+
function transformPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: PropertyAssignment, receiver: Expression, startsOnNewLine: boolean) {
2354+
const expression = createAssignment(
23492355
createMemberAccessForPropertyName(
23502356
receiver,
23512357
visitNode(property.name, visitor, isPropertyName)
23522358
),
23532359
visitNode(property.initializer, visitor, isExpression),
23542360
/*location*/ property
23552361
);
2362+
if (startsOnNewLine) {
2363+
expression.startsOnNewLine = true;
2364+
}
2365+
return expression;
23562366
}
23572367

23582368
/**
@@ -2362,15 +2372,19 @@ namespace ts {
23622372
* @param property The ShorthandPropertyAssignment node.
23632373
* @param receiver The receiver for the assignment.
23642374
*/
2365-
function transformShorthandPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: ShorthandPropertyAssignment, receiver: Expression) {
2366-
return createAssignment(
2375+
function transformShorthandPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: ShorthandPropertyAssignment, receiver: Expression, startsOnNewLine: boolean) {
2376+
const expression = createAssignment(
23672377
createMemberAccessForPropertyName(
23682378
receiver,
23692379
visitNode(property.name, visitor, isPropertyName)
23702380
),
23712381
getSynthesizedClone(property.name),
23722382
/*location*/ property
23732383
);
2384+
if (startsOnNewLine) {
2385+
expression.startsOnNewLine = true;
2386+
}
2387+
return expression;
23742388
}
23752389

23762390
/**
@@ -2380,15 +2394,19 @@ namespace ts {
23802394
* @param method The MethodDeclaration node.
23812395
* @param receiver The receiver for the assignment.
23822396
*/
2383-
function transformObjectLiteralMethodDeclarationToExpression(node: ObjectLiteralExpression, method: MethodDeclaration, receiver: Expression) {
2384-
return createAssignment(
2397+
function transformObjectLiteralMethodDeclarationToExpression(node: ObjectLiteralExpression, method: MethodDeclaration, receiver: Expression, startsOnNewLine: boolean) {
2398+
const expression = createAssignment(
23852399
createMemberAccessForPropertyName(
23862400
receiver,
23872401
visitNode(method.name, visitor, isPropertyName)
23882402
),
23892403
transformFunctionLikeToExpression(method, /*location*/ method, /*name*/ undefined),
23902404
/*location*/ method
23912405
);
2406+
if (startsOnNewLine) {
2407+
expression.startsOnNewLine = true;
2408+
}
2409+
return expression;
23922410
}
23932411

23942412
/**

src/compiler/transformers/generators.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,8 @@ namespace ts {
580580
transformAndEmitStatements(body.statements, statementOffset);
581581

582582
const buildResult = build();
583-
addNodes(statements, endLexicalEnvironment());
584-
addNode(statements, createReturn(buildResult));
583+
addRange(statements, endLexicalEnvironment());
584+
statements.push(createReturn(buildResult));
585585

586586
// Restore previous generator state
587587
inGeneratorFunctionBody = savedInGeneratorFunctionBody;
@@ -1019,7 +1019,7 @@ namespace ts {
10191019
);
10201020

10211021
const expressions = reduceLeft(properties, reduceProperty, <Expression[]>[], numInitialProperties);
1022-
addNode(expressions, getMutableClone(temp), multiLine);
1022+
expressions.push(multiLine ? startOnNewLine(getMutableClone(temp)) : temp);
10231023
return inlineExpressions(expressions);
10241024

10251025
function reduceProperty(expressions: Expression[], property: ObjectLiteralElement) {
@@ -1029,7 +1029,13 @@ namespace ts {
10291029
}
10301030

10311031
const expression = createExpressionForObjectLiteralElement(node, property, temp);
1032-
addNode(expressions, visitNode(expression, visitor, isExpression), multiLine);
1032+
const visited = visitNode(expression, visitor, isExpression);
1033+
if (visited) {
1034+
if (multiLine) {
1035+
visited.startsOnNewLine = true;
1036+
}
1037+
expressions.push(visited);
1038+
}
10331039
return expressions;
10341040
}
10351041
}

src/compiler/transformers/module/es6.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ namespace ts {
6565
return node;
6666
}
6767
return newExportClause
68-
? createExportDeclaration(newExportClause, node.moduleSpecifier)
68+
? createExportDeclaration(
69+
/*decorators*/ undefined,
70+
/*modifiers*/ undefined,
71+
newExportClause,
72+
node.moduleSpecifier)
6973
: undefined;
7074
}
7175

@@ -92,7 +96,11 @@ namespace ts {
9296
return undefined;
9397
}
9498
else if (newImportClause !== node.importClause) {
95-
return createImportDeclaration(newImportClause, node.moduleSpecifier);
99+
return createImportDeclaration(
100+
/*decorators*/ undefined,
101+
/*modifiers*/ undefined,
102+
newImportClause,
103+
node.moduleSpecifier);
96104
}
97105
}
98106
return node;

src/compiler/transformers/module/system.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ namespace ts {
197197
const statementOffset = addPrologueDirectives(statements, node.statements, /*ensureUseStrict*/ !compilerOptions.noImplicitUseStrict, visitSourceElement);
198198

199199
// var __moduleName = context_1 && context_1.id;
200-
addNode(statements,
200+
statements.push(
201201
createVariableStatement(
202202
/*modifiers*/ undefined,
203203
createVariableDeclarationList([
@@ -226,14 +226,14 @@ namespace ts {
226226
// - Temporary variables will appear at the top rather than at the bottom of the file
227227
// - Calls to the exporter for exported function declarations are grouped after
228228
// the declarations.
229-
addNodes(statements, endLexicalEnvironment());
229+
addRange(statements, endLexicalEnvironment());
230230

231231
// Emit early exports for function declarations.
232-
addNodes(statements, exportedFunctionDeclarations);
232+
addRange(statements, exportedFunctionDeclarations);
233233

234234
const exportStarFunction = addExportStarIfNeeded(statements);
235235

236-
addNode(statements,
236+
statements.push(
237237
createReturn(
238238
setMultiLine(
239239
createObjectLiteral([
@@ -292,7 +292,7 @@ namespace ts {
292292
if (exportedLocalNames) {
293293
for (const exportedLocalName of exportedLocalNames) {
294294
// write name of exported declaration, i.e 'export var x...'
295-
addNode(exportedNames,
295+
exportedNames.push(
296296
createPropertyAssignment(
297297
createLiteral(exportedLocalName.text),
298298
createLiteral(true)
@@ -314,7 +314,7 @@ namespace ts {
314314

315315
for (const element of exportDecl.exportClause.elements) {
316316
// write name of indirectly exported entry, i.e. 'export {x} from ...'
317-
addNode(exportedNames,
317+
exportedNames.push(
318318
createPropertyAssignment(
319319
createLiteral((element.name || element.propertyName).text),
320320
createLiteral(true)
@@ -324,7 +324,7 @@ namespace ts {
324324
}
325325

326326
const exportedNamesStorageRef = createUniqueName("exportedNames");
327-
addNode(statements,
327+
statements.push(
328328
createVariableStatement(
329329
/*modifiers*/ undefined,
330330
createVariableDeclarationList([
@@ -365,7 +365,7 @@ namespace ts {
365365
case SyntaxKind.ImportEqualsDeclaration:
366366
Debug.assert(importVariableName !== undefined);
367367
// save import into the local
368-
addNode(statements,
368+
statements.push(
369369
createStatement(
370370
createAssignment(importVariableName, parameterName)
371371
)
@@ -396,7 +396,7 @@ namespace ts {
396396
);
397397
}
398398

399-
addNode(statements,
399+
statements.push(
400400
createStatement(
401401
createCall(
402402
exportFunctionForFile,
@@ -412,7 +412,7 @@ namespace ts {
412412
// emit as:
413413
//
414414
// exportStar(foo_1_1);
415-
addNode(statements,
415+
statements.push(
416416
createStatement(
417417
createCall(
418418
exportStarFunction,
@@ -426,7 +426,7 @@ namespace ts {
426426
}
427427
}
428428

429-
addNode(setters,
429+
setters.push(
430430
createFunctionExpression(
431431
/*asteriskToken*/ undefined,
432432
/*name*/ undefined,
@@ -563,7 +563,7 @@ namespace ts {
563563
function visitExportDeclaration(node: ExportDeclaration): VisitResult<Statement> {
564564
if (!node.moduleSpecifier) {
565565
const statements: Statement[] = [];
566-
addNodes(statements, map(node.exportClause.elements, visitExportSpecifier));
566+
addRange(statements, map(node.exportClause.elements, visitExportSpecifier));
567567
return statements;
568568
}
569569

@@ -612,7 +612,10 @@ namespace ts {
612612
const isExported = hasModifier(node, ModifierFlags.Export);
613613
const expressions: Expression[] = [];
614614
for (const variable of node.declarationList.declarations) {
615-
addNode(expressions, <Expression>transformVariable(variable, isExported));
615+
const visited = <Expression>transformVariable(variable, isExported);
616+
if (visited) {
617+
expressions.push(visited);
618+
}
616619
}
617620

618621
if (expressions.length) {
@@ -715,7 +718,7 @@ namespace ts {
715718
const statements: Statement[] = [];
716719

717720
// Rewrite the class declaration into an assignment of a class expression.
718-
addNode(statements,
721+
statements.push(
719722
createStatement(
720723
createAssignment(
721724
name,
@@ -738,7 +741,7 @@ namespace ts {
738741
recordExportName(name);
739742
}
740743

741-
addNode(statements, createDeclarationExport(node));
744+
statements.push(createDeclarationExport(node));
742745
}
743746

744747
return statements;
@@ -758,7 +761,10 @@ namespace ts {
758761
if (shouldHoistLoopInitializer(initializer)) {
759762
const expressions: Expression[] = [];
760763
for (const variable of (<VariableDeclarationList>initializer).declarations) {
761-
addNode(expressions, <Expression>transformVariable(variable, /*isExported*/ false));
764+
const visited = <Expression>transformVariable(variable, /*isExported*/ false);
765+
if (visited) {
766+
expressions.push(visited);
767+
}
762768
};
763769

764770
return createFor(
@@ -1209,7 +1215,7 @@ namespace ts {
12091215
);
12101216
}
12111217

1212-
addNode(statements,
1218+
statements.push(
12131219
createFunctionDeclaration(
12141220
/*decorators*/ undefined,
12151221
/*modifiers*/ undefined,

0 commit comments

Comments
 (0)