Skip to content

Commit 044de3e

Browse files
More removal of getNodeId uses.
1 parent e83d144 commit 044de3e

File tree

11 files changed

+94
-90
lines changed

11 files changed

+94
-90
lines changed

src/compiler/checker.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,23 @@ namespace ts {
277277
this.flags = 0;
278278
}
279279

280+
const idCache = new Map<Node, number>();
280281
export function getNodeId(node: Node): number {
281-
if (!node.id) {
282-
node.id = nextNodeId;
282+
let result = idCache.get(node);
283+
if (!result) {
284+
result = nextNodeId;
285+
idCache.set(node, result);
283286
nextNodeId++;
284287
}
285-
return node.id;
288+
return result;
289+
}
290+
291+
export function getNodeIdOrDefault(node: Node): number {
292+
return idCache.get(node) || 0;
293+
}
294+
295+
export function setNodeIdForStrangeRedirect(node: Node, id: number): void {
296+
idCache.set(node, id);
286297
}
287298

288299
export function getSymbolId(symbol: Symbol): SymbolId {
@@ -974,7 +985,7 @@ namespace ts {
974985
const maximumSuggestionCount = 10;
975986
const mergedSymbols: Symbol[] = [];
976987
const symbolLinks: SymbolLinks[] = [];
977-
const nodeLinks: ESMap<Node, NodeLinks> = new Map();
988+
const nodeLinks = new Map<Node, NodeLinks>();
978989
const flowLoopCaches: ESMap<string, Type>[] = [];
979990
const flowLoopNodes: FlowNode[] = [];
980991
const flowLoopKeys: string[] = [];
@@ -40319,7 +40330,7 @@ namespace ts {
4031940330
const enclosingFile = getSourceFileOfNode(node);
4032040331
const links = getNodeLinks(enclosingFile);
4032140332
if (!(links.flags & NodeCheckFlags.TypeChecked)) {
40322-
links.deferredNodes = links.deferredNodes || new Set();
40333+
links.deferredNodes ||= new Set();
4032340334
links.deferredNodes.add(node);
4032440335
}
4032540336
}
@@ -41601,6 +41612,9 @@ namespace ts {
4160141612
}
4160241613

4160341614
function getNodeCheckFlags(node: Node): NodeCheckFlags {
41615+
// TODO: probably not meaningful, right?
41616+
// const nodeId = getNodeIdOrDefault(node);
41617+
// if (nodeId < 0 || nodeId >= nodeLinks.length) return 0;
4160441618
return nodeLinks.get(node)?.flags || 0;
4160541619
}
4160641620

src/compiler/factory/nodeFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5339,7 +5339,7 @@ namespace ts {
53395339
}
53405340

53415341
function flattenCommaElements(node: Expression): Expression | readonly Expression[] {
5342-
if (nodeIsSynthesized(node) && !isParseTreeNode(node) && !node.original && !node.emitNode && !node.id) {
5342+
if (nodeIsSynthesized(node) && !isParseTreeNode(node) && !node.original && !node.emitNode && !getNodeIdOrDefault(node)) {
53435343
if (isCommaListExpression(node)) {
53445344
return node.elements;
53455345
}

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,8 +2593,8 @@ namespace ts {
25932593
sourceFilesFoundSearchingNodeModules.set(path, currentNodeModulesDepth > 0);
25942594
Object.defineProperties(redirect, {
25952595
id: {
2596-
get(this: SourceFile) { return this.redirectInfo!.redirectTarget.id; },
2597-
set(this: SourceFile, value: SourceFile["id"]) { this.redirectInfo!.redirectTarget.id = value; },
2596+
get(this: SourceFile) { return getNodeIdOrDefault(this.redirectInfo!.redirectTarget); },
2597+
set(this: SourceFile, value: number) { setNodeIdForStrangeRedirect(this.redirectInfo!.redirectTarget, value); },
25982598
},
25992599
symbol: {
26002600
get(this: SourceFile) { return this.redirectInfo!.redirectTarget.symbol; },

src/compiler/transformers/classFields.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ namespace ts {
147147

148148
let enabledSubstitutions: ClassPropertySubstitutionFlags;
149149

150-
let classAliases: Identifier[];
150+
let classAliases: ESMap<Node, Identifier>;
151151

152152
/**
153153
* Tracks what computed name expressions originating from elided names must be inlined
@@ -162,7 +162,7 @@ namespace ts {
162162
let pendingStatements: Statement[] | undefined;
163163

164164
const classLexicalEnvironmentStack: (ClassLexicalEnvironment | undefined)[] = [];
165-
const classLexicalEnvironmentMap = new Map<number, ClassLexicalEnvironment>();
165+
const classLexicalEnvironmentMap = new WeakMap<Node, ClassLexicalEnvironment>();
166166
let currentClassLexicalEnvironment: ClassLexicalEnvironment | undefined;
167167
let currentComputedPropertyNameClassLexicalEnvironment: ClassLexicalEnvironment | undefined;
168168
let currentStaticPropertyDeclarationOrStaticBlock: PropertyDeclaration | ClassStaticBlockDeclaration | undefined;
@@ -738,7 +738,7 @@ namespace ts {
738738
function transformClassStaticBlockDeclaration(node: ClassStaticBlockDeclaration) {
739739
if (shouldTransformPrivateElementsOrClassStaticBlocks) {
740740
if (currentClassLexicalEnvironment) {
741-
classLexicalEnvironmentMap.set(getOriginalNodeId(node), currentClassLexicalEnvironment);
741+
classLexicalEnvironmentMap.set(getOriginalNode(node), currentClassLexicalEnvironment);
742742
}
743743

744744
startLexicalEnvironment();
@@ -1128,7 +1128,7 @@ namespace ts {
11281128
enableSubstitutionForClassAliases();
11291129
const alias = factory.cloneNode(temp) as GeneratedIdentifier;
11301130
alias.autoGenerateFlags &= ~GeneratedIdentifierFlags.ReservedInNestedScopes;
1131-
classAliases[getOriginalNodeId(node)] = alias;
1131+
classAliases.set(getOriginalNode(node), alias);
11321132
}
11331133

11341134
// To preserve the behavior of the old emitter, we explicitly indent
@@ -1375,7 +1375,7 @@ namespace ts {
13751375
// capture the lexical environment for the member
13761376
setOriginalNode(transformed, property);
13771377
addEmitFlags(transformed, EmitFlags.AdviseOnEmitNode);
1378-
classLexicalEnvironmentMap.set(getOriginalNodeId(transformed), currentClassLexicalEnvironment);
1378+
classLexicalEnvironmentMap.set(getOriginalNode(transformed), currentClassLexicalEnvironment);
13791379
}
13801380
currentStaticPropertyDeclarationOrStaticBlock = savedCurrentStaticPropertyDeclarationOrStaticBlock;
13811381
return transformed;
@@ -1453,7 +1453,7 @@ namespace ts {
14531453
context.enableSubstitution(SyntaxKind.Identifier);
14541454

14551455
// Keep track of class aliases.
1456-
classAliases = [];
1456+
classAliases = new Map<Node, Identifier>();
14571457
}
14581458
}
14591459

@@ -1516,18 +1516,16 @@ namespace ts {
15161516

15171517
function onEmitNode(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
15181518
const original = getOriginalNode(node);
1519-
if (original.id) {
1520-
const classLexicalEnvironment = classLexicalEnvironmentMap.get(original.id);
1521-
if (classLexicalEnvironment) {
1522-
const savedClassLexicalEnvironment = currentClassLexicalEnvironment;
1523-
const savedCurrentComputedPropertyNameClassLexicalEnvironment = currentComputedPropertyNameClassLexicalEnvironment;
1524-
currentClassLexicalEnvironment = classLexicalEnvironment;
1525-
currentComputedPropertyNameClassLexicalEnvironment = classLexicalEnvironment;
1526-
previousOnEmitNode(hint, node, emitCallback);
1527-
currentClassLexicalEnvironment = savedClassLexicalEnvironment;
1528-
currentComputedPropertyNameClassLexicalEnvironment = savedCurrentComputedPropertyNameClassLexicalEnvironment;
1529-
return;
1530-
}
1519+
const classLexicalEnvironment = classLexicalEnvironmentMap.get(original);
1520+
if (classLexicalEnvironment) {
1521+
const savedClassLexicalEnvironment = currentClassLexicalEnvironment;
1522+
const savedCurrentComputedPropertyNameClassLexicalEnvironment = currentComputedPropertyNameClassLexicalEnvironment;
1523+
currentClassLexicalEnvironment = classLexicalEnvironment;
1524+
currentComputedPropertyNameClassLexicalEnvironment = classLexicalEnvironment;
1525+
previousOnEmitNode(hint, node, emitCallback);
1526+
currentClassLexicalEnvironment = savedClassLexicalEnvironment;
1527+
currentComputedPropertyNameClassLexicalEnvironment = savedCurrentComputedPropertyNameClassLexicalEnvironment;
1528+
return;
15311529
}
15321530

15331531
switch (node.kind) {
@@ -1633,7 +1631,7 @@ namespace ts {
16331631
// constructor references in static property initializers.
16341632
const declaration = resolver.getReferencedValueDeclaration(node);
16351633
if (declaration) {
1636-
const classAlias = classAliases[declaration.id!]; // TODO: GH#18217
1634+
const classAlias = classAliases.get(declaration);
16371635
if (classAlias) {
16381636
const clone = factory.cloneNode(classAlias);
16391637
setSourceMapRange(clone, node);

src/compiler/transformers/es5.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ namespace ts {
1111

1212
// enable emit notification only if using --jsx preserve or react-native
1313
let previousOnEmitNode: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void;
14-
let noSubstitution: boolean[];
14+
let noSubstitution: Set<Node>;
1515
if (compilerOptions.jsx === JsxEmit.Preserve || compilerOptions.jsx === JsxEmit.ReactNative) {
1616
previousOnEmitNode = context.onEmitNode;
1717
context.onEmitNode = onEmitNode;
1818
context.enableEmitNotification(SyntaxKind.JsxOpeningElement);
1919
context.enableEmitNotification(SyntaxKind.JsxClosingElement);
2020
context.enableEmitNotification(SyntaxKind.JsxSelfClosingElement);
21-
noSubstitution = [];
21+
noSubstitution = new Set<Node>();
2222
}
2323

2424
const previousOnSubstituteNode = context.onSubstituteNode;
@@ -49,7 +49,7 @@ namespace ts {
4949
case SyntaxKind.JsxClosingElement:
5050
case SyntaxKind.JsxSelfClosingElement:
5151
const tagName = (node as JsxOpeningElement | JsxClosingElement | JsxSelfClosingElement).tagName;
52-
noSubstitution[getOriginalNodeId(tagName)] = true;
52+
noSubstitution.add(getOriginalNode(tagName));
5353
break;
5454
}
5555

@@ -63,7 +63,8 @@ namespace ts {
6363
* @param node The node to substitute.
6464
*/
6565
function onSubstituteNode(hint: EmitHint, node: Node) {
66-
if (node.id && noSubstitution && noSubstitution[node.id]) {
66+
// TODO: do we need to check for a Node ID here?
67+
if (getNodeIdOrDefault(node) && noSubstitution?.has(node)) {
6768
return previousOnSubstituteNode(hint, node);
6869
}
6970

src/compiler/transformers/module/module.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace ts {
4545

4646
let currentSourceFile: SourceFile; // The current file.
4747
let currentModuleInfo: ExternalModuleInfo; // The ExternalModuleInfo for the current file.
48-
const noSubstitution: boolean[] = []; // Set of nodes for which substitution rules should be ignored.
48+
const noSubstitution = new Set<Node>(); // Set of nodes for which substitution rules should be ignored.
4949
let needUMDDynamicImportHelper: boolean;
5050

5151
return chainBundle(context, transformSourceFile);
@@ -692,13 +692,13 @@ namespace ts {
692692
}
693693

694694
for (const exportName of exportedNames) {
695-
noSubstitution[getNodeId(expression)] = true;
695+
noSubstitution.add(expression);
696696
expression = createExportExpression(exportName, expression);
697697
setTextRange(expression, node);
698698
}
699699

700700
if (temp) {
701-
noSubstitution[getNodeId(expression)] = true;
701+
noSubstitution.add(expression);;
702702
expression = factory.createComma(expression, temp);
703703
setTextRange(expression, node);
704704
}
@@ -1794,7 +1794,7 @@ namespace ts {
17941794
*/
17951795
function onSubstituteNode(hint: EmitHint, node: Node) {
17961796
node = previousOnSubstituteNode(hint, node);
1797-
if (node.id && noSubstitution[node.id]) {
1797+
if (noSubstitution.has(node)) {
17981798
return node;
17991799
}
18001800

@@ -1852,7 +1852,7 @@ namespace ts {
18521852
function substituteCallExpression(node: CallExpression) {
18531853
if (isIdentifier(node.expression)) {
18541854
const expression = substituteExpressionIdentifier(node.expression);
1855-
noSubstitution[getNodeId(expression)] = true;
1855+
noSubstitution.add(expression);;
18561856
if (!isIdentifier(expression)) {
18571857
return addEmitFlags(
18581858
factory.updateCallExpression(node,
@@ -1871,7 +1871,7 @@ namespace ts {
18711871
function substituteTaggedTemplateExpression(node: TaggedTemplateExpression) {
18721872
if (isIdentifier(node.tag)) {
18731873
const tag = substituteExpressionIdentifier(node.tag);
1874-
noSubstitution[getNodeId(tag)] = true;
1874+
noSubstitution.add(tag);
18751875
if (!isIdentifier(tag)) {
18761876
return addEmitFlags(
18771877
factory.updateTaggedTemplateExpression(node,
@@ -1962,7 +1962,7 @@ namespace ts {
19621962
let expression: Expression = node;
19631963
for (const exportName of exportedNames) {
19641964
// Mark the node to prevent triggering this rule again.
1965-
noSubstitution[getNodeId(expression)] = true;
1965+
noSubstitution.add(expression);;
19661966
expression = createExportExpression(exportName, expression, /*location*/ node);
19671967
}
19681968

0 commit comments

Comments
 (0)