Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
restrict increment-decrement fixer while fixing the postfix unary exp…
Browse files Browse the repository at this point in the history
…ressions (#4415)
  • Loading branch information
enessoylu authored and adidahiya committed Jan 10, 2019
1 parent ba5ffe8 commit bc382bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/rules/incrementDecrementRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ function walk(context: Lint.WalkContext<Options>) {

function complainOnNode(node: ts.PostfixUnaryExpression | ts.PrefixUnaryExpression) {
const newOperatorText = node.operator === ts.SyntaxKind.PlusPlusToken ? "+= 1" : "-= 1";
const replacement = createReplacement(node, newOperatorText);
let replacement: Lint.Replacement | undefined;

if (
tsutils.isPrefixUnaryExpression(node) ||
node.parent.kind === ts.SyntaxKind.ExpressionStatement
) {
replacement = createReplacement(node, newOperatorText);
}

const failure = Rule.FAILURE_STRING_FACTORY(newOperatorText);

Expand Down
11 changes: 7 additions & 4 deletions test/rules/increment-decrement/default/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ x - 1;
1 - x;

x + (x += 1);
x + (x += 1);
x + x++;

x - (x -= 1);
x - (x -= 1);
x - x--;

(x += 1) + x;
(x += 1) + x;
x++ + x;

(x -= 1) - x;
(x -= 1) - x;
x-- - x;

expect(x++)
expect(x += 1)
5 changes: 5 additions & 0 deletions test/rules/increment-decrement/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ x-- - x;
~~~ [minus]
[plus]: Use an explicit += 1 operator.
[minus]: Use an explicit -= 1 operator.

expect(x++)
~~~ [plus]
expect(++x)
~~~ [plus]

0 comments on commit bc382bf

Please sign in to comment.