Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7243,17 +7243,17 @@ namespace ts {
forEachChild(sourceFile, visit);

if (lastNodeEntirelyBeforePosition) {
const lastChildOfLastEntireNodeBeforePosition = getLastChild(lastNodeEntirelyBeforePosition);
const lastChildOfLastEntireNodeBeforePosition = getLastDescendant(lastNodeEntirelyBeforePosition);
if (lastChildOfLastEntireNodeBeforePosition.pos > bestResult.pos) {
bestResult = lastChildOfLastEntireNodeBeforePosition;
}
}

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;
}
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}