Skip to content
7 changes: 7 additions & 0 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ function isConditionalTest(node: TSESTree.Node): boolean {
return isConditionalTest(parent);
}

if (
parent.type === AST_NODE_TYPES.UnaryExpression &&
parent.operator === '!'
) {
return isConditionalTest(parent);
}

if (
(parent.type === AST_NODE_TYPES.ConditionalExpression ||
parent.type === AST_NODE_TYPES.DoWhileStatement ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,34 @@ let b: string | boolean | undefined;
let c: string | boolean | undefined;

if (((a = b), b || c)) {
}
`,
options: [
{
ignoreConditionalTests: true,
},
],
},
{
code: `
let a: string | undefined;
let b: string | undefined;

if (!(a || b)) {
}
`,
options: [
{
ignoreConditionalTests: true,
},
],
},
{
code: `
let a: string | undefined;
let b: string | undefined;

if (!!(a || b)) {
}
`,
options: [
Expand Down Expand Up @@ -2266,7 +2294,38 @@ if (f(a ?? b)) {
],
options: [
{
ignoreBooleanCoercion: true,
ignoreConditionalTests: true,
},
],
},
{
code: `
declare const a: string | undefined;
declare const b: string;

if (+(a || b)) {
}
`,
errors: [
{
messageId: 'preferNullishOverOr',
suggestions: [
{
messageId: 'suggestNullish',
output: `
declare const a: string | undefined;
declare const b: string;

if (+(a ?? b)) {
}
`,
},
],
},
],
options: [
{
ignoreConditionalTests: true,
},
],
},
Expand Down
Loading