From bac73a33fea13225df58876bdeafb1450bbe87d2 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2020 15:26:34 -0700 Subject: [PATCH] Textually search for 'return' before diving into the tree when looking for return statements. --- src/compiler/utilities.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 95f81b987aa..2cb449f2db5 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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(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(node);