Skip to content

Commit a7746fb

Browse files
feat(eslint-plugin): [prefer-nullish-coalescing] fix detection of ignoreConditionalTests involving boolean ! operator (#10299)
Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>
1 parent b351703 commit a7746fb

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ function isConditionalTest(node: TSESTree.Node): boolean {
476476
return isConditionalTest(parent);
477477
}
478478

479+
if (
480+
parent.type === AST_NODE_TYPES.UnaryExpression &&
481+
parent.operator === '!'
482+
) {
483+
return isConditionalTest(parent);
484+
}
485+
479486
if (
480487
(parent.type === AST_NODE_TYPES.ConditionalExpression ||
481488
parent.type === AST_NODE_TYPES.DoWhileStatement ||

packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,34 @@ let b: string | boolean | undefined;
575575
let c: string | boolean | undefined;
576576
577577
if (((a = b), b || c)) {
578+
}
579+
`,
580+
options: [
581+
{
582+
ignoreConditionalTests: true,
583+
},
584+
],
585+
},
586+
{
587+
code: `
588+
let a: string | undefined;
589+
let b: string | undefined;
590+
591+
if (!(a || b)) {
592+
}
593+
`,
594+
options: [
595+
{
596+
ignoreConditionalTests: true,
597+
},
598+
],
599+
},
600+
{
601+
code: `
602+
let a: string | undefined;
603+
let b: string | undefined;
604+
605+
if (!!(a || b)) {
578606
}
579607
`,
580608
options: [
@@ -2266,7 +2294,38 @@ if (f(a ?? b)) {
22662294
],
22672295
options: [
22682296
{
2269-
ignoreBooleanCoercion: true,
2297+
ignoreConditionalTests: true,
2298+
},
2299+
],
2300+
},
2301+
{
2302+
code: `
2303+
declare const a: string | undefined;
2304+
declare const b: string;
2305+
2306+
if (+(a || b)) {
2307+
}
2308+
`,
2309+
errors: [
2310+
{
2311+
messageId: 'preferNullishOverOr',
2312+
suggestions: [
2313+
{
2314+
messageId: 'suggestNullish',
2315+
output: `
2316+
declare const a: string | undefined;
2317+
declare const b: string;
2318+
2319+
if (+(a ?? b)) {
2320+
}
2321+
`,
2322+
},
2323+
],
2324+
},
2325+
],
2326+
options: [
2327+
{
2328+
ignoreConditionalTests: true,
22702329
},
22712330
],
22722331
},

0 commit comments

Comments
 (0)