Add isForInOrOfStatement utility (#16455)

This commit is contained in:
Andy 2017-06-12 11:08:41 -07:00 committed by GitHub
parent 44d5c44cb5
commit 2748b3b334
7 changed files with 21 additions and 24 deletions

View File

@ -612,7 +612,7 @@ namespace ts {
break;
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
bindForInOrForOfStatement(<ForInStatement | ForOfStatement>node);
bindForInOrForOfStatement(<ForInOrOfStatement>node);
break;
case SyntaxKind.IfStatement:
bindIfStatement(<IfStatement>node);
@ -950,7 +950,7 @@ namespace ts {
currentFlow = finishFlowLabel(postLoopLabel);
}
function bindForInOrForOfStatement(node: ForInStatement | ForOfStatement): void {
function bindForInOrForOfStatement(node: ForInOrOfStatement): void {
const preLoopLabel = createLoopLabel();
const postLoopLabel = createBranchLabel();
addAntecedent(preLoopLabel, currentFlow);
@ -1328,7 +1328,7 @@ namespace ts {
function bindVariableDeclarationFlow(node: VariableDeclaration) {
bindEachChild(node);
if (node.initializer || node.parent.parent.kind === SyntaxKind.ForInStatement || node.parent.parent.kind === SyntaxKind.ForOfStatement) {
if (node.initializer || isForInOrOfStatement(node.parent.parent)) {
bindInitializedVariableFlow(node);
}
}

View File

@ -804,16 +804,8 @@ namespace ts {
break;
}
switch (declaration.parent.parent.kind) {
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
// ForIn/ForOf case - use site should not be used in expression part
if (isSameScopeDescendentOf(usage, (<ForInStatement | ForOfStatement>declaration.parent.parent).expression, container)) {
return true;
}
}
return false;
// ForIn/ForOf case - use site should not be used in expression part
return isForInOrOfStatement(declaration.parent.parent) && isSameScopeDescendentOf(usage, declaration.parent.parent.expression, container);
}
function isUsedInFunctionOrInstanceProperty(usage: Node, declaration: Node, container?: Node): boolean {
@ -19588,9 +19580,7 @@ namespace ts {
function errorUnusedLocal(node: Node, name: string) {
if (isIdentifierThatStartsWithUnderScore(node)) {
const declaration = getRootDeclaration(node.parent);
if (declaration.kind === SyntaxKind.VariableDeclaration &&
(declaration.parent.parent.kind === SyntaxKind.ForInStatement ||
declaration.parent.parent.kind === SyntaxKind.ForOfStatement)) {
if (declaration.kind === SyntaxKind.VariableDeclaration && isForInOrOfStatement(declaration.parent.parent)) {
return;
}
}
@ -20283,7 +20273,7 @@ namespace ts {
}
}
function checkForInOrForOfVariableDeclaration(iterationStatement: ForInStatement | ForOfStatement): void {
function checkForInOrForOfVariableDeclaration(iterationStatement: ForInOrOfStatement): void {
const variableDeclarationList = <VariableDeclarationList>iterationStatement.initializer;
// checkGrammarForInOrForOfStatement will check that there is exactly one declaration.
if (variableDeclarationList.declarations.length >= 1) {
@ -24263,7 +24253,7 @@ namespace ts {
}
}
function checkGrammarForInOrForOfStatement(forInOrOfStatement: ForInStatement | ForOfStatement): boolean {
function checkGrammarForInOrForOfStatement(forInOrOfStatement: ForInOrOfStatement): boolean {
if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) {
return true;
}

View File

@ -1708,6 +1708,8 @@ namespace ts {
incrementor?: Expression;
}
export type ForInOrOfStatement = ForInStatement | ForOfStatement;
export interface ForInStatement extends IterationStatement {
kind: SyntaxKind.ForInStatement;
initializer: ForInitializer;

View File

@ -1627,7 +1627,7 @@ namespace ts {
return unaryOperator === SyntaxKind.PlusPlusToken || unaryOperator === SyntaxKind.MinusMinusToken ? AssignmentKind.Compound : AssignmentKind.None;
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
return (<ForInStatement | ForOfStatement>parent).initializer === node ? AssignmentKind.Definite : AssignmentKind.None;
return (<ForInOrOfStatement>parent).initializer === node ? AssignmentKind.Definite : AssignmentKind.None;
case SyntaxKind.ParenthesizedExpression:
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.SpreadElement:
@ -5152,6 +5152,11 @@ namespace ts {
return false;
}
/* @internal */
export function isForInOrOfStatement(node: Node): node is ForInOrOfStatement {
return node.kind === SyntaxKind.ForInStatement || node.kind === SyntaxKind.ForOfStatement;
}
// Element
/* @internal */

View File

@ -1198,9 +1198,9 @@ namespace ts {
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
result = reduceNode((<ForInStatement | ForOfStatement>node).initializer, cbNode, result);
result = reduceNode((<ForInStatement | ForOfStatement>node).expression, cbNode, result);
result = reduceNode((<ForInStatement | ForOfStatement>node).statement, cbNode, result);
result = reduceNode((<ForInOrOfStatement>node).initializer, cbNode, result);
result = reduceNode((<ForInOrOfStatement>node).expression, cbNode, result);
result = reduceNode((<ForInOrOfStatement>node).statement, cbNode, result);
break;
case SyntaxKind.ReturnStatement:

View File

@ -1053,7 +1053,7 @@ namespace Harness {
];
let optionsIndex: ts.Map<ts.CommandLineOption>;
function getCommandLineOption(name: string): ts.CommandLineOption {
function getCommandLineOption(name: string): ts.CommandLineOption | undefined {
if (!optionsIndex) {
optionsIndex = ts.createMap<ts.CommandLineOption>();
const optionDeclarations = harnessOptionDeclarations.concat(ts.optionDeclarations);

View File

@ -146,7 +146,7 @@ namespace ts.BreakpointResolver {
case SyntaxKind.ForOfStatement:
// span in initializer
return spanInInitializerOfForLike(<ForOfStatement | ForInStatement>node);
return spanInInitializerOfForLike(<ForOfStatement>node);
case SyntaxKind.SwitchStatement:
// span on switch(...)