Skip to content

Commit de89459

Browse files
committed
add new rule
1 parent e134169 commit de89459

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

Jakefile.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,6 @@ var serverCoreSources = [
108108
return path.join(serverDirectory, f);
109109
});
110110

111-
var scriptSources = [
112-
"tslint/booleanTriviaRule.ts",
113-
"tslint/nextLineRule.ts",
114-
"tslint/noNullRule.ts",
115-
"tslint/preferConstRule.ts",
116-
"tslint/typeOperatorSpacingRule.ts",
117-
"tslint/noInOperatorRule.ts"
118-
].map(function (f) {
119-
return path.join(scriptsDirectory, f);
120-
});
121-
122111
var serverSources = serverCoreSources.concat(servicesSources);
123112

124113
var languageServiceLibrarySources = [
@@ -877,7 +866,8 @@ var tslintRules = ([
877866
"preferConstRule",
878867
"booleanTriviaRule",
879868
"typeOperatorSpacingRule",
880-
"noInOperatorRule"
869+
"noInOperatorRule",
870+
"noIncrementDecrementRule"
881871
]);
882872
var tslintRulesFiles = tslintRules.map(function(p) {
883873
return path.join(tslintRuleDir, p + ".ts");
@@ -923,7 +913,7 @@ function lintFileAsync(options, path, cb) {
923913
var lintTargets = compilerSources
924914
.concat(harnessCoreSources)
925915
.concat(serverCoreSources)
926-
.concat(scriptSources);
916+
.concat(tslintRulesFiles);
927917

928918
desc("Runs tslint on the compiler sources");
929919
task("lint", ["build-rules"], function() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 '++' or '--' operators outside for for loops or statements - prefer '+= 1' and '-= 1'.";
7+
8+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
9+
return this.applyWithWalker(new IncrementDecrementWalker(sourceFile, this.getOptions()));
10+
}
11+
}
12+
13+
class IncrementDecrementWalker extends Lint.RuleWalker {
14+
15+
visitPostfixUnaryExpression(node: ts.PostfixUnaryExpression) {
16+
super.visitPostfixUnaryExpression(node);
17+
if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator == ts.SyntaxKind.MinusMinusToken) {
18+
this.visitIncrementDecrement(node);
19+
}
20+
}
21+
22+
visitPrefixUnaryExpression(node: ts.PrefixUnaryExpression) {
23+
super.visitPrefixUnaryExpression(node);
24+
if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator == ts.SyntaxKind.MinusMinusToken) {
25+
this.visitIncrementDecrement(node);
26+
}
27+
}
28+
29+
visitIncrementDecrement(node: ts.UnaryExpression) {
30+
if (node.parent && (node.parent.kind === ts.SyntaxKind.ExpressionStatement || node.parent.kind === ts.SyntaxKind.ForStatement)) {
31+
return;
32+
}
33+
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
34+
}
35+
}

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"boolean-trivia": true,
4242
"type-operator-spacing": true,
4343
"prefer-const": true,
44-
"no-in-operator": true
44+
"no-in-operator": true,
45+
"no-increment-decrement": true
4546
}
4647
}

0 commit comments

Comments
 (0)