From 6d82a2010997f6706c60b089e028d2552cbd5745 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 22 Mar 2018 09:32:05 -0700 Subject: [PATCH] Combine getLastChild helpers (#22418) --- src/compiler/parser.ts | 16 +++------------- src/compiler/utilities.ts | 18 ++++++++++++++++++ src/services/utilities.ts | 13 ------------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index b22906540aa..12177f8545b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -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 diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 69249952166..236177ea6ff 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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, key: string | number): boolean { key = String(key); diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 09887d9dffa..2772b8920e7 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -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; - } }