-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [prefer-nullish-coalescing] add ignorePrimitives
option
#6487
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
Changes from all commits
b427e6b
8e016d3
09f207a
24fb9c5
7aec9fa
ed7d525
6954d62
6be0360
a576095
8dd233f
25a0852
e66ee9b
5c4ab38
3b1e012
2e579a6
e38a2a9
7d30fe5
7907cf5
40c1839
d085f34
2dff212
bea946b
6d4aa01
357fd59
d3f6ee0
e34fb0a
419a383
81ada98
53bbfba
d1b0347
ef29982
3747796
4b6c03f
7207e05
5a18ee3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,29 @@ a ?? (b && c && d); | |
|
||
**_NOTE:_** Errors for this specific case will be presented as suggestions (see below), instead of fixes. This is because it is not always safe to automatically convert `||` to `??` within a mixed logical expression, as we cannot tell the intended precedence of the operator. Note that by design, `??` requires parentheses when used with `&&` or `||` in the same expression. | ||
|
||
### `ignorePrimitives` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Docs] Could you add some examples please? We generally try to -at least for newly added rule options- so users can see exactly how the code looks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to look at the style for examples with options, this rule seems unique in how the examples are presented but decided to keep it the same since I've noticed bradzacher did the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heh yeah we'll eventually clean all these up... eventually. Thanks! |
||
|
||
If you would like to ignore certain primitive types that can be falsy then you may pass an object containing a boolean value for each primitive: | ||
|
||
- `string: true`, ignores `null` or `undefined` unions with `string` (default: false). | ||
- `number: true`, ignores `null` or `undefined` unions with `number` (default: false). | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `bigint: true`, ignores `null` or `undefined` unions with `bigint` (default: false). | ||
- `boolean: true`, ignores `null` or `undefined` unions with `boolean` (default: false). | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Incorrect code for `ignorePrimitives: { string: true }`, and correct code for `ignorePrimitives: { string: false }`: | ||
|
||
```ts | ||
const foo: string | undefined = 'bar'; | ||
foo || 'a string'; | ||
``` | ||
|
||
Correct code for `ignorePrimitives: { string: true }`: | ||
|
||
```ts | ||
const foo: string | undefined = 'bar'; | ||
foo ?? 'a string'; | ||
``` | ||
|
||
## 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,16 @@ import * as util from '../util'; | |
|
||
export type Options = [ | ||
{ | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean; | ||
ignoreConditionalTests?: boolean; | ||
ignoreTernaryTests?: boolean; | ||
ignoreMixedLogicalExpressions?: boolean; | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean; | ||
ignorePrimitives?: { | ||
bigint?: boolean; | ||
boolean?: boolean; | ||
number?: boolean; | ||
string?: boolean; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Non-actionable] In theory we could allow this to be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, leaving it for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I need to open an issue for it or can I reference the same one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a new issue for tracking. It gets confusing having multiple PRs target the same issue. If you don't have the time to file a new issue, no worries (I do hate asking folks to fill out paperwork). I'll set a reminder for a couple days from now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #7180 ✨ |
||
ignoreTernaryTests?: boolean; | ||
}, | ||
]; | ||
|
||
|
@@ -44,16 +50,25 @@ export default util.createRule<Options, MessageIds>({ | |
{ | ||
type: 'object', | ||
properties: { | ||
ignoreConditionalTests: { | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: { | ||
type: 'boolean', | ||
}, | ||
ignoreTernaryTests: { | ||
ignoreConditionalTests: { | ||
type: 'boolean', | ||
}, | ||
ignoreMixedLogicalExpressions: { | ||
type: 'boolean', | ||
}, | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: { | ||
ignorePrimitives: { | ||
type: 'object', | ||
properties: { | ||
bigint: { type: 'boolean' }, | ||
boolean: { type: 'boolean' }, | ||
number: { type: 'boolean' }, | ||
string: { type: 'boolean' }, | ||
}, | ||
}, | ||
ignoreTernaryTests: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
|
@@ -63,20 +78,27 @@ export default util.createRule<Options, MessageIds>({ | |
}, | ||
defaultOptions: [ | ||
{ | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false, | ||
ignoreConditionalTests: true, | ||
ignoreTernaryTests: true, | ||
ignoreMixedLogicalExpressions: true, | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false, | ||
ignorePrimitives: { | ||
bigint: false, | ||
boolean: false, | ||
number: false, | ||
string: false, | ||
}, | ||
}, | ||
], | ||
create( | ||
context, | ||
[ | ||
{ | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing, | ||
ignoreConditionalTests, | ||
ignoreTernaryTests, | ||
ignoreMixedLogicalExpressions, | ||
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing, | ||
ignorePrimitives, | ||
ignoreTernaryTests, | ||
}, | ||
], | ||
) { | ||
|
@@ -279,6 +301,22 @@ export default util.createRule<Options, MessageIds>({ | |
return; | ||
} | ||
|
||
const ignorableFlags = [ | ||
ignorePrimitives!.bigint && ts.TypeFlags.BigInt, | ||
ignorePrimitives!.boolean && ts.TypeFlags.BooleanLiteral, | ||
ignorePrimitives!.number && ts.TypeFlags.Number, | ||
ignorePrimitives!.string && ts.TypeFlags.String, | ||
] | ||
.filter((flag): flag is number => flag !== undefined) | ||
.reduce((previous, flag) => previous | flag, 0); | ||
if ( | ||
(type as ts.UnionOrIntersectionType).types.some(t => | ||
tsutils.isTypeFlagSet(t, ignorableFlags), | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
const barBarOperator = util.nullThrows( | ||
sourceCode.getTokenAfter( | ||
node.left, | ||
|
Uh oh!
There was an error while loading. Please reload this page.