From ad9ec4bcced035857390e68a29c8bbafdb4c3c37 Mon Sep 17 00:00:00 2001 From: uhyo Date: Sat, 11 Feb 2023 20:27:46 +0900 Subject: [PATCH 1/2] add new tests --- .../rules/no-unnecessary-condition.test.ts | 75 +++++++++++++++++++ 1 file changed, 75 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 6a5f803605f0..96cad00c908b 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -549,6 +549,36 @@ type OptionalFoo = Foo | undefined; declare const foo: OptionalFoo; foo?.[1]?.length; `, + // https://github.com/typescript-eslint/typescript-eslint/issues/6264 + ` +function get(obj: Obj, key: Key) { + const value = obj[key]; + if (value) { + return value; + } + throw new Error('BOOM!'); +} + +get({ foo: null }, 'foo'); + `, + { + code: ` +function getElem(dict: Record, key: string) { + if (dict[key]) { + return dict[key].foo; + } else { + return ''; + } +} + `, + parserOptions: { + tsconfigRootDir: getFixturesRootDir(), + project: './tsconfig.noUncheckedIndexedAccess.json', + }, + dependencyConstraints: { + typescript: '4.1', + }, + }, ], invalid: [ // Ensure that it's checking in all the right places @@ -1601,5 +1631,50 @@ foo?.test.length; }, ], }, + { + code: ` +function pick, Key extends keyof Obj>( + obj: Obj, + key: Key, +): Obj[Key] { + const k = obj[key]; + if (obj[key]) { + return obj[key]; + } + throw new Error('Boom!'); +} + +pick({ foo: 1, bar: 2 }, 'bar'); + `, + errors: [ + { + messageId: 'alwaysTruthy', + line: 7, + endLine: 7, + column: 7, + endColumn: 15, + }, + ], + }, + { + code: ` +function getElem(dict: Record, key: string) { + if (dict[key]) { + return dict[key].foo; + } else { + return ''; + } +} + `, + errors: [ + { + messageId: 'alwaysTruthy', + line: 3, + endLine: 3, + column: 7, + endColumn: 16, + }, + ], + }, ], }); From e8a80b7f3166ced90eb4bd851a75ce1e52a97c79 Mon Sep 17 00:00:00 2001 From: uhyo Date: Sat, 11 Feb 2023 20:37:14 +0900 Subject: [PATCH 2/2] allow naked index access type to be a condition --- packages/eslint-plugin/src/rules/no-unnecessary-condition.ts | 4 ++-- 1 file changed, 2 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 596dcf798467..4951e281af4e 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -235,13 +235,13 @@ export default createRule({ const type = getNodeType(node); // Conditional is always necessary if it involves: - // `any` or `unknown` or a naked type parameter + // `any` or `unknown` or a naked type variable if ( unionTypeParts(type).some( part => isTypeAnyType(part) || isTypeUnknownType(part) || - isTypeFlagSet(part, ts.TypeFlags.TypeParameter), + isTypeFlagSet(part, ts.TypeFlags.TypeVariable), ) ) { return; 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