Utilize getContainingFunction in services.

This commit is contained in:
Daniel Rosenwasser 2014-08-29 17:13:14 -07:00
parent 7e5802192e
commit ba396ed28f
2 changed files with 18 additions and 17 deletions

View file

@ -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 <SignatureDeclaration>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 <SignatureDeclaration>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.

View file

@ -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 = <FunctionDeclaration>getContainingFunction(returnStatement);
// If we didn't find a containing function with a block body, bail out.
if (!(isAnyFunction(node) && hasKind((<FunctionDeclaration>node).body, SyntaxKind.FunctionBlock))) {
if (!(isAnyFunction(func) && hasKind(func.body, SyntaxKind.FunctionBlock))) {
return undefined;
}
var keywords: Node[] = []
forEachReturnStatement(<Block>(<FunctionDeclaration>node).body, returnStmt => {
forEachReturnStatement(<Block>(<FunctionDeclaration>func).body, returnStmt => {
pushKeywordIf(keywords, returnStmt.getFirstToken(), SyntaxKind.ReturnKeyword);
});