From f0a6fd3a727f8dc042f797fa16609734b14a3498 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2020 16:34:33 -0700 Subject: [PATCH] Guard against no parent nodes. --- src/compiler/utilities.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 2cb449f2db..103cec55f1 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1218,19 +1218,21 @@ 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); + 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 { - // 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; + 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: