Skip to content

Commit

Permalink
Add suggestion for 'non-existent-operator' (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Mar 24, 2022
1 parent d3c2f8f commit 35ea28d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -17,7 +17,7 @@ Rules in this category aim to find places in code which have a high chance of be
* Return values from functions without side effects should not be ignored ([`no-ignored-return`]) (*uses-types*)
* Loops with at most one iteration should be refactored ([`no-one-iteration-loop`])
* The output of functions that don't return anything should not be used ([`no-use-of-empty-return-value`])
* Non-existent operators '=+', '=-' and '=!' should not be used ([`non-existent-operator`])
* Non-existent operators '=+', '=-' and '=!' should not be used ([`non-existent-operator`]) (:wrench: *fixable*)

### Code Smell Detection :pig:

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/non-existent-operator.md
@@ -1,5 +1,7 @@
# non-existent-operator

:wrench: *fixable*

The use of operators pairs (`=+`, `=-` or `=!`) where the reversed, single operator was meant (`+=`, `-=` or `!=`) will compile and run, but not produce the expected results.

This rule raises an issue when `=+`, `=-` and `=!` are used without any space between the two operators and when there is at least one whitespace after.
Expand Down
18 changes: 18 additions & 0 deletions src/rules/non-existent-operator.ts
Expand Up @@ -26,9 +26,11 @@ const rule: TSESLint.RuleModule<string, string[]> = {
meta: {
messages: {
useExistingOperator: 'Was "{{operator}}=" meant instead?',
suggestExistingOperator: 'Replace with "{{operator}}" operator',
},
schema: [],
type: 'problem',
hasSuggestions: true,
docs: {
description: 'Non-existent operators "=+", "=-" and "=!" should not be used',
recommended: 'error',
Expand Down Expand Up @@ -75,12 +77,28 @@ function checkOperator(
areAdjacent(assignmentOperatorToken, unaryOperatorToken) &&
!areAdjacent(unaryOperatorToken, expressionFirstToken)
) {
const suggest: TSESLint.ReportSuggestionArray<string> = [];
if (unaryNode.parent?.type === 'AssignmentExpression') {
const range: [number, number] = [
assignmentOperatorToken.range[0],
unaryOperatorToken.range[1],
];
const invertedOperators = unaryOperatorToken.value + assignmentOperatorToken.value;
suggest.push({
messageId: 'suggestExistingOperator',
data: {
operator: invertedOperators,
},
fix: fixer => fixer.replaceTextRange(range, invertedOperators),
});
}
context.report({
messageId: 'useExistingOperator',
data: {
operator: unaryNode.operator,
},
loc: { start: assignmentOperatorToken.loc.start, end: unaryOperatorToken.loc.end },
suggest,
});
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/rules/non-existent-operator.test.ts
Expand Up @@ -53,6 +53,15 @@ ruleTester.run("Non-existent operators '=+', '=-' and '=!' should not be used",
endLine: 1,
column: 3,
endColumn: 5,
suggestions: [
{
messageId: 'suggestExistingOperator',
data: {
operator: '+=',
},
output: `x += y;`,
},
],
},
],
},
Expand Down Expand Up @@ -114,6 +123,7 @@ ruleTester.run("Non-existent operators '=+', '=-' and '=!' should not be used",
endLine: 1,
column: 7,
endColumn: 9,
suggestions: [],
},
],
},
Expand Down

0 comments on commit 35ea28d

Please sign in to comment.