Skip to content

Commit

Permalink
fix(eslint-plugin): [strict-boolean-expressions] replace dangerous fi…
Browse files Browse the repository at this point in the history
…xer with suggestions (#7394)

* fix(eslint-plugin): remove explicit rule check for logical negation expressions

* feat: add null check error without fix options

* refactor: add explicit null check rule suggestion

* feat: change suggestion fix message id

* test: add fix compare nullish suggestion

* refactor: move !nullableObject to suggestions
  • Loading branch information
kemilbeltre authored Aug 10, 2023
1 parent bad37a2 commit ad8401a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 25 deletions.
32 changes: 21 additions & 11 deletions packages/eslint-plugin/src/rules/strict-boolean-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,23 +703,33 @@ export default util.createRule<Options, MessageId>({
context.report({
node,
messageId: 'conditionErrorNullableObject',
fix: util.getWrappingFixer({
sourceCode,
node: node.parent,
innerNode: node,
wrap: code => `${code} == null`,
}),
suggest: [
{
messageId: 'conditionFixCompareNullish',
fix: util.getWrappingFixer({
sourceCode,
node: node.parent,
innerNode: node,
wrap: code => `${code} == null`,
}),
},
],
});
} else {
// if (nullableObject)
context.report({
node,
messageId: 'conditionErrorNullableObject',
fix: util.getWrappingFixer({
sourceCode,
node,
wrap: code => `${code} != null`,
}),
suggest: [
{
messageId: 'conditionFixCompareNullish',
fix: util.getWrappingFixer({
sourceCode,
node,
wrap: code => `${code} != null`,
}),
},
],
});
}
}
Expand Down
110 changes: 96 additions & 14 deletions packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,15 +1008,40 @@ if (y) {
<T extends {} | null | undefined>(x: T) => x ? 1 : 0;
`,
errors: [
{ messageId: 'conditionErrorNullableObject', line: 2, column: 37 },
{ messageId: 'conditionErrorNullableObject', line: 3, column: 33 },
{ messageId: 'conditionErrorNullableObject', line: 4, column: 52 },
{
messageId: 'conditionErrorNullableObject',
line: 2,
column: 37,
suggestions: [
{
messageId: 'conditionFixCompareNullish',
output: 'declare const x: object | null; if (x != null) {}',
},
],
},
{
messageId: 'conditionErrorNullableObject',
line: 3,
column: 33,
suggestions: [
{
messageId: 'conditionFixCompareNullish',
output: ` (x?: { a: number }) => x == null;`,
},
],
},
{
messageId: 'conditionErrorNullableObject',
line: 4,
column: 52,
suggestions: [
{
messageId: 'conditionFixCompareNullish',
output: ` <T extends {} | null | undefined>(x: T) => (x != null) ? 1 : 0;`,
},
],
},
],
output: `
declare const x: object | null; if (x != null) {}
(x?: { a: number }) => x == null;
<T extends {} | null | undefined>(x: T) => (x != null) ? 1 : 0;
`,
}),

// nullable string in boolean context
Expand Down Expand Up @@ -1620,18 +1645,75 @@ if (x) {
obj && 1 || 0
`,
errors: [
{ messageId: 'conditionErrorNullableObject', line: 3, column: 10 },
{ messageId: 'conditionErrorNullableObject', line: 4, column: 10 },
{ messageId: 'conditionErrorNullableObject', line: 5, column: 9 },
{ messageId: 'conditionErrorNullableObject', line: 6, column: 9 },
],
output: `
{
messageId: 'conditionErrorNullableObject',
line: 3,
column: 10,
suggestions: [
{
messageId: 'conditionFixCompareNullish',
output: `
declare const obj: { x: number } | null;
(obj == null) ? 1 : 0
!obj
obj || 0
obj && 1 || 0
`,
},
],
},
{
messageId: 'conditionErrorNullableObject',
line: 4,
column: 10,
suggestions: [
{
messageId: 'conditionFixCompareNullish',
output: `
declare const obj: { x: number } | null;
!obj ? 1 : 0
obj == null
obj || 0
obj && 1 || 0
`,
},
],
},
{
messageId: 'conditionErrorNullableObject',
line: 5,
column: 9,
suggestions: [
{
messageId: 'conditionFixCompareNullish',
output: `
declare const obj: { x: number } | null;
!obj ? 1 : 0
!obj
;(obj != null) || 0
obj && 1 || 0
`,
},
],
},
{
messageId: 'conditionErrorNullableObject',
line: 6,
column: 9,
suggestions: [
{
messageId: 'conditionFixCompareNullish',
output: `
declare const obj: { x: number } | null;
!obj ? 1 : 0
!obj
obj || 0
;(obj != null) && 1 || 0
`,
},
],
},
],
},
],
});

0 comments on commit ad8401a

Please sign in to comment.