Skip to content

Commit 6d82a20

Browse files
author
Andy
authored
Combine getLastChild helpers (microsoft#22418)
1 parent b6b51a4 commit 6d82a20

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/compiler/parser.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -7243,17 +7243,17 @@ namespace ts {
72437243
forEachChild(sourceFile, visit);
72447244

72457245
if (lastNodeEntirelyBeforePosition) {
7246-
const lastChildOfLastEntireNodeBeforePosition = getLastChild(lastNodeEntirelyBeforePosition);
7246+
const lastChildOfLastEntireNodeBeforePosition = getLastDescendant(lastNodeEntirelyBeforePosition);
72477247
if (lastChildOfLastEntireNodeBeforePosition.pos > bestResult.pos) {
72487248
bestResult = lastChildOfLastEntireNodeBeforePosition;
72497249
}
72507250
}
72517251

72527252
return bestResult;
72537253

7254-
function getLastChild(node: Node): Node {
7254+
function getLastDescendant(node: Node): Node {
72557255
while (true) {
7256-
const lastChild = getLastChildWorker(node);
7256+
const lastChild = getLastChild(node);
72577257
if (lastChild) {
72587258
node = lastChild;
72597259
}
@@ -7263,16 +7263,6 @@ namespace ts {
72637263
}
72647264
}
72657265

7266-
function getLastChildWorker(node: Node): Node | undefined {
7267-
let last: Node;
7268-
forEachChild(node, child => {
7269-
if (nodeIsPresent(child)) {
7270-
last = child;
7271-
}
7272-
});
7273-
return last;
7274-
}
7275-
72767266
function visit(child: Node) {
72777267
if (nodeIsMissing(child)) {
72787268
// Missing nodes are effectively invisible to us. We never even consider them

src/compiler/utilities.ts

+18
Original file line numberDiff line numberDiff line change
@@ -3883,6 +3883,24 @@ namespace ts {
38833883
return isStringLiteral(moduleSpecifier) ? moduleSpecifier.text : getTextOfNode(moduleSpecifier);
38843884
}
38853885

3886+
export function getLastChild(node: Node): Node | undefined {
3887+
let lastChild: Node | undefined;
3888+
forEachChild(node,
3889+
child => {
3890+
if (nodeIsPresent(child)) lastChild = child;
3891+
},
3892+
children => {
3893+
// As an optimization, jump straight to the end of the list.
3894+
for (let i = children.length - 1; i >= 0; i--) {
3895+
if (nodeIsPresent(children[i])) {
3896+
lastChild = children[i];
3897+
break;
3898+
}
3899+
}
3900+
});
3901+
return lastChild;
3902+
}
3903+
38863904
/** Add a value to a set, and return true if it wasn't already present. */
38873905
export function addToSeen(seen: Map<true>, key: string | number): boolean {
38883906
key = String(key);

src/services/utilities.ts

-13
Original file line numberDiff line numberDiff line change
@@ -1499,17 +1499,4 @@ namespace ts {
14991499
function getFirstChild(node: Node): Node | undefined {
15001500
return node.forEachChild(child => child);
15011501
}
1502-
1503-
function getLastChild(node: Node): Node | undefined {
1504-
let lastChild: Node | undefined;
1505-
node.forEachChild(
1506-
child => { lastChild = child; },
1507-
children => {
1508-
// As an optimization, jump straight to the end of the list.
1509-
if (children.length) {
1510-
lastChild = last(children);
1511-
}
1512-
});
1513-
return lastChild;
1514-
}
15151502
}

0 commit comments

Comments
 (0)