Combine getLastChild helpers (#22418)

This commit is contained in:
Andy 2018-03-22 09:32:05 -07:00 committed by GitHub
parent b6b51a4d38
commit 6d82a20109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 26 deletions

View file

@ -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

View file

@ -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);

View file

@ -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;
}
}