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

Commit 1d6b198

Browse files
Deactivate 'no-inverted-boolean' and replace fix with suggestion (#322)
* Deactivate 'no-inverted-boolean' and replace fix with suggestion * Fix formatting * Update README Co-authored-by: yassin-kammoun-sonarsource <[email protected]>
1 parent d98f168 commit 1d6b198

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Code Smells, or maintainability issues, are raised for places of code which migh
3232
* Two branches in a conditional structure should not have exactly the same implementation ([`no-duplicated-branches`])
3333
* Boolean expressions should not be gratuitous ([`no-gratuitous-expressions`])
3434
* Functions should not have identical implementations ([`no-identical-functions`])
35-
* Boolean checks should not be inverted ([`no-inverted-boolean-check`]) (:wrench: *fixable*)
35+
* Boolean checks should not be inverted ([`no-inverted-boolean-check`]) (:wrench: *fixable*, *disabled*)
3636
* "switch" statements should not be nested ([`no-nested-switch`])
3737
* Template literals should not be nested ([`no-nested-template-literals`])
3838
* Boolean literals should not be redundant ([`no-redundant-boolean`])

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rules/no-inverted-boolean-check.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ const rule: TSESLint.RuleModule<string, string[]> = {
3838
meta: {
3939
messages: {
4040
useOppositeOperator: 'Use the opposite operator ({{invertedOperator}}) instead.',
41+
suggestOperationInversion: 'Invert inner operation (apply if NaN is not expected)',
4142
},
4243
schema: [],
4344
type: 'suggestion',
4445
docs: {
4546
description: 'Boolean checks should not be inverted',
46-
recommended: 'error',
47+
recommended: false,
4748
url: docsUrl(__filename),
4849
},
50+
hasSuggestions: true,
4951
fixable: 'code',
5052
},
5153
create(context) {
@@ -70,10 +72,18 @@ function visitUnaryExpression(
7072
unaryExpression.parent?.type === 'UnaryExpression' ? ['(', ')'] : ['', ''];
7173
context.report({
7274
messageId: 'useOppositeOperator',
75+
suggest: [
76+
{
77+
messageId: 'suggestOperationInversion',
78+
fix: fixer =>
79+
fixer.replaceText(
80+
unaryExpression,
81+
`${start}${left} ${invertedOperator} ${right}${end}`,
82+
),
83+
},
84+
],
7385
data: { invertedOperator },
7486
node: unaryExpression,
75-
fix: fixer =>
76-
fixer.replaceText(unaryExpression, `${start}${left} ${invertedOperator} ${right}${end}`),
7787
});
7888
}
7989
}

tests/rules/no-inverted-boolean-check.test.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,82 +45,79 @@ ruleTester.run('no-inverted-boolean-check', rule, {
4545
code: `if (!(x == 1)) {}`,
4646
errors: [
4747
{
48-
...message('!='),
48+
...error('!=', `if (x != 1) {}`),
4949
line: 1,
5050
endLine: 1,
5151
column: 5,
5252
endColumn: 14,
5353
},
5454
],
55-
output: `if (x != 1) {}`,
5655
},
5756
// `!=` => `==`
5857
{
5958
code: `if (!(x != 1)) {}`,
60-
errors: [message('==')],
61-
output: `if (x == 1) {}`,
59+
errors: [error('==', `if (x == 1) {}`)],
6260
},
6361
// `===` => `!==`
6462
{
6563
code: `if (!(x === 1)) {}`,
66-
errors: [message('!==')],
67-
output: `if (x !== 1) {}`,
64+
errors: [error('!==', `if (x !== 1) {}`)],
6865
},
6966
// `!==` => `===`
7067
{
7168
code: `if (!(x !== 1)) {}`,
72-
errors: [message('===')],
73-
output: `if (x === 1) {}`,
69+
errors: [error('===', `if (x === 1) {}`)],
7470
},
7571
// `>` => `<=`
7672
{
7773
code: `if (!(x > 1)) {}`,
78-
errors: [message('<=')],
79-
output: `if (x <= 1) {}`,
74+
errors: [error('<=', `if (x <= 1) {}`)],
8075
},
8176
// `<` => `>=`
8277
{
8378
code: `if (!(x < 1)) {}`,
84-
errors: [message('>=')],
85-
output: `if (x >= 1) {}`,
79+
errors: [error('>=', `if (x >= 1) {}`)],
8680
},
8781
// `>=` => `<`
8882
{
8983
code: `if (!(x >= 1)) {}`,
90-
errors: [message('<')],
91-
output: `if (x < 1) {}`,
84+
errors: [error('<', `if (x < 1) {}`)],
9285
},
9386
// `<=` => `>`
9487
{
9588
code: `if (!(x <= 1)) {}`,
96-
errors: [message('>')],
97-
output: `if (x > 1) {}`,
89+
errors: [error('>', `if (x > 1) {}`)],
9890
},
9991
// ternary operator
10092
{
10193
code: `!(x != 1) ? 1 : 2`,
102-
errors: [message('==')],
103-
output: `x == 1 ? 1 : 2`,
94+
errors: [error('==', `x == 1 ? 1 : 2`)],
10495
},
10596
// not conditional
10697
{
10798
code: `foo(!(x === 1))`,
108-
errors: [message('!==')],
109-
output: `foo(x !== 1)`,
99+
errors: [error('!==', `foo(x !== 1)`)],
110100
},
111101
{
112102
code: `let foo = !(x <= 4)`,
113-
errors: [message('>')],
114-
output: `let foo = x > 4`,
103+
errors: [error('>', `let foo = x > 4`)],
115104
},
116105
{
117106
code: `let foo = !!(a < b)`,
118-
errors: [message('>=')],
119-
output: 'let foo = !(a >= b)',
107+
errors: [error('>=', 'let foo = !(a >= b)')],
120108
},
121109
],
122110
});
123111

124-
function message(invertedOperator: string): TSESLint.TestCaseError<string> {
125-
return { messageId: 'useOppositeOperator', data: { invertedOperator } };
112+
function error(invertedOperator: string, output: string): TSESLint.TestCaseError<string> {
113+
return {
114+
messageId: 'useOppositeOperator',
115+
data: { invertedOperator },
116+
suggestions: [
117+
{
118+
messageId: 'suggestOperationInversion',
119+
output,
120+
},
121+
],
122+
};
126123
}

0 commit comments

Comments
 (0)