Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-nullish-coalescing] handle case when type…
Browse files Browse the repository at this point in the history
… of left side is null or undefined (#7225)
  • Loading branch information
auvred committed Jul 17, 2023
1 parent 0fef2e3 commit b62affe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
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
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',
},
],
},
],
});

0 comments on commit b62affe

Please sign in to comment.