Skip to content

Commit e8f2225

Browse files
Don't cache child lists for tokens (#58233)
1 parent aedd1b1 commit e8f2225

File tree

8 files changed

+21
-19
lines changed

8 files changed

+21
-19
lines changed

src/compiler/factory/nodeChildren.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import { Node } from "../_namespaces/ts";
1+
import {
2+
emptyArray,
3+
isNodeKind,
4+
Node,
5+
} from "../_namespaces/ts";
26

3-
const nodeChildren = new WeakMap<Node, Node[] | undefined>();
7+
const nodeChildren = new WeakMap<Node, readonly Node[] | undefined>();
48

59
/** @internal */
6-
export function getNodeChildren(node: Node): Node[] | undefined {
10+
export function getNodeChildren(node: Node): readonly Node[] | undefined {
11+
if (!isNodeKind(node.kind)) return emptyArray;
12+
713
return nodeChildren.get(node);
814
}
915

1016
/** @internal */
11-
export function setNodeChildren(node: Node, children: Node[]): Node[] {
17+
export function setNodeChildren(node: Node, children: readonly Node[]): readonly Node[] {
1218
nodeChildren.set(node, children);
1319
return children;
1420
}

src/compiler/factory/nodeFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6209,7 +6209,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
62096209
}
62106210

62116211
// @api
6212-
function createSyntaxList(children: Node[]) {
6212+
function createSyntaxList(children: readonly Node[]) {
62136213
const node = createBaseNode<SyntaxList>(SyntaxKind.SyntaxList);
62146214
setNodeChildren(node, children);
62156215
return node;

src/compiler/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9006,7 +9006,7 @@ export interface NodeFactory {
90069006
// Synthetic Nodes
90079007
//
90089008
/** @internal */ createSyntheticExpression(type: Type, isSpread?: boolean, tupleNameSource?: ParameterDeclaration | NamedTupleMember): SyntheticExpression;
9009-
/** @internal */ createSyntaxList(children: Node[]): SyntaxList;
9009+
/** @internal */ createSyntaxList(children: readonly Node[]): SyntaxList;
90109010

90119011
//
90129012
// Transformation nodes

src/services/services.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class NodeObject<TKind extends SyntaxKind> implements Node {
438438
return this.getChildren(sourceFile)[index];
439439
}
440440

441-
public getChildren(sourceFile?: SourceFileLike): Node[] {
441+
public getChildren(sourceFile?: SourceFileLike): readonly Node[] {
442442
this.assertHasRealPosition("Node without a real position cannot be scanned and thus has no token nodes - use forEachChild and collect the result if that's fine");
443443
return getNodeChildren(this) ?? setNodeChildren(this, createChildren(this, sourceFile));
444444
}
@@ -473,11 +473,7 @@ class NodeObject<TKind extends SyntaxKind> implements Node {
473473
}
474474
}
475475

476-
function createChildren(node: Node, sourceFile: SourceFileLike | undefined): Node[] {
477-
if (!isNodeKind(node.kind)) {
478-
return emptyArray;
479-
}
480-
476+
function createChildren(node: Node, sourceFile: SourceFileLike | undefined): readonly Node[] {
481477
const children: Node[] = [];
482478

483479
if (isJSDocCommentContainingNode(node)) {

src/services/smartSelection.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function getSelectionChildren(node: Node): readonly Node[] {
279279
* Groups sibling nodes together into their own SyntaxList if they
280280
* a) are adjacent, AND b) match a predicate function.
281281
*/
282-
function groupChildren(children: Node[], groupOn: (child: Node) => boolean): Node[] {
282+
function groupChildren(children: readonly Node[], groupOn: (child: Node) => boolean): Node[] {
283283
const result: Node[] = [];
284284
let group: Node[] | undefined;
285285
for (const child of children) {
@@ -315,7 +315,7 @@ function groupChildren(children: Node[], groupOn: (child: Node) => boolean): Nod
315315
* @param separateTrailingSemicolon If the last token is a semicolon, it will be returned as a separate
316316
* child rather than be included in the right-hand group.
317317
*/
318-
function splitChildren(children: Node[], pivotOn: (child: Node) => boolean, separateTrailingSemicolon = true): Node[] {
318+
function splitChildren(children: readonly Node[], pivotOn: (child: Node) => boolean, separateTrailingSemicolon = true): readonly Node[] {
319319
if (children.length < 2) {
320320
return children;
321321
}
@@ -336,7 +336,7 @@ function splitChildren(children: Node[], pivotOn: (child: Node) => boolean, sepa
336336
return separateLastToken ? result.concat(lastToken) : result;
337337
}
338338

339-
function createSyntaxList(children: Node[]): SyntaxList {
339+
function createSyntaxList(children: readonly Node[]): SyntaxList {
340340
Debug.assertGreaterThanOrEqual(children.length, 1);
341341
return setTextRangePosEnd(parseNodeFactory.createSyntaxList(children), children[0].pos, last(children).end);
342342
}

src/services/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ declare module "../compiler/types" {
4949
getSourceFile(): SourceFile;
5050
getChildCount(sourceFile?: SourceFile): number;
5151
getChildAt(index: number, sourceFile?: SourceFile): Node;
52-
getChildren(sourceFile?: SourceFile): Node[];
52+
getChildren(sourceFile?: SourceFile): readonly Node[];
5353
/** @internal */
54-
getChildren(sourceFile?: SourceFileLike): Node[]; // eslint-disable-line @typescript-eslint/unified-signatures
54+
getChildren(sourceFile?: SourceFileLike): readonly Node[]; // eslint-disable-line @typescript-eslint/unified-signatures
5555
getStart(sourceFile?: SourceFile, includeJsDocComment?: boolean): number;
5656
/** @internal */
5757
getStart(sourceFile?: SourceFileLike, includeJsDocComment?: boolean): number; // eslint-disable-line @typescript-eslint/unified-signatures

src/services/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ function findRightmostToken(n: Node, sourceFile: SourceFileLike): Node | undefin
18301830
/**
18311831
* Finds the rightmost child to the left of `children[exclusiveStartPosition]` which is a non-all-whitespace token or has constituent tokens.
18321832
*/
1833-
function findRightmostChildNodeWithTokens(children: Node[], exclusiveStartPosition: number, sourceFile: SourceFileLike, parentKind: SyntaxKind): Node | undefined {
1833+
function findRightmostChildNodeWithTokens(children: readonly Node[], exclusiveStartPosition: number, sourceFile: SourceFileLike, parentKind: SyntaxKind): Node | undefined {
18341834
for (let i = exclusiveStartPosition - 1; i >= 0; i--) {
18351835
const child = children[i];
18361836

tests/baselines/reference/api/typescript.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4215,7 +4215,7 @@ declare namespace ts {
42154215
getSourceFile(): SourceFile;
42164216
getChildCount(sourceFile?: SourceFile): number;
42174217
getChildAt(index: number, sourceFile?: SourceFile): Node;
4218-
getChildren(sourceFile?: SourceFile): Node[];
4218+
getChildren(sourceFile?: SourceFile): readonly Node[];
42194219
getStart(sourceFile?: SourceFile, includeJsDocComment?: boolean): number;
42204220
getFullStart(): number;
42214221
getEnd(): number;

0 commit comments

Comments
 (0)