From 232093af9f6d83d1630e5daa20d021eb095cc266 Mon Sep 17 00:00:00 2001 From: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com> Date: Wed, 11 Aug 2021 14:31:03 +0200 Subject: [PATCH] Fix no-inverted-boolean-check: enclose autofix with parentheses on double negation (#278) --- src/rules/no-inverted-boolean-check.ts | 5 ++++- tests/rules/no-inverted-boolean-check.test.ts | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/rules/no-inverted-boolean-check.ts b/src/rules/no-inverted-boolean-check.ts index c51229c9..0df8dbdd 100644 --- a/src/rules/no-inverted-boolean-check.ts +++ b/src/rules/no-inverted-boolean-check.ts @@ -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}`), }); } } diff --git a/tests/rules/no-inverted-boolean-check.test.ts b/tests/rules/no-inverted-boolean-check.test.ts index 8cfcad76..1ccb87e4 100644 --- a/tests/rules/no-inverted-boolean-check.test.ts +++ b/tests/rules/no-inverted-boolean-check.test.ts @@ -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)', + }, ], });