Skip to content

Commit eaae68c

Browse files
authored
fix(eslint-plugin): [no-unnecessary-boolean-literal-compare] flag values of a type parameter with boolean type constraints (#10474)
* [no-unnecessary-boolean-literal-compare] lag values of a type parameter with boolean type constraints * fix redundant tests
1 parent bddb35b commit eaae68c

2 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎packages/eslint-plugin/src/rules/no-unnecessary-boolean-literal-compare.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
44
import * as tsutils from 'ts-api-utils';
55
import * as ts from 'typescript';
66

7-
import { createRule, getParserServices, isStrongPrecedenceNode } from '../util';
7+
import {
8+
createRule,
9+
getConstrainedTypeAtLocation,
10+
getParserServices,
11+
isStrongPrecedenceNode,
12+
} from '../util';
813

914
type MessageIds =
1015
| 'comparingNullableToFalse'
@@ -89,7 +94,10 @@ export default createRule<Options, MessageIds>({
8994
return undefined;
9095
}
9196

92-
const expressionType = services.getTypeAtLocation(comparison.expression);
97+
const expressionType = getConstrainedTypeAtLocation(
98+
services,
99+
comparison.expression,
100+
);
93101

94102
if (isBooleanType(expressionType)) {
95103
return {

‎packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, {
5555
declare const varTrueOrStringOrUndefined: true | string | undefined;
5656
varTrueOrStringOrUndefined == true;
5757
`,
58+
`
59+
const test: <T>(someCondition: T) => void = someCondition => {
60+
if (someCondition === true) {
61+
}
62+
};
63+
`,
64+
`
65+
const test: <T>(someCondition: boolean | string) => void = someCondition => {
66+
if (someCondition === true) {
67+
}
68+
};
69+
`,
5870
`
5971
declare const varBooleanOrUndefined: boolean | undefined;
6072
varBooleanOrUndefined === true;
@@ -73,6 +85,28 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, {
7385
`,
7486
options: [{ allowComparingNullableBooleansToTrue: false }],
7587
},
88+
{
89+
code: `
90+
const test: <T extends boolean | undefined>(
91+
someCondition: T,
92+
) => void = someCondition => {
93+
if (someCondition === true) {
94+
}
95+
};
96+
`,
97+
options: [{ allowComparingNullableBooleansToFalse: false }],
98+
},
99+
{
100+
code: `
101+
const test: <T extends boolean | undefined>(
102+
someCondition: T,
103+
) => void = someCondition => {
104+
if (someCondition === false) {
105+
}
106+
};
107+
`,
108+
options: [{ allowComparingNullableBooleansToTrue: false }],
109+
},
76110
"'false' === true;",
77111
"'true' === false;",
78112
],
@@ -481,5 +515,62 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, {
481515
}
482516
`,
483517
},
518+
{
519+
code: `
520+
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
521+
if (someCondition === true) {
522+
}
523+
};
524+
`,
525+
errors: [
526+
{
527+
messageId: 'direct',
528+
},
529+
],
530+
output: `
531+
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
532+
if (someCondition) {
533+
}
534+
};
535+
`,
536+
},
537+
{
538+
code: `
539+
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
540+
if (!(someCondition !== false)) {
541+
}
542+
};
543+
`,
544+
errors: [
545+
{
546+
messageId: 'negated',
547+
},
548+
],
549+
output: `
550+
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
551+
if (!someCondition) {
552+
}
553+
};
554+
`,
555+
},
556+
{
557+
code: `
558+
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
559+
if (!((someCondition ?? true) !== false)) {
560+
}
561+
};
562+
`,
563+
errors: [
564+
{
565+
messageId: 'negated',
566+
},
567+
],
568+
output: `
569+
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
570+
if (!(someCondition ?? true)) {
571+
}
572+
};
573+
`,
574+
},
484575
],
485576
});

0 commit comments

Comments
 (0)