From ba396ed28fc6060d3891b604fa228e9ed0518470 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 29 Aug 2014 17:13:14 -0700 Subject: [PATCH] Utilize getContainingFunction in services. --- src/compiler/checker.ts | 22 +++++++++++----------- src/services/services.ts | 13 +++++++------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 575a9978f2f..0fc14f157d7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5774,17 +5774,6 @@ module ts { // TODO: Check that target label is valid } - function getContainingFunction(node: Node): SignatureDeclaration { - while (true) { - node = node.parent; - if (!node || node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression || - node.kind === SyntaxKind.ArrowFunction || node.kind === SyntaxKind.Method || node.kind === SyntaxKind.Constructor || - node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor) { - return node; - } - } - } - function checkReturnStatement(node: ReturnStatement) { if (node.expression && !(getNodeLinks(node.expression).flags & NodeCheckFlags.TypeChecked)) { var func = getContainingFunction(node); @@ -7124,6 +7113,17 @@ module ts { return checker; } + + export function getContainingFunction(node: Node): SignatureDeclaration { + while (true) { + node = node.parent; + if (!node || node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression || + node.kind === SyntaxKind.ArrowFunction || node.kind === SyntaxKind.Method || node.kind === SyntaxKind.Constructor || + node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor) { + return node; + } + } + } // 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. diff --git a/src/services/services.ts b/src/services/services.ts index 0d75dbb2b36..2ad05fdb726 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1313,6 +1313,10 @@ module ts { } function isAnyFunction(node: Node): boolean { + if (!node) { + return false; + } + switch (node.kind) { case SyntaxKind.FunctionExpression: case SyntaxKind.FunctionDeclaration: @@ -2267,18 +2271,15 @@ module ts { } function getReturnOccurrences(returnStatement: ReturnStatement): ReferenceEntry[]{ - var node: Node = returnStatement; - while (!isAnyFunction(node) && node.parent) { - node = node.parent; - } + var func = getContainingFunction(returnStatement); // If we didn't find a containing function with a block body, bail out. - if (!(isAnyFunction(node) && hasKind((node).body, SyntaxKind.FunctionBlock))) { + if (!(isAnyFunction(func) && hasKind(func.body, SyntaxKind.FunctionBlock))) { return undefined; } var keywords: Node[] = [] - forEachReturnStatement((node).body, returnStmt => { + forEachReturnStatement((func).body, returnStmt => { pushKeywordIf(keywords, returnStmt.getFirstToken(), SyntaxKind.ReturnKeyword); });