Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestion for 'non-existent-operator' #336

Merged
merged 1 commit into from Mar 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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