Skip to content

Commit a76eb69

Browse files
Add an 'isVariableLike' helper function.
1 parent 865f67e commit a76eb69

7 files changed

Lines changed: 37 additions & 19 deletions

File tree

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ module ts {
528528
// var x;
529529
// }
530530
// 'var x' will be placed into the function locals and 'let x' - into the locals of the block
531-
bindChildren(node, 0, /*isBlockScopeContainer*/ !isAnyFunction(node.parent));
531+
bindChildren(node, 0, /*isBlockScopeContainer*/ !isFunctionLike(node.parent));
532532
break;
533533
case SyntaxKind.CatchClause:
534534
case SyntaxKind.ForStatement:

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5045,7 +5045,7 @@ module ts {
50455045
function isInsideFunction(node: Node, threshold: Node): boolean {
50465046
var current = node;
50475047
while (current && current !== threshold) {
5048-
if (isAnyFunction(current)) {
5048+
if (isFunctionLike(current)) {
50495049
return true;
50505050
}
50515051
current = current.parent;
@@ -8550,7 +8550,7 @@ module ts {
85508550
// if block scoped variable is defined in the function\module\source file scope (because of variable hoisting)
85518551
var namesShareScope =
85528552
container &&
8553-
(container.kind === SyntaxKind.Block && isAnyFunction(container.parent) ||
8553+
(container.kind === SyntaxKind.Block && isFunctionLike(container.parent) ||
85548554
(container.kind === SyntaxKind.ModuleBlock && container.kind === SyntaxKind.ModuleDeclaration) ||
85558555
container.kind === SyntaxKind.SourceFile);
85568556

@@ -9065,7 +9065,7 @@ module ts {
90659065
if (!checkGrammarStatementInAmbientContext(node)) {
90669066
var current = node.parent;
90679067
while (current) {
9068-
if (isAnyFunction(current)) {
9068+
if (isFunctionLike(current)) {
90699069
break;
90709070
}
90719071
if (current.kind === SyntaxKind.LabeledStatement && (<LabeledStatement>current).label.text === node.label.text) {
@@ -11623,7 +11623,7 @@ module ts {
1162311623
function checkGrammarBreakOrContinueStatement(node: BreakOrContinueStatement): boolean {
1162411624
var current: Node = node;
1162511625
while (current) {
11626-
if (isAnyFunction(current)) {
11626+
if (isFunctionLike(current)) {
1162711627
return grammarErrorOnNode(node, Diagnostics.Jump_target_cannot_cross_function_boundary);
1162811628
}
1162911629

@@ -11954,7 +11954,7 @@ module ts {
1195411954

1195511955
// Find containing block which is either Block, ModuleBlock, SourceFile
1195611956
var links = getNodeLinks(node);
11957-
if (!links.hasReportedStatementInAmbientContext && isAnyFunction(node.parent)) {
11957+
if (!links.hasReportedStatementInAmbientContext && isFunctionLike(node.parent)) {
1195811958
return getNodeLinks(node).hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts)
1195911959
}
1196011960

src/compiler/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,7 +3888,7 @@ module ts {
38883888
function getEnclosingBlockScopeContainer(node: Node): Node {
38893889
var current = node;
38903890
while (current) {
3891-
if (isAnyFunction(current)) {
3891+
if (isFunctionLike(current)) {
38923892
return current;
38933893
}
38943894
switch (current.kind) {
@@ -3903,7 +3903,7 @@ module ts {
39033903
case SyntaxKind.Block:
39043904
// function block is not considered block-scope container
39053905
// see comment in binder.ts: bind(...), case for SyntaxKind.Block
3906-
if (!isAnyFunction(current.parent)) {
3906+
if (!isFunctionLike(current.parent)) {
39073907
return current;
39083908
}
39093909
}

src/compiler/utilities.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,25 @@ module ts {
395395
}
396396
}
397397

398-
export function isAnyFunction(node: Node): boolean {
398+
/* @internal */
399+
export function isVariableLike(node: Node): boolean {
400+
if (node) {
401+
switch (node.kind) {
402+
case SyntaxKind.VariableDeclaration:
403+
case SyntaxKind.Parameter:
404+
case SyntaxKind.BindingElement:
405+
case SyntaxKind.PropertyDeclaration:
406+
case SyntaxKind.PropertyAssignment:
407+
case SyntaxKind.ShorthandPropertyAssignment:
408+
case SyntaxKind.EnumMember:
409+
return true;
410+
}
411+
}
412+
413+
return false;
414+
}
415+
416+
export function isFunctionLike(node: Node): boolean {
399417
if (node) {
400418
switch (node.kind) {
401419
case SyntaxKind.Constructor:
@@ -422,7 +440,7 @@ module ts {
422440
}
423441

424442
export function isFunctionBlock(node: Node) {
425-
return node && node.kind === SyntaxKind.Block && isAnyFunction(node.parent);
443+
return node && node.kind === SyntaxKind.Block && isFunctionLike(node.parent);
426444
}
427445

428446
export function isObjectLiteralMethod(node: Node) {
@@ -432,7 +450,7 @@ module ts {
432450
export function getContainingFunction(node: Node): FunctionLikeDeclaration {
433451
while (true) {
434452
node = node.parent;
435-
if (!node || isAnyFunction(node)) {
453+
if (!node || isFunctionLike(node)) {
436454
return <FunctionLikeDeclaration>node;
437455
}
438456
}
@@ -1151,7 +1169,7 @@ module ts {
11511169
}
11521170

11531171
export function nodeStartsNewLexicalEnvironment(n: Node): boolean {
1154-
return isAnyFunction(n) || n.kind === SyntaxKind.ModuleDeclaration || n.kind === SyntaxKind.SourceFile;
1172+
return isFunctionLike(n) || n.kind === SyntaxKind.ModuleDeclaration || n.kind === SyntaxKind.SourceFile;
11551173
}
11561174

11571175
export function nodeIsSynthesized(node: Node): boolean {

src/services/breakpoints.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ module ts.BreakpointResolver {
259259
}
260260

261261
// return type of function go to previous token
262-
if (isAnyFunction(node.parent) && (<FunctionLikeDeclaration>node.parent).type === node) {
262+
if (isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).type === node) {
263263
return spanInPreviousNode(node);
264264
}
265265

@@ -499,7 +499,7 @@ module ts.BreakpointResolver {
499499

500500
function spanInColonToken(node: Node): TextSpan {
501501
// Is this : specifying return annotation of the function declaration
502-
if (isAnyFunction(node.parent) || node.parent.kind === SyntaxKind.PropertyAssignment) {
502+
if (isFunctionLike(node.parent) || node.parent.kind === SyntaxKind.PropertyAssignment) {
503503
return spanInPreviousNode(node);
504504
}
505505

src/services/services.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ module ts {
20142014

20152015
function isNameOfFunctionDeclaration(node: Node): boolean {
20162016
return node.kind === SyntaxKind.Identifier &&
2017-
isAnyFunction(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
2017+
isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
20182018
}
20192019

20202020
/** 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 {
37993799
}
38003800
}
38013801
// Do not cross function boundaries.
3802-
else if (!isAnyFunction(node)) {
3802+
else if (!isFunctionLike(node)) {
38033803
forEachChild(node, aggregate);
38043804
}
38053805
};
@@ -3931,7 +3931,7 @@ module ts {
39313931
statementAccumulator.push(<BreakOrContinueStatement>node);
39323932
}
39333933
// Do not cross function boundaries.
3934-
else if (!isAnyFunction(node)) {
3934+
else if (!isFunctionLike(node)) {
39353935
forEachChild(node, aggregate);
39363936
}
39373937
};
@@ -3962,7 +3962,7 @@ module ts {
39623962
break;
39633963
default:
39643964
// Don't cross function boundaries.
3965-
if (isAnyFunction(node)) {
3965+
if (isFunctionLike(node)) {
39663966
return undefined;
39673967
}
39683968
break;

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ module ts {
283283
return (<CallExpression>node).typeArguments;
284284
}
285285

286-
if (isAnyFunction(node) || node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.InterfaceDeclaration) {
286+
if (isFunctionLike(node) || node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.InterfaceDeclaration) {
287287
return (<FunctionLikeDeclaration>node).typeParameters;
288288
}
289289

0 commit comments

Comments
 (0)