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] treat enums and literals as their underlying primitive types #9376

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,13 @@ export default createRule<Options, MessageIds>({
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const ignorableFlags = [
(ignorePrimitives === true || ignorePrimitives!.bigint) &&
ts.TypeFlags.BigInt,
ts.TypeFlags.BigIntLike,
(ignorePrimitives === true || ignorePrimitives!.boolean) &&
ts.TypeFlags.BooleanLiteral,
ts.TypeFlags.BooleanLike,
(ignorePrimitives === true || ignorePrimitives!.number) &&
ts.TypeFlags.Number,
ts.TypeFlags.NumberLike,
(ignorePrimitives === true || ignorePrimitives!.string) &&
ts.TypeFlags.String,
ts.TypeFlags.StringLike,
]
.filter((flag): flag is number => typeof flag === 'number')
.reduce((previous, flag) => previous | flag, 0);
Expand Down
172 changes: 96 additions & 76 deletions packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,102 @@ x || y;
declare const y: number;
x || y;
`,
{
code: `
declare const x: 0 | 1 | 0n | 1n | undefined;
x || y;
`,
options: [
{
ignorePrimitives: {
string: true,
number: false,
boolean: true,
bigint: true,
},
},
],
},
{
code: `
declare const x: 0 | 1 | 0n | 1n | undefined;
x || y;
`,
options: [
{
ignorePrimitives: {
string: true,
number: true,
boolean: true,
bigint: false,
},
},
],
},
{
code: `
declare const x: 0 | 'foo' | undefined;
x || y;
`,
options: [
{
ignorePrimitives: {
number: true,
string: true,
},
},
],
},
{
code: `
declare const x: 0 | 'foo' | undefined;
x || y;
`,
options: [
{
ignorePrimitives: {
number: true,
string: false,
},
},
],
},
{
code: `
enum Enum {
A = 0,
B = 1,
C = 2,
}
declare const x: Enum | undefined;
x || y;
`,
options: [
{
ignorePrimitives: {
number: true,
},
},
],
},
{
code: `
enum Enum {
A = 0,
B = 1,
C = 2,
}
declare const x: Enum.A | Enum.B | undefined;
x || y;
`,
options: [
{
ignorePrimitives: {
number: true,
},
},
],
},
],
invalid: [
...nullishTypeInvalidTest((nullish, type) => ({
Expand Down Expand Up @@ -1145,42 +1241,6 @@ x || y;
{
code: `
declare const x: 0 | 1 | 0n | 1n | undefined;
x || y;
`,
output: null,
options: [
{
ignorePrimitives: {
string: true,
number: false,
boolean: true,
bigint: true,
},
},
],
errors: [{ messageId: 'preferNullishOverOr' }],
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these tests to have been wrong originally; this says "ignore bigint", and there's a bigint in the union, but the union is not ignored. Now moved up to "valid" (with the other similar tests).

{
code: `
declare const x: 0 | 1 | 0n | 1n | undefined;
x || y;
`,
output: null,
options: [
{
ignorePrimitives: {
string: true,
number: true,
boolean: true,
bigint: false,
},
},
],
errors: [{ messageId: 'preferNullishOverOr' }],
},
{
code: `
declare const x: 0 | 1 | 0n | 1n | undefined;
x || y;
`,
output: null,
Expand Down Expand Up @@ -1216,46 +1276,6 @@ x || y;
},
{
code: `
declare const x: 0 | 'foo' | undefined;
x || y;
`,
output: null,
options: [
{
ignorePrimitives: {
number: true,
string: true,
},
},
],
errors: [
{
messageId: 'preferNullishOverOr',
},
],
},
{
code: `
declare const x: 0 | 'foo' | undefined;
x || y;
`,
output: null,
options: [
{
ignorePrimitives: {
number: true,
string: false,
},
},
],
errors: [
{
messageId: 'preferNullishOverOr',
},
],
},
{
code: `
declare const x: null;
x || y;
`,
Expand Down
Loading