Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Fix no-inverted-boolean-check: enclose autofix with parentheses on do…
Browse files Browse the repository at this point in the history
…uble negation (#278)
  • Loading branch information
yassin-kammoun-sonarsource authored Aug 11, 2021
1 parent 536fdd9 commit 232093a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/rules/no-inverted-boolean-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ function visitUnaryExpression(
if (invertedOperator) {
const left = context.getSourceCode().getText(condition.left);
const right = context.getSourceCode().getText(condition.right);
const [start, end] =
unaryExpression.parent?.type === 'UnaryExpression' ? ['(', ')'] : ['', ''];
context.report({
message: MESSAGE,
data: { invertedOperator },
node: unaryExpression,
fix: fixer => fixer.replaceText(unaryExpression, `${left} ${invertedOperator} ${right}`),
fix: fixer =>
fixer.replaceText(unaryExpression, `${start}${left} ${invertedOperator} ${right}${end}`),
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/rules/no-inverted-boolean-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ ruleTester.run('no-inverted-boolean-check', rule, {
errors: [message('>')],
output: `let foo = x > 4`,
},
{
code: `let foo = !!(a < b)`,
errors: [message('>=')],
output: 'let foo = !(a >= b)',
},
],
});

Expand Down

0 comments on commit 232093a

Please # to comment.