Skip to content

feat(eslint-plugin): [prefer-nullish-coalescing] fix detection of ignoreConditionalTests involving boolean ! operator #10299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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