Skip to content

Commit 9b7270f

Browse files
committed
Minor cleanup of ts transformations.
1 parent 4b9555d commit 9b7270f

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

  • src/compiler/transformers

src/compiler/transformers/ts.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace ts {
1818
export function transformTypeScript(context: TransformationContext) {
1919
const {
2020
setNodeEmitFlags,
21+
getNodeEmitFlags,
2122
startLexicalEnvironment,
2223
endLexicalEnvironment,
2324
hoistVariableDeclaration,
@@ -82,9 +83,9 @@ namespace ts {
8283
*/
8384
function transformSourceFile(node: SourceFile) {
8485
currentSourceFile = node;
85-
node = visitEachChild(node, visitor, context);
86-
setNodeEmitFlags(node, NodeEmitFlags.EmitEmitHelpers);
87-
return node;
86+
const visited = visitEachChild(node, visitor, context);
87+
setNodeEmitFlags(visited, NodeEmitFlags.EmitEmitHelpers | getNodeEmitFlags(node));
88+
return visited;
8889
}
8990

9091
/**
@@ -94,7 +95,6 @@ namespace ts {
9495
*/
9596
function visitWithStack(node: Node, visitor: (node: Node) => VisitResult<Node>): VisitResult<Node> {
9697
// Save state
97-
const savedCurrentNamespace = currentNamespace;
9898
const savedCurrentScope = currentScope;
9999
const savedCurrentParent = currentParent;
100100
const savedCurrentNode = currentNode;
@@ -105,7 +105,6 @@ namespace ts {
105105
const visited = visitor(node);
106106

107107
// Restore state
108-
currentNamespace = savedCurrentNamespace;
109108
currentScope = savedCurrentScope;
110109
currentParent = savedCurrentParent;
111110
currentNode = savedCurrentNode;
@@ -411,6 +410,7 @@ namespace ts {
411410
function visitClassDeclaration(node: ClassDeclaration): VisitResult<Statement> {
412411
const staticProperties = getInitializedProperties(node, /*isStatic*/ true);
413412
const hasExtendsClause = getClassExtendsHeritageClauseElement(node) !== undefined;
413+
let decoratedClassAlias: Identifier;
414414

415415
// emit name if
416416
// - node has a name
@@ -421,7 +421,6 @@ namespace ts {
421421
name = getGeneratedNameForNode(node);
422422
}
423423

424-
let decoratedClassAlias: Identifier;
425424
const statements: Statement[] = [];
426425
if (!node.decorators) {
427426
// ${modifiers} class ${name} ${heritageClauses} {
@@ -449,25 +448,25 @@ namespace ts {
449448
// From ES6 specification:
450449
// HasLexicalDeclaration (N) : Determines if the argument identifier has a binding in this environment record that was created using
451450
// a lexical declaration such as a LexicalDeclaration or a ClassDeclaration.
452-
addNodes(statements, generateInitializedPropertyStatements(node, staticProperties, name));
451+
addInitializedPropertyStatements(statements, node, staticProperties, name);
453452

454453
// Write any decorators of the node.
455-
addNodes(statements, generateClassElementDecorationStatements(node, /*isStatic*/ false));
456-
addNodes(statements, generateClassElementDecorationStatements(node, /*isStatic*/ true));
457-
addNode(statements, generateConstructorDecorationStatement(node, decoratedClassAlias));
454+
addClassElementDecorationStatements(statements, node, /*isStatic*/ false);
455+
addClassElementDecorationStatements(statements, node, /*isStatic*/ true);
456+
addConstructorDecorationStatement(statements, node, decoratedClassAlias);
458457

459458
// If the class is exported as part of a TypeScript namespace, emit the namespace export.
460459
// Otherwise, if the class was exported at the top level and was decorated, emit an export
461460
// declaration or export default for the class.
462461
if (isNamespaceExport(node)) {
463-
addNode(statements, createNamespaceExport(name, name));
462+
addNamespaceExport(statements, name, name);
464463
}
465464
else if (node.decorators) {
466465
if (isDefaultExternalModuleExport(node)) {
467-
addNode(statements, createExportDefault(name));
466+
statements.push(createExportDefault(name));
468467
}
469468
else if (isNamedExternalModuleExport(node)) {
470-
addNode(statements, createExternalModuleExport(name));
469+
statements.push(createExternalModuleExport(name));
471470
}
472471
}
473472

@@ -806,7 +805,7 @@ namespace ts {
806805
// }
807806
//
808807
const properties = getInitializedProperties(node, /*isStatic*/ false);
809-
addNodes(statements, generateInitializedPropertyStatements(node, properties, createThis()));
808+
addInitializedPropertyStatements(statements, node, properties, createThis());
810809

811810
if (constructor) {
812811
// The class already had a constructor, so we should add the existing statements, skipping the initial super call.
@@ -928,8 +927,7 @@ namespace ts {
928927
* @param properties An array of property declarations to transform.
929928
* @param receiver The receiver on which each property should be assigned.
930929
*/
931-
function generateInitializedPropertyStatements(node: ClassExpression | ClassDeclaration, properties: PropertyDeclaration[], receiver: LeftHandSideExpression) {
932-
const statements: Statement[] = [];
930+
function addInitializedPropertyStatements(statements: Statement[], node: ClassExpression | ClassDeclaration, properties: PropertyDeclaration[], receiver: LeftHandSideExpression) {
933931
for (const property of properties) {
934932
statements.push(
935933
createStatement(
@@ -938,8 +936,6 @@ namespace ts {
938936
)
939937
);
940938
}
941-
942-
return statements;
943939
}
944940

945941
/**
@@ -1178,8 +1174,8 @@ namespace ts {
11781174
* @param isStatic A value indicating whether to generate statements for static or
11791175
* instance members.
11801176
*/
1181-
function generateClassElementDecorationStatements(node: ClassDeclaration, isStatic: boolean) {
1182-
return map(generateClassElementDecorationExpressions(node, isStatic), expressionToStatement);
1177+
function addClassElementDecorationStatements(statements: Statement[], node: ClassDeclaration, isStatic: boolean) {
1178+
addRange(statements, map(generateClassElementDecorationExpressions(node, isStatic), expressionToStatement));
11831179
}
11841180

11851181
/**
@@ -1277,9 +1273,11 @@ namespace ts {
12771273
*
12781274
* @param node The class node.
12791275
*/
1280-
function generateConstructorDecorationStatement(node: ClassDeclaration, decoratedClassAlias: Identifier) {
1276+
function addConstructorDecorationStatement(statements: Statement[], node: ClassDeclaration, decoratedClassAlias: Identifier) {
12811277
const expression = generateConstructorDecorationExpression(node, decoratedClassAlias);
1282-
return expression ? createStatement(expression) : undefined;
1278+
if (expression) {
1279+
statements.push(createStatement(expression));
1280+
}
12831281
}
12841282

12851283
/**
@@ -2549,6 +2547,10 @@ namespace ts {
25492547
return createStatement(expression, /*location*/ undefined);
25502548
}
25512549

2550+
function addNamespaceExport(statements: Statement[], exportName: Identifier, exportValue: Expression, location?: TextRange) {
2551+
statements.push(createNamespaceExport(exportName, exportValue, location));
2552+
}
2553+
25522554
function createNamespaceExport(exportName: Identifier, exportValue: Expression, location?: TextRange) {
25532555
return createStatement(
25542556
createAssignment(

0 commit comments

Comments
 (0)