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)', + }, ], });