diff --git a/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md b/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md index 451f7dff1d45..b0d9ac9b6c92 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 f9a953f501cf..d4f790770a51 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 9ba8d2dd34f2..334754454f1c 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 622ae8649944..2a4fd6b7550e 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; }, ];