From de89459162c322d19de450a6915207d9e26a7df6 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 1 Dec 2015 16:19:40 -0800 Subject: [PATCH] add new rule --- Jakefile.js | 16 ++-------- scripts/tslint/noIncrementDecrementRule.ts | 35 ++++++++++++++++++++++ tslint.json | 3 +- 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 scripts/tslint/noIncrementDecrementRule.ts diff --git a/Jakefile.js b/Jakefile.js index 398b897097d..c6f364fe77f 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -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() { diff --git a/scripts/tslint/noIncrementDecrementRule.ts b/scripts/tslint/noIncrementDecrementRule.ts new file mode 100644 index 00000000000..f3b8f26f9f9 --- /dev/null +++ b/scripts/tslint/noIncrementDecrementRule.ts @@ -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)); + } +} diff --git a/tslint.json b/tslint.json index 9b010d9a896..a0b08594c88 100644 --- a/tslint.json +++ b/tslint.json @@ -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 } }