Textually search for 'return' before diving into the tree when looking for return statements.

This commit is contained in:
Daniel Rosenwasser 2020-10-02 15:26:34 -07:00
parent 35111231f7
commit bac73a33fe

View file

@ -1218,10 +1218,20 @@ namespace ts {
// Warning: This has the same semantics as the forEach family of functions,
// in that traversal terminates in the event that 'visitor' supplies a truthy value.
export function forEachReturnStatement<T>(body: Block, visitor: (stmt: ReturnStatement) => T): T | undefined {
const sourceText = getSourceFileOfNode(body).text;
let currentPosition = sourceText.indexOf("return", body.pos);
return traverse(body);
function traverse(node: Node): T | undefined {
// Catch up to the current node.
while (0 <= currentPosition && currentPosition < node.pos) {
currentPosition = sourceText.indexOf("return", currentPosition + 1);
}
if (currentPosition < 0 || node.end <= currentPosition) {
// Nope, no candidates here.
return undefined;
}
switch (node.kind) {
case SyntaxKind.ReturnStatement:
return visitor(<ReturnStatement>node);