add new rule

This commit is contained in:
Wesley Wigham
2015-12-01 16:19:40 -08:00
parent e134169590
commit de89459162
3 changed files with 40 additions and 14 deletions

View File

@@ -108,17 +108,6 @@ var serverCoreSources = [
return path.join(serverDirectory, f);
});
var scriptSources = [
"tslint/booleanTriviaRule.ts",
"tslint/nextLineRule.ts",
"tslint/noNullRule.ts",
"tslint/preferConstRule.ts",
"tslint/typeOperatorSpacingRule.ts",
"tslint/noInOperatorRule.ts"
].map(function (f) {
return path.join(scriptsDirectory, f);
});
var serverSources = serverCoreSources.concat(servicesSources);
var languageServiceLibrarySources = [
@@ -877,7 +866,8 @@ var tslintRules = ([
"preferConstRule",
"booleanTriviaRule",
"typeOperatorSpacingRule",
"noInOperatorRule"
"noInOperatorRule",
"noIncrementDecrementRule"
]);
var tslintRulesFiles = tslintRules.map(function(p) {
return path.join(tslintRuleDir, p + ".ts");
@@ -923,7 +913,7 @@ function lintFileAsync(options, path, cb) {
var lintTargets = compilerSources
.concat(harnessCoreSources)
.concat(serverCoreSources)
.concat(scriptSources);
.concat(tslintRulesFiles);
desc("Runs tslint on the compiler sources");
task("lint", ["build-rules"], function() {

View File

@@ -0,0 +1,35 @@
import * as Lint from "tslint/lib/lint";
import * as ts from "typescript";
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "Don't use '++' or '--' operators outside for for loops or statements - prefer '+= 1' and '-= 1'.";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new IncrementDecrementWalker(sourceFile, this.getOptions()));
}
}
class IncrementDecrementWalker extends Lint.RuleWalker {
visitPostfixUnaryExpression(node: ts.PostfixUnaryExpression) {
super.visitPostfixUnaryExpression(node);
if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator == ts.SyntaxKind.MinusMinusToken) {
this.visitIncrementDecrement(node);
}
}
visitPrefixUnaryExpression(node: ts.PrefixUnaryExpression) {
super.visitPrefixUnaryExpression(node);
if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator == ts.SyntaxKind.MinusMinusToken) {
this.visitIncrementDecrement(node);
}
}
visitIncrementDecrement(node: ts.UnaryExpression) {
if (node.parent && (node.parent.kind === ts.SyntaxKind.ExpressionStatement || node.parent.kind === ts.SyntaxKind.ForStatement)) {
return;
}
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
}

View File

@@ -41,6 +41,7 @@
"boolean-trivia": true,
"type-operator-spacing": true,
"prefer-const": true,
"no-in-operator": true
"no-in-operator": true,
"no-increment-decrement": true
}
}