Compare commits

...

2 commits

Author SHA1 Message Date
Daniel Rosenwasser f0a6fd3a72 Guard against no parent nodes. 2020-10-02 16:34:33 -07:00
Daniel Rosenwasser bac73a33fe Textually search for 'return' before diving into the tree when looking for return statements. 2020-10-02 15:26:34 -07:00

View file

@ -1218,10 +1218,22 @@ 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 as string | undefined;
let currentPosition = sourceText?.indexOf("return", body.pos) ?? body.pos;
return traverse(body);
function traverse(node: Node): T | undefined {
if (sourceText !== 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);