Utilize getContainingFunction in services.
This commit is contained in:
parent
7e5802192e
commit
ba396ed28f
|
@ -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);
|
||||
|
@ -7125,6 +7114,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.
|
||||
export function forEachReturnStatement<T>(body: Block, visitor: (stmt: ReturnStatement) => T): T {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue