Skip to content

Commit

Permalink
fix(eslint-plugin): [strict-bool-expr] fix unconstrained generic
Browse files Browse the repository at this point in the history
  • Loading branch information
phaux committed Oct 9, 2021
1 parent 87e1ef7 commit 66bcc69
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/eslint-plugin/src/rules/strict-boolean-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ export default util.createRule<Options, MessageId>({
ts.TypeFlags.StringLike |
ts.TypeFlags.NumberLike |
ts.TypeFlags.BigIntLike |
ts.TypeFlags.TypeParameter |
ts.TypeFlags.Any |
ts.TypeFlags.Unknown |
ts.TypeFlags.Never,
Expand All @@ -789,8 +790,13 @@ export default util.createRule<Options, MessageId>({
}

if (
types.some(
type => util.isTypeAnyType(type) || util.isTypeUnknownType(type),
types.some(type =>
util.isTypeFlagSet(
type,
ts.TypeFlags.TypeParameter |
ts.TypeFlags.Any |
ts.TypeFlags.Unknown,
),
)
) {
variantTypes.add('any');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,17 @@ if (y) {
declare const x: null; if (x) {}
(x: undefined) => !x;
<T extends null | undefined>(x: T) => x ? 1 : 0;
<T extends null>(x: T) => x ? 1 : 0;
<T extends undefined>(x: T) => x ? 1 : 0;
`,
errors: [
{ messageId: 'conditionErrorNullish', line: 2, column: 1 },
{ messageId: 'conditionErrorNullish', line: 3, column: 9 },
{ messageId: 'conditionErrorNullish', line: 4, column: 36 },
{ messageId: 'conditionErrorNullish', line: 5, column: 28 },
{ messageId: 'conditionErrorNullish', line: 6, column: 47 },
{ messageId: 'conditionErrorNullish', line: 7, column: 35 },
{ messageId: 'conditionErrorNullish', line: 8, column: 40 },
],
}),

Expand All @@ -316,13 +320,19 @@ if (y) {
declare const x: symbol; if (x) {}
(x: () => void) => !x;
<T extends object>(x: T) => x ? 1 : 0;
<T extends Object | Function>(x: T) => x ? 1 : 0;
<T extends { a: number }>(x: T) => x ? 1 : 0;
<T extends () => void>(x: T) => x ? 1 : 0;
`,
errors: [
{ messageId: 'conditionErrorObject', line: 2, column: 1 },
{ messageId: 'conditionErrorObject', line: 3, column: 10 },
{ messageId: 'conditionErrorObject', line: 4, column: 38 },
{ messageId: 'conditionErrorObject', line: 5, column: 29 },
{ messageId: 'conditionErrorObject', line: 6, column: 37 },
{ messageId: 'conditionErrorObject', line: 7, column: 48 },
{ messageId: 'conditionErrorObject', line: 8, column: 44 },
{ messageId: 'conditionErrorObject', line: 9, column: 41 },
],
}),

Expand Down Expand Up @@ -843,12 +853,12 @@ if (y) {
}),

// any in boolean context
// TODO: when `T` is not `extends any` then the error is `conditionErrorObject` (says it's always truthy, which is false)
...batchedSingleLineTests<MessageId, Options>({
code: noFormat`
if (x) {}
x => !x;
<T extends any>(x: T) => x ? 1 : 0;
<T>(x: T) => x ? 1 : 0;
`,
errors: [
{
Expand Down Expand Up @@ -884,6 +894,17 @@ if (y) {
},
],
},
{
messageId: 'conditionErrorAny',
line: 5,
column: 22,
suggestions: [
{
messageId: 'conditionFixCastBoolean',
output: ' <T>(x: T) => (Boolean(x)) ? 1 : 0;',
},
],
},
],
}),

Expand Down

0 comments on commit 66bcc69

Please sign in to comment.