Skip to content

Commit

Permalink
Reassign for-in loop variable; resolves #2199
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 21, 2018
1 parent 6fdc744 commit 4783eef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/ast/nodes/ForInStatement.ts
@@ -1,7 +1,7 @@
import BlockScope from '../scopes/BlockScope';
import VariableDeclaration from './VariableDeclaration';
import Scope from '../scopes/Scope';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { ExecutionPathOptions, NEW_EXECUTION_PATH } from '../ExecutionPathOptions';
import { PatternNode } from './shared/Pattern';
import * as NodeType from './NodeType';
import { ExpressionNode, Node, StatementBase, StatementNode } from './shared/Node';
Expand All @@ -19,6 +19,13 @@ export default class ForInStatement extends StatementBase {
right: ExpressionNode;
body: StatementNode;

bind() {
super.bind();
if (this.left.type !== NodeType.VariableDeclaration) {
this.left.reassignPath(EMPTY_PATH, NEW_EXECUTION_PATH);
}
}

createScope(parentScope: Scope) {
this.scope = new BlockScope({ parent: parentScope });
}
Expand Down
4 changes: 3 additions & 1 deletion src/ast/nodes/ForOfStatement.ts
Expand Up @@ -21,7 +21,9 @@ export default class ForOfStatement extends StatementBase {

bind() {
super.bind();
this.left.reassignPath(EMPTY_PATH, NEW_EXECUTION_PATH);
if (this.left.type !== NodeType.VariableDeclaration) {
this.left.reassignPath(EMPTY_PATH, NEW_EXECUTION_PATH);
}
}

createScope(parentScope: Scope) {
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/for-loops-as-assignments/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'make sure for loops are counted as variable assignments (#2199)'
};
21 changes: 21 additions & 0 deletions test/function/samples/for-loops-as-assignments/main.js
@@ -0,0 +1,21 @@
let x = false;
let iteratedForIn = false;

for (x in {key: true}) {
if (x === 'key') {
iteratedForIn = true;
}
}

assert.ok(iteratedForIn);

let y = false;
let iteratedForOf = false;

for (y of ['key']) {
if (y === 'key') {
iteratedForOf = true;
}
}

assert.ok(iteratedForOf);

0 comments on commit 4783eef

Please sign in to comment.