From dfcafae515e7f4d1ae69387eb163200e455dd0ce Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:24:43 +0300 Subject: [PATCH] feat(eslint-plugin): [prefer-nullish-coalescing] allow `ignorePrimitives` option to be `true` (#7331) * feat(eslint-plugin): [prefer-nullish-coalescing] allow `ignorePrimitives` option to be `true` * chore: update test snapshot --- .../docs/rules/prefer-nullish-coalescing.md | 2 + .../src/rules/prefer-nullish-coalescing.ts | 50 ++++++++++++------- .../rules/prefer-nullish-coalescing.test.ts | 7 +++ .../prefer-nullish-coalescing.shot | 48 +++++++++++------- 4 files changed, 70 insertions(+), 37 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md b/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md index 451f7dff1d4..b0d9ac9b6c9 100644 --- a/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md +++ b/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md @@ -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. diff --git a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts index f9a953f501c..d4f790770a5 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -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; }, ]; @@ -60,13 +62,21 @@ export default util.createRule({ 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', @@ -302,12 +312,16 @@ export default util.createRule({ } 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 && diff --git a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts index 9ba8d2dd34f..334754454f1 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -215,6 +215,13 @@ x || y; `, options: [{ ignorePrimitives: { [type]: true } }], })), + ...ignorablePrimitiveTypes.map>(type => ({ + code: ` +declare const x: ${type} | undefined; +x || y; + `, + options: [{ ignorePrimitives: true }], + })), ], invalid: [ ...nullishTypeInvalidTest((nullish, type) => ({ diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-nullish-coalescing.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-nullish-coalescing.shot index 622ae864994..2a4fd6b7550 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/prefer-nullish-coalescing.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-nullish-coalescing.shot @@ -18,21 +18,29 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos "type": "boolean" }, "ignorePrimitives": { - "properties": { - "bigint": { - "type": "boolean" - }, - "boolean": { - "type": "boolean" - }, - "number": { - "type": "boolean" + "oneOf": [ + { + "properties": { + "bigint": { + "type": "boolean" + }, + "boolean": { + "type": "boolean" + }, + "number": { + "type": "boolean" + }, + "string": { + "type": "boolean" + } + }, + "type": "object" }, - "string": { + { + "enum": [true], "type": "boolean" } - }, - "type": "object" + ] }, "ignoreTernaryTests": { "type": "boolean" @@ -50,13 +58,15 @@ type Options = [ allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean; ignoreConditionalTests?: boolean; ignoreMixedLogicalExpressions?: boolean; - ignorePrimitives?: { - bigint?: boolean; - boolean?: boolean; - number?: boolean; - string?: boolean; - [k: string]: unknown; - }; + ignorePrimitives?: + | { + bigint?: boolean; + boolean?: boolean; + number?: boolean; + string?: boolean; + [k: string]: unknown; + } + | true; ignoreTernaryTests?: boolean; }, ];