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..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 @@ -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,28 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, { `, options: [{ allowComparingNullableBooleansToTrue: false }], }, + { + code: ` + const test: ( + someCondition: T, + ) => void = someCondition => { + if (someCondition === true) { + } + }; + `, + options: [{ allowComparingNullableBooleansToFalse: false }], + }, + { + code: ` + const test: ( + someCondition: T, + ) => void = someCondition => { + if (someCondition === false) { + } + }; + `, + options: [{ allowComparingNullableBooleansToTrue: false }], + }, "'false' === true;", "'true' === false;", ], @@ -481,5 +515,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)) { + } + }; + `, + }, ], }); 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