Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [prefer-nullish-coalescing] allowignorePrimitives option to betrue#7331

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 2 commits intotypescript-eslint:mainfromauvred:feat/7180
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 =>typeofflag=== 'number')
.reduce((previous, flag) => previous | flag, 0);
if (
type.flags !== ts.TypeFlags.Null &&
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop

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


[8]ページ先頭

©2009-2025 Movatter.jp