Skip to content

Commit

Permalink
feat(eslint-plugin): [prefer-nullish-coalescing] allow `ignorePrimiti…
Browse files Browse the repository at this point in the history
…ves` option to be `true` (#7331)

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

* chore: update test snapshot
  • Loading branch information
auvred committed Aug 5, 2023
1 parent 2103c00 commit dfcafae
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 37 deletions.
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
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
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.

0 comments on commit dfcafae

Please sign in to comment.