From c2c0552ea8a32a9cbf16bc93293ac11195f3822a Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Thu, 11 Jan 2024 01:12:22 +0900 Subject: [PATCH 1/2] fix(eslint-plugin): [no-unnecessary-condition] fix false positive for type variable --- .../src/rules/no-unnecessary-condition.ts | 8 +++- .../rules/no-unnecessary-condition.test.ts | 41 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index 0a31535797d7..b5819ddb1f0f 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -271,7 +271,10 @@ export default createRule({ if ( isTypeFlagSet( type, - ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter, + ts.TypeFlags.Any | + ts.TypeFlags.Unknown | + ts.TypeFlags.TypeParameter | + ts.TypeFlags.TypeVariable, ) ) { return; @@ -345,7 +348,8 @@ export default createRule({ flag |= ts.TypeFlags.Any | ts.TypeFlags.Unknown | - ts.TypeFlags.TypeParameter; + ts.TypeFlags.TypeParameter | + ts.TypeFlags.TypeVariable; // Allow loose comparison to nullish values. if (node.operator === '==' || node.operator === '!=') { 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 7c31bf419578..85eef6ad220f 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -253,6 +253,26 @@ function test(a: T) { const t16 = undefined !== a; } `, + ` +function foo(arg: T, key: keyof T): void { + const t1 = arg[key] == null; + const t2 = null == arg[key]; + const t3 = arg[key] != null; + const t4 = null != arg[key]; + const t5 = arg[key] == undefined; + const t6 = undefined == arg[key]; + const t7 = arg[key] != undefined; + const t8 = undefined != arg[key]; + const t9 = arg[key] === null; + const t10 = null === arg[key]; + const t11 = arg[key] !== null; + const t12 = null !== arg[key]; + const t13 = arg[key] === undefined; + const t14 = undefined === arg[key]; + const t15 = arg[key] !== undefined; + const t16 = undefined !== arg[key]; +} + `, // Predicate functions ` @@ -317,6 +337,11 @@ function test(a: T) { ` function test(a: T) { return a ?? 'default'; +} + `, + ` +function foo(arg: T, key: keyof T): void { + arg[key] ?? 'default'; } `, // Indexing cases @@ -740,6 +765,13 @@ foo ||= 1; ` declare let foo: number; foo &&= 1; + `, + ` +function foo(arg: T, key: keyof T): void { + arg[key] ??= 'default'; + arg[key] ||= 'default'; + arg[key] &&= 'default'; +} `, // https://github.com/typescript-eslint/typescript-eslint/issues/6264 ` @@ -1084,7 +1116,14 @@ function test(a: never) { `, errors: [ruleError(3, 10, 'never')], }, - + { + code: ` +function test(num: T[K]) { + num ?? 'default'; +} + `, + errors: [ruleError(3, 3, 'neverNullish')], + }, // Predicate functions { code: ` From c2fbb1cfccd96cf6629f5f7e54f7e9cbee56f28b Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Sat, 13 Jan 2024 14:55:29 +0900 Subject: [PATCH 2/2] chore: simplify test cases --- .../rules/no-unnecessary-condition.test.ts | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) 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 85eef6ad220f..a144c7026c4b 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -255,22 +255,7 @@ function test(a: T) { `, ` function foo(arg: T, key: keyof T): void { - const t1 = arg[key] == null; - const t2 = null == arg[key]; - const t3 = arg[key] != null; - const t4 = null != arg[key]; - const t5 = arg[key] == undefined; - const t6 = undefined == arg[key]; - const t7 = arg[key] != undefined; - const t8 = undefined != arg[key]; - const t9 = arg[key] === null; - const t10 = null === arg[key]; - const t11 = arg[key] !== null; - const t12 = null !== arg[key]; - const t13 = arg[key] === undefined; - const t14 = undefined === arg[key]; - const t15 = arg[key] !== undefined; - const t16 = undefined !== arg[key]; + arg[key] == null; } `, @@ -769,8 +754,6 @@ foo &&= 1; ` function foo(arg: T, key: keyof T): void { arg[key] ??= 'default'; - arg[key] ||= 'default'; - arg[key] &&= 'default'; } `, // https://github.com/typescript-eslint/typescript-eslint/issues/6264 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy