Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [strict-bool-expr] treat unconstrained generic as any #3981

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, looks like the attached issue was already known at a point in time!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, but I had no idea how to fix it until now and just assumed it's a bug in TS

...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