From 1937257e1f048c446dfeafd4b356bcbc9a13e964 Mon Sep 17 00:00:00 2001 From: Ronen Amiel Date: Sat, 7 Dec 2024 20:48:25 +0200 Subject: [PATCH 1/2] [no-unnecessary-boolean-literal-compare] lag values of a type parameter with boolean type constraints --- .../no-unnecessary-boolean-literal-compare.ts | 12 ++- ...nnecessary-boolean-literal-compare.test.ts | 87 +++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-boolean-literal-compare.ts b/packages/eslint-plugin/src/rules/no-unnecessary-boolean-literal-compare.ts index 9a0c38c47a80..db96ef6f2a8f 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-boolean-literal-compare.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-boolean-literal-compare.ts @@ -4,7 +4,12 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import * as tsutils from 'ts-api-utils'; import * as ts from 'typescript'; -import { createRule, getParserServices, isStrongPrecedenceNode } from '../util'; +import { + createRule, + getConstrainedTypeAtLocation, + getParserServices, + isStrongPrecedenceNode, +} from '../util'; type MessageIds = | 'comparingNullableToFalse' @@ -89,7 +94,10 @@ export default createRule({ return undefined; } - const expressionType = services.getTypeAtLocation(comparison.expression); + const expressionType = getConstrainedTypeAtLocation( + services, + comparison.expression, + ); if (isBooleanType(expressionType)) { return { diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts index 331212829ca3..a7f4724322d6 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts @@ -55,6 +55,18 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, { declare const varTrueOrStringOrUndefined: true | string | undefined; varTrueOrStringOrUndefined == true; `, + ` + const test: (someCondition: T) => void = someCondition => { + if (someCondition === true) { + } + }; + `, + ` + const test: (someCondition: boolean | string) => void = someCondition => { + if (someCondition === true) { + } + }; + `, ` declare const varBooleanOrUndefined: boolean | undefined; varBooleanOrUndefined === true; @@ -73,6 +85,24 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, { `, options: [{ allowComparingNullableBooleansToTrue: false }], }, + { + code: ` + const test: (someCondition: boolean | undefined) => void = someCondition => { + if (someCondition === true) { + } + }; + `, + options: [{ allowComparingNullableBooleansToFalse: false }], + }, + { + code: ` + const test: (someCondition: boolean | undefined) => void = someCondition => { + if (someCondition === false) { + } + }; + `, + options: [{ allowComparingNullableBooleansToTrue: false }], + }, "'false' === true;", "'true' === false;", ], @@ -481,5 +511,62 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, { } `, }, + { + code: ` + const test: (someCondition: T) => void = someCondition => { + if (someCondition === true) { + } + }; + `, + errors: [ + { + messageId: 'direct', + }, + ], + output: ` + const test: (someCondition: T) => void = someCondition => { + if (someCondition) { + } + }; + `, + }, + { + code: ` + const test: (someCondition: T) => void = someCondition => { + if (!(someCondition !== false)) { + } + }; + `, + errors: [ + { + messageId: 'negated', + }, + ], + output: ` + const test: (someCondition: T) => void = someCondition => { + if (!someCondition) { + } + }; + `, + }, + { + code: ` + const test: (someCondition: T) => void = someCondition => { + if (!((someCondition ?? true) !== false)) { + } + }; + `, + errors: [ + { + messageId: 'negated', + }, + ], + output: ` + const test: (someCondition: T) => void = someCondition => { + if (!(someCondition ?? true)) { + } + }; + `, + }, ], }); From 60e677a180fa3ea849d71a1825ec396780585038 Mon Sep 17 00:00:00 2001 From: Ronen Amiel Date: Sat, 14 Dec 2024 09:57:02 +0200 Subject: [PATCH 2/2] fix redundant tests --- .../rules/no-unnecessary-boolean-literal-compare.test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts index a7f4724322d6..f99a78ff9df6 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts @@ -87,7 +87,9 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, { }, { code: ` - const test: (someCondition: boolean | undefined) => void = someCondition => { + const test: ( + someCondition: T, + ) => void = someCondition => { if (someCondition === true) { } }; @@ -96,7 +98,9 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, { }, { code: ` - const test: (someCondition: boolean | undefined) => void = someCondition => { + const test: ( + someCondition: T, + ) => void = someCondition => { if (someCondition === false) { } }; 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