Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #855 from csnover/noVarKeyword-for
Browse files Browse the repository at this point in the history
Forbid var keyword in for, for-in, and for-of loops
  • Loading branch information
adidahiya committed Dec 16, 2015
2 parents b7206e2 + b093959 commit 8617838
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/rules/noVarKeywordRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,26 @@ class NoVarKeywordWalker extends Lint.RuleWalker {

super.visitVariableStatement(node);
}

public visitForStatement(node: ts.ForStatement) {
this.handleInitializerNode(node.initializer);
super.visitForStatement(node);
}

public visitForInStatement(node: ts.ForInStatement) {
this.handleInitializerNode(node.initializer);
super.visitForInStatement(node);
}

public visitForOfStatement(node: ts.ForOfStatement) {
this.handleInitializerNode(node.initializer);
super.visitForOfStatement(node);
}

private handleInitializerNode(node: ts.VariableDeclarationList | ts.Expression) {
if (node && node.kind === ts.SyntaxKind.VariableDeclarationList &&
!(Lint.isNodeFlagSet(node, ts.NodeFlags.Let) || Lint.isNodeFlagSet(node, ts.NodeFlags.Const))) {
this.addFailure(this.createFailure(node.getStart(), "var".length, Rule.FAILURE_STRING));
}
}
}
11 changes: 11 additions & 0 deletions test/files/rules/novarkeyword.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ var i, // failure

var [a, b] = [1, 2]; // failure

for (var n; false;); // failure
for (var n1 in foo); // failure
for (var n2 of foo); // failure

declare var tmp2: any;

let bar;
Expand All @@ -22,3 +26,10 @@ module quz {
}

let [x, y] = [1, 2];

for (n; false;);
for (let n; false;);
for (let name in foo);
for (let name of foo);
for (const name in foo);
for (const name of foo);
3 changes: 3 additions & 0 deletions test/rules/noVarKeywordRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe("<no-var-keyword>", () => {
createFailure([4, 5], [4, 8]),
createFailure([7, 1], [7, 4]),
createFailure([10, 1], [10, 4]),
createFailure([12, 6], [12, 9]),
createFailure([13, 6], [13, 9]),
createFailure([14, 6], [14, 9]),
];
const actualFailures = TestUtils.applyRuleOnFile(fileName, NoVarKeywordRule);

Expand Down

0 comments on commit 8617838

Please sign in to comment.