Skip to content

Commit 9b29ca7

Browse files
authored
fix(eslint-plugin): [strict-bool-expr] treat unconstrained generic as any (#3981)
1 parent cd08f7a commit 9b29ca7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/eslint-plugin/src/rules/strict-boolean-expressions.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ export default util.createRule<Options, MessageId>({
779779
ts.TypeFlags.StringLike |
780780
ts.TypeFlags.NumberLike |
781781
ts.TypeFlags.BigIntLike |
782+
ts.TypeFlags.TypeParameter |
782783
ts.TypeFlags.Any |
783784
ts.TypeFlags.Unknown |
784785
ts.TypeFlags.Never,
@@ -789,8 +790,13 @@ export default util.createRule<Options, MessageId>({
789790
}
790791

791792
if (
792-
types.some(
793-
type => util.isTypeAnyType(type) || util.isTypeUnknownType(type),
793+
types.some(type =>
794+
util.isTypeFlagSet(
795+
type,
796+
ts.TypeFlags.TypeParameter |
797+
ts.TypeFlags.Any |
798+
ts.TypeFlags.Unknown,
799+
),
794800
)
795801
) {
796802
variantTypes.add('any');

packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,17 @@ if (y) {
298298
declare const x: null; if (x) {}
299299
(x: undefined) => !x;
300300
<T extends null | undefined>(x: T) => x ? 1 : 0;
301+
<T extends null>(x: T) => x ? 1 : 0;
302+
<T extends undefined>(x: T) => x ? 1 : 0;
301303
`,
302304
errors: [
303305
{ messageId: 'conditionErrorNullish', line: 2, column: 1 },
304306
{ messageId: 'conditionErrorNullish', line: 3, column: 9 },
305307
{ messageId: 'conditionErrorNullish', line: 4, column: 36 },
306308
{ messageId: 'conditionErrorNullish', line: 5, column: 28 },
307309
{ messageId: 'conditionErrorNullish', line: 6, column: 47 },
310+
{ messageId: 'conditionErrorNullish', line: 7, column: 35 },
311+
{ messageId: 'conditionErrorNullish', line: 8, column: 40 },
308312
],
309313
}),
310314

@@ -316,13 +320,19 @@ if (y) {
316320
declare const x: symbol; if (x) {}
317321
(x: () => void) => !x;
318322
<T extends object>(x: T) => x ? 1 : 0;
323+
<T extends Object | Function>(x: T) => x ? 1 : 0;
324+
<T extends { a: number }>(x: T) => x ? 1 : 0;
325+
<T extends () => void>(x: T) => x ? 1 : 0;
319326
`,
320327
errors: [
321328
{ messageId: 'conditionErrorObject', line: 2, column: 1 },
322329
{ messageId: 'conditionErrorObject', line: 3, column: 10 },
323330
{ messageId: 'conditionErrorObject', line: 4, column: 38 },
324331
{ messageId: 'conditionErrorObject', line: 5, column: 29 },
325332
{ messageId: 'conditionErrorObject', line: 6, column: 37 },
333+
{ messageId: 'conditionErrorObject', line: 7, column: 48 },
334+
{ messageId: 'conditionErrorObject', line: 8, column: 44 },
335+
{ messageId: 'conditionErrorObject', line: 9, column: 41 },
326336
],
327337
}),
328338

@@ -843,12 +853,12 @@ if (y) {
843853
}),
844854

845855
// any in boolean context
846-
// TODO: when `T` is not `extends any` then the error is `conditionErrorObject` (says it's always truthy, which is false)
847856
...batchedSingleLineTests<MessageId, Options>({
848857
code: noFormat`
849858
if (x) {}
850859
x => !x;
851860
<T extends any>(x: T) => x ? 1 : 0;
861+
<T>(x: T) => x ? 1 : 0;
852862
`,
853863
errors: [
854864
{
@@ -884,6 +894,17 @@ if (y) {
884894
},
885895
],
886896
},
897+
{
898+
messageId: 'conditionErrorAny',
899+
line: 5,
900+
column: 22,
901+
suggestions: [
902+
{
903+
messageId: 'conditionFixCastBoolean',
904+
output: ' <T>(x: T) => (Boolean(x)) ? 1 : 0;',
905+
},
906+
],
907+
},
887908
],
888909
}),
889910

0 commit comments

Comments
 (0)