Skip to content
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

fix(eslint-plugin): [prefer-nullish-coalescing] handle case when type of left side is null or undefined #7225

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ export default util.createRule<Options, MessageIds>({
.filter((flag): flag is number => flag !== undefined)
.reduce((previous, flag) => previous | flag, 0);
if (
type.flags !== ts.TypeFlags.Null &&
type.flags !== ts.TypeFlags.Undefined &&
(type as ts.UnionOrIntersectionType).types.some(t =>
tsutils.isTypeFlagSet(t, ignorableFlags),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1208,5 +1208,47 @@ x || y;
},
],
},
{
code: `
declare const x: null;
x || y;
`,
errors: [
{
messageId: 'preferNullishOverOr',
},
],
},
{
code: `
const x = undefined;
x || y;
`,
errors: [
{
messageId: 'preferNullishOverOr',
},
],
},
{
code: `
null || y;
`,
errors: [
{
messageId: 'preferNullishOverOr',
},
],
},
{
code: `
undefined || y;
`,
errors: [
{
messageId: 'preferNullishOverOr',
},
],
},
],
});