mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-11 16:38:46 -05:00
Combine getLastChild helpers (#22418)
This commit is contained in:
@@ -7243,7 +7243,7 @@ namespace ts {
|
||||
forEachChild(sourceFile, visit);
|
||||
|
||||
if (lastNodeEntirelyBeforePosition) {
|
||||
const lastChildOfLastEntireNodeBeforePosition = getLastChild(lastNodeEntirelyBeforePosition);
|
||||
const lastChildOfLastEntireNodeBeforePosition = getLastDescendant(lastNodeEntirelyBeforePosition);
|
||||
if (lastChildOfLastEntireNodeBeforePosition.pos > bestResult.pos) {
|
||||
bestResult = lastChildOfLastEntireNodeBeforePosition;
|
||||
}
|
||||
@@ -7251,9 +7251,9 @@ namespace ts {
|
||||
|
||||
return bestResult;
|
||||
|
||||
function getLastChild(node: Node): Node {
|
||||
function getLastDescendant(node: Node): Node {
|
||||
while (true) {
|
||||
const lastChild = getLastChildWorker(node);
|
||||
const lastChild = getLastChild(node);
|
||||
if (lastChild) {
|
||||
node = lastChild;
|
||||
}
|
||||
@@ -7263,16 +7263,6 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function getLastChildWorker(node: Node): Node | undefined {
|
||||
let last: Node;
|
||||
forEachChild(node, child => {
|
||||
if (nodeIsPresent(child)) {
|
||||
last = child;
|
||||
}
|
||||
});
|
||||
return last;
|
||||
}
|
||||
|
||||
function visit(child: Node) {
|
||||
if (nodeIsMissing(child)) {
|
||||
// Missing nodes are effectively invisible to us. We never even consider them
|
||||
|
||||
@@ -3883,6 +3883,24 @@ namespace ts {
|
||||
return isStringLiteral(moduleSpecifier) ? moduleSpecifier.text : getTextOfNode(moduleSpecifier);
|
||||
}
|
||||
|
||||
export function getLastChild(node: Node): Node | undefined {
|
||||
let lastChild: Node | undefined;
|
||||
forEachChild(node,
|
||||
child => {
|
||||
if (nodeIsPresent(child)) lastChild = child;
|
||||
},
|
||||
children => {
|
||||
// As an optimization, jump straight to the end of the list.
|
||||
for (let i = children.length - 1; i >= 0; i--) {
|
||||
if (nodeIsPresent(children[i])) {
|
||||
lastChild = children[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
return lastChild;
|
||||
}
|
||||
|
||||
/** Add a value to a set, and return true if it wasn't already present. */
|
||||
export function addToSeen(seen: Map<true>, key: string | number): boolean {
|
||||
key = String(key);
|
||||
|
||||
@@ -1499,17 +1499,4 @@ namespace ts {
|
||||
function getFirstChild(node: Node): Node | undefined {
|
||||
return node.forEachChild(child => child);
|
||||
}
|
||||
|
||||
function getLastChild(node: Node): Node | undefined {
|
||||
let lastChild: Node | undefined;
|
||||
node.forEachChild(
|
||||
child => { lastChild = child; },
|
||||
children => {
|
||||
// As an optimization, jump straight to the end of the list.
|
||||
if (children.length) {
|
||||
lastChild = last(children);
|
||||
}
|
||||
});
|
||||
return lastChild;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user