8000 Switched to onEmitNode · nycdotnet/TypeScript@8b35af4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b35af4

Browse files
committed
Switched to onEmitNode
1 parent e66e51d commit 8b35af4

File tree

1 file changed

+61
-79
lines changed
  • src/compiler/transformers

1 file changed

+61
-79
lines changed

src/compiler/transformers/ts.ts

Lines changed: 61 additions & 79 deletions
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ namespace ts {
2828
const languageVersion = getEmitScriptTarget(compilerOptions);
2929

3030
// Save the previous transformation hooks.
31-
const previousOnBeforeEmitNode = context.onBeforeEmitNode;
32-
const previousOnAfterEmitNode = context.onAfterEmitNode;
31+
const previousOnEmitNode = context.onEmitNode;
3332
const previousExpressionSubstitution = context.expressionSubstitution;
3433

3534
// Set new transformation hooks.
36-
context.onBeforeEmitNode = onBeforeEmitNode;
37-
context.onAfterEmitNode = onAfterEmitNode;
35+
context.onEmitNode = onEmitNode;
3836
context.expressionSubstitution = substituteExpression;
3937

4038
// These variables contain state that changes as we descend into the tree.
@@ -64,19 +62,16 @@ namespace ts {
6462
let currentDecoratedClassAliases: Map<Identifier>;
6563

6664
/**
67-
* Keeps track of how deeply nested we are within any containing namespaces
68-
* when performing just-in-time substitution while printing an expression identifier.
69-
* If the nest level is greater than zero, then we are performing a substitution
70-
* inside of a namespace and we should perform the more costly checks to determine
71-
* whether the identifier points to an exported declaration.
65+
* Keeps track of whether we are within any containing namespaces when performing
66+
* just-in-time substitution while printing an expression identifier.
7267
*/
73-
let namespaceNestLevel: number;
68+
let isEnclosedInNamespace: boolean;
7469

7570
/**
76-
* This array keeps track of containers where `super` is valid, for use with
71+
* This keeps track of containers where `super` is valid, for use with
7772
* just-in-time substitution for `super` expressions inside of async methods.
7873
*/
79-
let superContainerStack: SuperContainer[];
74+
let currentSuperContainer: SuperContainer;
8075

8176
return transformSourceFile;
8277

@@ -2378,21 +2373,24 @@ namespace ts {
23782373
// x_1.y = ...;
23792374
// })(x || (x = {}));
23802375
statements.push(
2381-
setOriginalNode(
2382-
createStatement(
2383-
createCall(
2384-
createParen(
2385-
createFunctionExpression(
2386-
/*asteriskToken*/ undefined,
2387-
/*name*/ undefined,
2388-
[createParameter(currentNamespaceLocalName)],
2389-
transformModuleBody(node)
2390-
)
2391-
),
2392-
[moduleParam]
2393-
)
2376+
setNodeEmitFlags(
2377+
setOriginalNode(
2378+
createStatement(
2379+
createCall(
2380+
createParen(
2381+
createFunctionExpression(
2382+
/*asteriskToken*/ undefined,
2383+
/*name*/ undefined,
2384+
[createParameter(currentNamespaceLocalName)],
2385+
transformModuleBody(node)
2386+
)
2387+
),
2388+
[moduleParam]
2389+
)
2390+
),
2391+
node
23942392
),
2395-
node
2393+
NodeEmitFlags.AdviseOnEmitNode
23962394
)
23972395
);
23982396

@@ -2594,62 +2592,51 @@ namespace ts {
25942592
: getClassPrototype(node);
25952593
}
25962594

2597-
function onBeforeEmitNode(node: Node): void {
2598-
previousOnBeforeEmitNode(node);
2595+
function isClassWithDecorators(node: Node): node is ClassDeclaration {
2596+
return node.kind === SyntaxKind.ClassDeclaration && node.decorators !== undefined;
2597+
}
25992598

2599+
function isSuperContainer(node: Node): node is SuperContainer {
26002600
const kind = node.kind;
2601-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses
2602-
&& kind === SyntaxKind.ClassDeclaration
2603-
&& node.decorators) {
2604-
currentDecoratedClassAliases[getOriginalNodeId(node)] = decoratedClassAliases[getOriginalNodeId(node)];
2605-
}
2601+
return kind === SyntaxKind.ClassDeclaration
2602+
|| kind === SyntaxKind.Constructor
2603+
|| kind === SyntaxKind.MethodDeclaration
2604+
|| kind === SyntaxKind.GetAccessor
2605+
|| kind === SyntaxKind.SetAccessor;
2606+
}
26062607

2607-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
2608-
&& (kind === SyntaxKind.ClassDeclaration
2609-
|| kind === SyntaxKind.Constructor
2610-
|| kind === SyntaxKind.MethodDeclaration
2611-
|| kind === SyntaxKind.GetAccessor
2612-
|| kind === SyntaxKind.SetAccessor)) {
2608+
function isTransformedModuleDeclaration(node: Node): boolean {
2609+
return getOriginalNode(node).kind === SyntaxKind.ModuleDeclaration;
2610+
}
26132611

2614-
if (!superContainerStack) {
2615-
superContainerStack = [];
2616-
}
2612+
function onEmitNode(node: Node, emit: (node: Node) => void): void {
2613+
const savedIsEnclosedInNamespace = isEnclosedInNamespace;
2614+
const savedCurrentSuperContainer = currentSuperContainer;
26172615

2618-
superContainerStack.push(<SuperContainer>node);
2616+
// If we need support substitutions for aliases for decorated classes,
2617+
// we should enable it here.
2618+
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses && isClassWithDecorators(node)) {
2619+
currentDecoratedClassAliases[getOriginalNodeId(node)] = decoratedClassAliases[getOriginalNodeId(node)];
26192620
}
26202621

2621-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
2622-
&& kind === SyntaxKind.ModuleDeclaration) {
2623-
namespaceNestLevel++;
2622+
// If we need to support substitutions for `super` in an async method,
2623+
// we should track it here.
2624+
if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper && isSuperContainer(node)) {
2625+
currentSuperContainer = node;
26242626
}
2625-
}
26262627

2627-
function onAfterEmitNode(node: Node): void {
2628-
previousOnAfterEmitNode(node);
2629-
2630-
const kind = node.kind;
2631-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses
2632-
&& kind === SyntaxKind.ClassDeclaration
2633-
&& node.decorators) {
2634-
currentDecoratedClassAliases[getOriginalNodeId(node)] = undefined;
2628+
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports && isTransformedModuleDeclaration(node)) {
2629+
isEnclosedInNamespace = true;
26352630
}
26362631

2637-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
2638-
&& (kind === SyntaxKind.ClassDeclaration
2639-
|| kind === SyntaxKind.Constructor
2640-
|| kind === SyntaxKind.MethodDeclaration
2641-
|| kind === SyntaxKind.GetAccessor
2642-
|| kind === SyntaxKind.SetAccessor)) {
2632+
previousOnEmitNode(node, emit);
26432633

2644-
if (superContainerStack) {
2645-
superContainerStack.pop();
2646-
}
2634+
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses && isClassWithDecorators(node)) {
2635+
currentDecoratedClassAliases[getOriginalNodeId(node)] = undefined;
26472636
}
26482637

2649-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
2650-
&& kind === SyntaxKind.ModuleDeclaration) {
2651-
namespaceNestLevel--;
2652-
}
2638+
isEnclosedInNamespace = savedIsEnclosedInNamespace;
2639+
currentSuperContainer = savedCurrentSuperContainer;
26532640
}
26542641

26552642
function substituteExpression(node: Expression): Expression {
@@ -2660,7 +2647,7 @@ namespace ts {
26602647
return substituteExpressionIdentifier(<Identifier>node);
26612648
}
26622649

2663-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports) {
2650+
if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper) {
26642651
switch (node.kind) {
26652652
case SyntaxKind.CallExpression:
26662653
return substituteCallExpression(<CallExpression>node);
@@ -2691,8 +2678,7 @@ namespace ts {
26912678
}
26922679
}
26932680

2694-
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
2695-
&& namespaceNestLevel > 0) {
2681+
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports && isEnclosedInNamespace) {
26962682
// If we are nested within a namespace declaration, we may need to qualifiy
26972683
// an identifier that is exported from a merged namespace.
26982684
const original = getOriginalNode(node);
@@ -2758,8 +2744,8 @@ namespace ts {
27582744
}
27592745

27602746
function enableExpressionSubstitutionForAsyncMethodsWithSuper() {
2761-
if ((enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports) === 0) {
2762-
enabledSubstitutions |= TypeScriptSubstitutionFlags.NamespaceExports;
2747+
if ((enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper) === 0) {
2748+
enabledSubstitutions |= TypeScriptSubstitutionFlags.AsyncMethodsWithSuper;
27632749

27642750
// We need to enable substitutions for call, property access, and element access
27652751
// if we need to rewrite super calls.
@@ -2800,9 +2786,6 @@ namespace ts {
28002786

28012787
// We need to be notified when entering and exiting namespaces.
28022788
context.enableEmitNotification(SyntaxKind.ModuleDeclaration);
2803-
2804-
// Keep track of namespace nesting depth
2805-
namespaceNestLevel = 0;
28062789
}
28072790
}
28082791

@@ -2827,9 +2810,8 @@ namespace ts {
28272810
}
28282811

28292812
function getSuperContainerAsyncMethodFlags() {
2830-
const container = lastOrUndefined(superContainerStack);
2831-
return container !== undefined
2832-
&& resolver.getNodeCheckFlags(getOriginalNode(container)) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding);
2813+
return currentSuperContainer !== undefined
2814+
&& resolver.getNodeCheckFlags(getOriginalNode(currentSuperContainer)) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding);
28332815
}
28342816
}
28352817
}

0 commit comments

Comments
 (0)
0