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

feat(eslint-plugin): [prefer-nullish-coalescing] allow ignorePrimitives option to be true #7331

Merged
merged 2 commits into from
Aug 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ const foo: string | undefined = 'bar';
foo ?? 'a string';
```

Also, if you would like to ignore all primitives types, you can set `ignorePrimitives: true`. It would be equivalent to `ignorePrimitives: { string: true, number: true, bigint: true, boolean: true }`.

## When Not To Use It

If you are not using TypeScript 3.7 (or greater), then you will not be able to use this rule, as the operator is not supported.
Expand Down
50 changes: 32 additions & 18 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export type Options = [
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
ignoreConditionalTests?: boolean;
ignoreMixedLogicalExpressions?: boolean;
ignorePrimitives?: {
bigint?: boolean;
boolean?: boolean;
number?: boolean;
string?: boolean;
};
ignorePrimitives?:
| {
bigint?: boolean;
boolean?: boolean;
number?: boolean;
string?: boolean;
}
| true;
ignoreTernaryTests?: boolean;
},
];
Expand Down Expand Up @@ -60,13 +62,21 @@ export default util.createRule<Options, MessageIds>({
type: 'boolean',
},
ignorePrimitives: {
type: 'object',
properties: {
bigint: { type: 'boolean' },
boolean: { type: 'boolean' },
number: { type: 'boolean' },
string: { type: 'boolean' },
},
oneOf: [
{
type: 'object',
properties: {
bigint: { type: 'boolean' },
boolean: { type: 'boolean' },
number: { type: 'boolean' },
string: { type: 'boolean' },
},
},
{
type: 'boolean',
enum: [true],
},
],
},
ignoreTernaryTests: {
type: 'boolean',
Expand Down Expand Up @@ -302,12 +312,16 @@ export default util.createRule<Options, MessageIds>({
}

const ignorableFlags = [
ignorePrimitives!.bigint && ts.TypeFlags.BigInt,
ignorePrimitives!.boolean && ts.TypeFlags.BooleanLiteral,
ignorePrimitives!.number && ts.TypeFlags.Number,
ignorePrimitives!.string && ts.TypeFlags.String,
(ignorePrimitives === true || ignorePrimitives!.bigint) &&
ts.TypeFlags.BigInt,
(ignorePrimitives === true || ignorePrimitives!.boolean) &&
ts.TypeFlags.BooleanLiteral,
(ignorePrimitives === true || ignorePrimitives!.number) &&
ts.TypeFlags.Number,
(ignorePrimitives === true || ignorePrimitives!.string) &&
ts.TypeFlags.String,
]
.filter((flag): flag is number => flag !== undefined)
.filter((flag): flag is number => typeof flag === 'number')
.reduce((previous, flag) => previous | flag, 0);
if (
type.flags !== ts.TypeFlags.Null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ x || y;
`,
options: [{ ignorePrimitives: { [type]: true } }],
})),
...ignorablePrimitiveTypes.map<TSESLint.ValidTestCase<Options>>(type => ({
code: `
declare const x: ${type} | undefined;
x || y;
`,
options: [{ ignorePrimitives: true }],
})),
],
invalid: [
...nullishTypeInvalidTest((nullish, type) => ({
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.