Add an 'isVariableLike' helper function.

This commit is contained in:
Cyrus Najmabadi 2015-03-06 18:45:45 -08:00
parent 865f67e738
commit a76eb69996
7 changed files with 37 additions and 19 deletions

View File

@ -528,7 +528,7 @@ module ts {
// var x;
// }
// 'var x' will be placed into the function locals and 'let x' - into the locals of the block
bindChildren(node, 0, /*isBlockScopeContainer*/ !isAnyFunction(node.parent));
bindChildren(node, 0, /*isBlockScopeContainer*/ !isFunctionLike(node.parent));
break;
case SyntaxKind.CatchClause:
case SyntaxKind.ForStatement:

View File

@ -5045,7 +5045,7 @@ module ts {
function isInsideFunction(node: Node, threshold: Node): boolean {
var current = node;
while (current && current !== threshold) {
if (isAnyFunction(current)) {
if (isFunctionLike(current)) {
return true;
}
current = current.parent;
@ -8550,7 +8550,7 @@ module ts {
// if block scoped variable is defined in the function\module\source file scope (because of variable hoisting)
var namesShareScope =
container &&
(container.kind === SyntaxKind.Block && isAnyFunction(container.parent) ||
(container.kind === SyntaxKind.Block && isFunctionLike(container.parent) ||
(container.kind === SyntaxKind.ModuleBlock && container.kind === SyntaxKind.ModuleDeclaration) ||
container.kind === SyntaxKind.SourceFile);
@ -9065,7 +9065,7 @@ module ts {
if (!checkGrammarStatementInAmbientContext(node)) {
var current = node.parent;
while (current) {
if (isAnyFunction(current)) {
if (isFunctionLike(current)) {
break;
}
if (current.kind === SyntaxKind.LabeledStatement && (<LabeledStatement>current).label.text === node.label.text) {
@ -11623,7 +11623,7 @@ module ts {
function checkGrammarBreakOrContinueStatement(node: BreakOrContinueStatement): boolean {
var current: Node = node;
while (current) {
if (isAnyFunction(current)) {
if (isFunctionLike(current)) {
return grammarErrorOnNode(node, Diagnostics.Jump_target_cannot_cross_function_boundary);
}
@ -11954,7 +11954,7 @@ module ts {
// Find containing block which is either Block, ModuleBlock, SourceFile
var links = getNodeLinks(node);
if (!links.hasReportedStatementInAmbientContext && isAnyFunction(node.parent)) {
if (!links.hasReportedStatementInAmbientContext && isFunctionLike(node.parent)) {
return getNodeLinks(node).hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts)
}

View File

@ -3888,7 +3888,7 @@ module ts {
function getEnclosingBlockScopeContainer(node: Node): Node {
var current = node;
while (current) {
if (isAnyFunction(current)) {
if (isFunctionLike(current)) {
return current;
}
switch (current.kind) {
@ -3903,7 +3903,7 @@ module ts {
case SyntaxKind.Block:
// function block is not considered block-scope container
// see comment in binder.ts: bind(...), case for SyntaxKind.Block
if (!isAnyFunction(current.parent)) {
if (!isFunctionLike(current.parent)) {
return current;
}
}

View File

@ -395,7 +395,25 @@ module ts {
}
}
export function isAnyFunction(node: Node): boolean {
/* @internal */
export function isVariableLike(node: Node): boolean {
if (node) {
switch (node.kind) {
case SyntaxKind.VariableDeclaration:
case SyntaxKind.Parameter:
case SyntaxKind.BindingElement:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShorthandPropertyAssignment:
case SyntaxKind.EnumMember:
return true;
}
}
return false;
}
export function isFunctionLike(node: Node): boolean {
if (node) {
switch (node.kind) {
case SyntaxKind.Constructor:
@ -422,7 +440,7 @@ module ts {
}
export function isFunctionBlock(node: Node) {
return node && node.kind === SyntaxKind.Block && isAnyFunction(node.parent);
return node && node.kind === SyntaxKind.Block && isFunctionLike(node.parent);
}
export function isObjectLiteralMethod(node: Node) {
@ -432,7 +450,7 @@ module ts {
export function getContainingFunction(node: Node): FunctionLikeDeclaration {
while (true) {
node = node.parent;
if (!node || isAnyFunction(node)) {
if (!node || isFunctionLike(node)) {
return <FunctionLikeDeclaration>node;
}
}
@ -1151,7 +1169,7 @@ module ts {
}
export function nodeStartsNewLexicalEnvironment(n: Node): boolean {
return isAnyFunction(n) || n.kind === SyntaxKind.ModuleDeclaration || n.kind === SyntaxKind.SourceFile;
return isFunctionLike(n) || n.kind === SyntaxKind.ModuleDeclaration || n.kind === SyntaxKind.SourceFile;
}
export function nodeIsSynthesized(node: Node): boolean {

View File

@ -259,7 +259,7 @@ module ts.BreakpointResolver {
}
// return type of function go to previous token
if (isAnyFunction(node.parent) && (<FunctionLikeDeclaration>node.parent).type === node) {
if (isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).type === node) {
return spanInPreviousNode(node);
}
@ -499,7 +499,7 @@ module ts.BreakpointResolver {
function spanInColonToken(node: Node): TextSpan {
// Is this : specifying return annotation of the function declaration
if (isAnyFunction(node.parent) || node.parent.kind === SyntaxKind.PropertyAssignment) {
if (isFunctionLike(node.parent) || node.parent.kind === SyntaxKind.PropertyAssignment) {
return spanInPreviousNode(node);
}

View File

@ -2014,7 +2014,7 @@ module ts {
function isNameOfFunctionDeclaration(node: Node): boolean {
return node.kind === SyntaxKind.Identifier &&
isAnyFunction(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
}
/** Returns true if node is a name of an object literal property, e.g. "a" in x = { "a": 1 } */
@ -3799,7 +3799,7 @@ module ts {
}
}
// Do not cross function boundaries.
else if (!isAnyFunction(node)) {
else if (!isFunctionLike(node)) {
forEachChild(node, aggregate);
}
};
@ -3931,7 +3931,7 @@ module ts {
statementAccumulator.push(<BreakOrContinueStatement>node);
}
// Do not cross function boundaries.
else if (!isAnyFunction(node)) {
else if (!isFunctionLike(node)) {
forEachChild(node, aggregate);
}
};
@ -3962,7 +3962,7 @@ module ts {
break;
default:
// Don't cross function boundaries.
if (isAnyFunction(node)) {
if (isFunctionLike(node)) {
return undefined;
}
break;

View File

@ -283,7 +283,7 @@ module ts {
return (<CallExpression>node).typeArguments;
}
if (isAnyFunction(node) || node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.InterfaceDeclaration) {
if (isFunctionLike(node) || node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.InterfaceDeclaration) {
return (<FunctionLikeDeclaration>node).typeParameters;
}