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

restrict increment-decrement fixer while fixing the postfix unary exp… #4415

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]