Skip to content

Commit 2748b3b

Browse files
author
Andy
authored
Add isForInOrOfStatement utility (#16455)
1 parent 44d5c44 commit 2748b3b

File tree

7 files changed

+21
-24
lines changed

7 files changed

+21
-24
lines changed

src/compiler/binder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ namespace ts {
612612
break;
613613
case SyntaxKind.ForInStatement:
614614
case SyntaxKind.ForOfStatement:
615-
bindForInOrForOfStatement(<ForInStatement | ForOfStatement>node);
615+
bindForInOrForOfStatement(<ForInOrOfStatement>node);
616616
break;
617617
case SyntaxKind.IfStatement:
618618
bindIfStatement(<IfStatement>node);
@@ -950,7 +950,7 @@ namespace ts {
950950
currentFlow = finishFlowLabel(postLoopLabel);
951951
}
952952

953-
function bindForInOrForOfStatement(node: ForInStatement | ForOfStatement): void {
953+
function bindForInOrForOfStatement(node: ForInOrOfStatement): void {
954954
const preLoopLabel = createLoopLabel();
955955
const postLoopLabel = createBranchLabel();
956956
addAntecedent(preLoopLabel, currentFlow);
@@ -1328,7 +1328,7 @@ namespace ts {
13281328

13291329
function bindVariableDeclarationFlow(node: VariableDeclaration) {
13301330
bindEachChild(node);
1331-
if (node.initializer || node.parent.parent.kind === SyntaxKind.ForInStatement || node.parent.parent.kind === SyntaxKind.ForOfStatement) {
1331+
if (node.initializer || isForInOrOfStatement(node.parent.parent)) {
13321332
bindInitializedVariableFlow(node);
13331333
}
13341334
}

src/compiler/checker.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -804,16 +804,8 @@ namespace ts {
804804
break;
805805
}
806806

807-
switch (declaration.parent.parent.kind) {
808-
case SyntaxKind.ForInStatement:
809-
case SyntaxKind.ForOfStatement:
810-
// ForIn/ForOf case - use site should not be used in expression part
811-
if (isSameScopeDescendentOf(usage, (<ForInStatement | ForOfStatement>declaration.parent.parent).expression, container)) {
812-
return true;
813-
}
814-
}
815-
816-
return false;
807+
// ForIn/ForOf case - use site should not be used in expression part
808+
return isForInOrOfStatement(declaration.parent.parent) && isSameScopeDescendentOf(usage, declaration.parent.parent.expression, container);
817809
}
818810

819811
function isUsedInFunctionOrInstanceProperty(usage: Node, declaration: Node, container?: Node): boolean {
@@ -19588,9 +19580,7 @@ namespace ts {
1958819580
function errorUnusedLocal(node: Node, name: string) {
1958919581
if (isIdentifierThatStartsWithUnderScore(node)) {
1959019582
const declaration = getRootDeclaration(node.parent);
19591-
if (declaration.kind === SyntaxKind.VariableDeclaration &&
19592-
(declaration.parent.parent.kind === SyntaxKind.ForInStatement ||
19593-
declaration.parent.parent.kind === SyntaxKind.ForOfStatement)) {
19583+
if (declaration.kind === SyntaxKind.VariableDeclaration && isForInOrOfStatement(declaration.parent.parent)) {
1959419584
return;
1959519585
}
1959619586
}
@@ -20283,7 +20273,7 @@ namespace ts {
2028320273
}
2028420274
}
2028520275

20286-
function checkForInOrForOfVariableDeclaration(iterationStatement: ForInStatement | ForOfStatement): void {
20276+
function checkForInOrForOfVariableDeclaration(iterationStatement: ForInOrOfStatement): void {
2028720277
const variableDeclarationList = <VariableDeclarationList>iterationStatement.initializer;
2028820278
// checkGrammarForInOrForOfStatement will check that there is exactly one declaration.
2028920279
if (variableDeclarationList.declarations.length >= 1) {
@@ -24263,7 +24253,7 @@ namespace ts {
2426324253
}
2426424254
}
2426524255

24266-
function checkGrammarForInOrForOfStatement(forInOrOfStatement: ForInStatement | ForOfStatement): boolean {
24256+
function checkGrammarForInOrForOfStatement(forInOrOfStatement: ForInOrOfStatement): boolean {
2426724257
if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) {
2426824258
return true;
2426924259
}

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,8 @@ namespace ts {
17081708
incrementor?: Expression;
17091709
}
17101710

1711+
export type ForInOrOfStatement = ForInStatement | ForOfStatement;
1712+
17111713
export interface ForInStatement extends IterationStatement {
17121714
kind: SyntaxKind.ForInStatement;
17131715
initializer: ForInitializer;

src/compiler/utilities.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ namespace ts {
16271627
return unaryOperator === SyntaxKind.PlusPlusToken || unaryOperator === SyntaxKind.MinusMinusToken ? AssignmentKind.Compound : AssignmentKind.None;
16281628
case SyntaxKind.ForInStatement:
16291629
case SyntaxKind.ForOfStatement:
1630-
return (<ForInStatement | ForOfStatement>parent).initializer === node ? AssignmentKind.Definite : AssignmentKind.None;
1630+
return (<ForInOrOfStatement>parent).initializer === node ? AssignmentKind.Definite : AssignmentKind.None;
16311631
case SyntaxKind.ParenthesizedExpression:
16321632
case SyntaxKind.ArrayLiteralExpression:
16331633
case SyntaxKind.SpreadElement:
@@ -5152,6 +5152,11 @@ namespace ts {
51525152
return false;
51535153
}
51545154

5155+
/* @internal */
5156+
export function isForInOrOfStatement(node: Node): node is ForInOrOfStatement {
5157+
return node.kind === SyntaxKind.ForInStatement || node.kind === SyntaxKind.ForOfStatement;
5158+
}
5159+
51555160
// Element
51565161

51575162
/* @internal */

src/compiler/visitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,9 +1198,9 @@ namespace ts {
11981198

11991199
case SyntaxKind.ForInStatement:
12001200
case SyntaxKind.ForOfStatement:
1201-
result = reduceNode((<ForInStatement | ForOfStatement>node).initializer, cbNode, result);
1202-
result = reduceNode((<ForInStatement | ForOfStatement>node).expression, cbNode, result);
1203-
result = reduceNode((<ForInStatement | ForOfStatement>node).statement, cbNode, result);
1201+
result = reduceNode((<ForInOrOfStatement>node).initializer, cbNode, result);
1202+
result = reduceNode((<ForInOrOfStatement>node).expression, cbNode, result);
1203+
result = reduceNode((<ForInOrOfStatement>node).statement, cbNode, result);
12041204
break;
12051205

12061206
case SyntaxKind.ReturnStatement:

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ namespace Harness {
10531053
];
10541054

10551055
let optionsIndex: ts.Map<ts.CommandLineOption>;
1056-
function getCommandLineOption(name: string): ts.CommandLineOption {
1056+
function getCommandLineOption(name: string): ts.CommandLineOption | undefined {
10571057
if (!optionsIndex) {
10581058
optionsIndex = ts.createMap<ts.CommandLineOption>();
10591059
const optionDeclarations = harnessOptionDeclarations.concat(ts.optionDeclarations);

src/services/breakpoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ namespace ts.BreakpointResolver {
146146

147147
case SyntaxKind.ForOfStatement:
148148
// span in initializer
149-
return spanInInitializerOfForLike(<ForOfStatement | ForInStatement>node);
149+
return spanInInitializerOfForLike(<ForOfStatement>node);
150150

151151
case SyntaxKind.SwitchStatement:
152152
// span on switch(...)

0 commit comments

Comments
 (0)