Skip to content

Commit 3085806

Browse files
committed
lint rule forbidding the in keyword binary expression
1 parent 5e86ca1 commit 3085806

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Jakefile.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ var scriptSources = [
113113
"tslint/nextLineRule.ts",
114114
"tslint/noNullRule.ts",
115115
"tslint/preferConstRule.ts",
116-
"tslint/typeOperatorSpacingRule.ts"
116+
"tslint/typeOperatorSpacingRule.ts",
117+
"tslint/noInOperatorRule.ts"
117118
].map(function (f) {
118119
return path.join(scriptsDirectory, f);
119120
});
@@ -875,7 +876,8 @@ var tslintRules = ([
875876
"noNullRule",
876877
"preferConstRule",
877878
"booleanTriviaRule",
878-
"typeOperatorSpacingRule"
879+
"typeOperatorSpacingRule",
880+
"noInOperatorRule"
879881
]);
880882
var tslintRulesFiles = tslintRules.map(function(p) {
881883
return path.join(tslintRuleDir, p + ".ts");

scripts/tslint/noInOperatorRule.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as Lint from "tslint/lib/lint";
2+
import * as ts from "typescript";
3+
4+
5+
export class Rule extends Lint.Rules.AbstractRule {
6+
public static FAILURE_STRING = "Don't use the 'in' keyword - use 'hasProperty' to check for key presence instead";
7+
8+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
9+
return this.applyWithWalker(new InWalker(sourceFile, this.getOptions()));
10+
}
11+
}
12+
13+
class InWalker extends Lint.RuleWalker {
14+
visitNode(node: ts.Node) {
15+
super.visitNode(node);
16+
if (node.kind === ts.SyntaxKind.InKeyword && node.parent && node.parent.kind === ts.SyntaxKind.BinaryExpression) {
17+
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
18+
}
19+
}
20+
}

tslint.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"no-null": true,
4141
"boolean-trivia": true,
4242
"type-operator-spacing": true,
43-
"prefer-const": true
43+
"prefer-const": true,
44+
"no-in-operator": true
4445
}
4546
}

0 commit comments

Comments
 (0)