@@ -3,7 +3,8 @@ import * as ts from "typescript";
33
44
55export class Rule extends Lint . Rules . AbstractRule {
6- public static FAILURE_STRING = "Don't use '++' or '--' operators outside for loops or statements - prefer '+= 1' and '-= 1'." ;
6+ public static POSTFIX_FAILURE_STRING = "Don't use '++' or '--' postfix operators outside statements, for loops, or element access expressions." ;
7+ public static PREFIX_FAILURE_STRING = "Don't use '++' or '--' prefix operators." ;
78
89 public apply ( sourceFile : ts . SourceFile ) : Lint . RuleFailure [ ] {
910 return this . applyWithWalker ( new IncrementDecrementWalker ( sourceFile , this . getOptions ( ) ) ) ;
@@ -22,14 +23,14 @@ class IncrementDecrementWalker extends Lint.RuleWalker {
2223 visitPrefixUnaryExpression ( node : ts . PrefixUnaryExpression ) {
2324 super . visitPrefixUnaryExpression ( node ) ;
2425 if ( node . operator === ts . SyntaxKind . PlusPlusToken || node . operator == ts . SyntaxKind . MinusMinusToken ) {
25- this . visitIncrementDecrement ( node ) ;
26+ this . addFailure ( this . createFailure ( node . getStart ( ) , node . getWidth ( ) , Rule . PREFIX_FAILURE_STRING ) ) ;
2627 }
2728 }
2829
2930 visitIncrementDecrement ( node : ts . UnaryExpression ) {
30- if ( node . parent && ( node . parent . kind === ts . SyntaxKind . ExpressionStatement || node . parent . kind === ts . SyntaxKind . ForStatement ) ) {
31+ if ( node . parent && ( node . parent . kind === ts . SyntaxKind . ExpressionStatement || node . parent . kind === ts . SyntaxKind . ForStatement || node . parent . kind === ts . SyntaxKind . ElementAccessExpression ) ) {
3132 return ;
3233 }
33- this . addFailure ( this . createFailure ( node . getStart ( ) , node . getWidth ( ) , Rule . FAILURE_STRING ) ) ;
34+ this . addFailure ( this . createFailure ( node . getStart ( ) , node . getWidth ( ) , Rule . POSTFIX_FAILURE_STRING ) ) ;
3435 }
3536}
0 commit comments