From 64cb69e1e24d09fa23376b4b5335a2d55eb2fb55 Mon Sep 17 00:00:00 2001 From: dora <31735614+dora1998@users.noreply.github.com> Date: Sat, 15 Apr 2023 17:24:50 +0900 Subject: [PATCH 1/2] fix(eslint-plugin): [no-unnecessary-condition] allow nullish coalescing for naked type parameter --- .../src/rules/no-unnecessary-condition.ts | 10 ++++++++-- .../tests/rules/no-unnecessary-condition.test.ts | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index 6af9bc0e9e0..1d0ae09a49b 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -263,8 +263,14 @@ export default createRule({ function checkNodeForNullish(node: TSESTree.Expression): void { const type = getNodeType(node); - // Conditional is always necessary if it involves `any` or `unknown` - if (isTypeAnyType(type) || isTypeUnknownType(type)) { + + // Conditional is always necessary if it involves `any`, `unknown` or a naked type parameter + if ( + isTypeFlagSet( + type, + ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter, + ) + ) { return; } diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts index e3647102305..5418fa198a1 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -280,6 +280,11 @@ function test(a: string | null | undefined) { ` function test(a: unknown) { return a ?? 'default'; +} + `, + ` +function test(a: T) { + return a ?? 'default'; } `, // Indexing cases From a1c766c4d1d3fa76100de1c4aab3865adb59a30a Mon Sep 17 00:00:00 2001 From: dora <31735614+dora1998@users.noreply.github.com> Date: Mon, 17 Apr 2023 15:11:57 +0900 Subject: [PATCH 2/2] add tests --- .../rules/no-unnecessary-condition.test.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts index 5418fa198a1..4cb0e1604b7 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -285,6 +285,11 @@ function test(a: unknown) { ` function test(a: T) { return a ?? 'default'; +} + `, + ` +function test(a: T) { + return a ?? 'default'; } `, // Indexing cases @@ -832,6 +837,14 @@ function test(a: string) { code: ` function test(a: string | false) { return a ?? 'default'; +} + `, + errors: [ruleError(3, 10, 'neverNullish')], + }, + { + code: ` +function test(a: T) { + return a ?? 'default'; } `, errors: [ruleError(3, 10, 'neverNullish')], @@ -863,6 +876,14 @@ function test(a: null[]) { }, { code: ` +function test(a: T) { + return a ?? 'default'; +} + `, + errors: [ruleError(3, 10, 'alwaysNullish')], + }, + { + code: ` function test(a: never) { return a ?? 'default'; }