-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [await-thenable] report invalid (non-promise) values passed to promise aggregator methods #11267
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for the PR, @ronami! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
View your CI Pipeline Execution ↗ for commit 99343da
☁️ Nx Cloud last updated this comment at |
Codecov Report✅ All modified and coverable lines are covered by tests. Please upload reports for the commit 99343da to get more accurate results. Additional details and impacted files@@ Coverage Diff @@
## main #11267 +/- ##
==========================================
- Coverage 90.86% 90.85% -0.01%
==========================================
Files 503 502 -1
Lines 51046 51013 -33
Branches 8418 8405 -13
==========================================
- Hits 46384 46350 -34
Misses 4648 4648
- Partials 14 15 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
It would be great if a test case for for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (tsutils.isTypeReference(part)) { | ||
const typeArguments = checker.getTypeArguments(part); | ||
|
||
// only check the first type argument of `Iterator<...>` or `Array<...>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The heuristics here seem to have minor edge case bugs.
interface MyArray<Unused, T> extends Array<T> {};
declare const arrayOfNull: MyArray<Promise<void>, null>;
declare const arrayOfPromises: MyArray<null, Promise<void>>;
Promise.all(arrayOfNull); // no report; should report
Promise.all(arrayOfPromises); // does report; shouldn't report
Note that if you switch interface
to type
, this works correctly 🧐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I didn't consider this edge case!
I took some time trying to figure this out, here are my thoughts:
-
checker.isArrayType()
doesn't flag this type as array, and I've only managed to compare this kind of value is withchecker.isArrayLikeType()
(which seems to check if the value is assignable toArray<any>
).Getting the type of the element(s) of the array seems to be a bit trickier, and may be possible through TypeScript's internal
getIterationTypesOfIterable
or similar.Unless additional APIs are exposed, the best I've managed to come up with is using
checker.isArrayLikeType()
and getting the value of the array viaarrayType.getNumberIndexType()
. This seems to work OK, though a similar case that extendsIterable
still has this issue (playground link):interface MyIterable<Unused, T> extends Iterable<T> { } declare const x: MyIterable<Promise<void>, null>; // should report but doesn't Promise.all(x);
-
This issue seems to affect additional rules, I was able to create these reproducible ones (should I open issues for them? I'm not sure how often this case gets used in the wild):
-
no-base-to-string
(link to playground):// normally reports correctly declare const x: Array<object>; `${x}`; // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfObjects: MyArray<null, object>; `${arrayOfObjects}`;
-
no-floating-promises
(link to playground):// normally reports correctly declare const x: Array<Promise<void>>; x; // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfPromises: MyArray<null, Promise<void>>; arrayOfPromises;
-
prefer-reduce-type-parameter
(link to playground):// normally reports correctly declare const x: Array<string> x.reduce( (accum, name) => ({ ...accum, [name]: true, }), {} as Record<string, boolean>, ); // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfStrings: MyArray<null, string>; arrayOfStrings.reduce( (accum, name) => ({ ...accum, [name]: true, }), {} as Record<string, boolean>, );
-
-
I updated the PR with the changes described above, would love to hear your thoughts.
Edit: I think the main
branch has failing tests which cause this PR to be red too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! I'm going to be away from a computer until beginning of August. I can look at it then, or feel free to move this along in the meantime 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always appreciate how deeply you investigate things like this, and how clear the examples you come up with are 🙏
Interesting! I didn't consider this edge case!
I took some time trying to figure this out, here are my thoughts:
checker.isArrayType()
doesn't flag this type as array, and I've only managed to compare this kind of value is withchecker.isArrayLikeType()
(which seems to check if the value is assignable toArray<any>
).
Getting the type of the element(s) of the array seems to be a bit trickier, and may be possible through TypeScript's internalgetIterationTypesOfIterable
or similar.
Unless additional APIs are exposed, the best I've managed to come up with is usingchecker.isArrayLikeType()
and getting the value of the array viaarrayType.getNumberIndexType()
. This seems to work OK, though a similar case that extendsIterable
still has this issue (playground link):interface MyIterable<Unused, T> extends Iterable<T> { } declare const x: MyIterable<Promise<void>, null>; // should report but doesn't Promise.all(x);This issue seems to affect additional rules, I was able to create these reproducible ones (should I open issues for them? I'm not sure how often this case gets used in the wild):
no-base-to-string
(link to playground):// normally reports correctly declare const x: Array<object>; `${x}`; // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfObjects: MyArray<null, object>; `${arrayOfObjects}`;
I actually think this behavior is arguably correct here. It's only with a true built-in array that the .toString()
behavior is known to subsequently call its elements' .toString()
s, whereas other "array"s/arraylikes may have different stringification. So in this case, we're reasoning based on implementation details of built-in arrays.
no-floating-promises
(link to playground):// normally reports correctly declare const x: Array<Promise<void>>; x; // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfPromises: MyArray<null, Promise<void>>; arrayOfPromises;prefer-reduce-type-parameter
(link to playground):// normally reports correctly declare const x: Array<string> x.reduce( (accum, name) => ({ ...accum, [name]: true, }), {} as Record<string, boolean>, ); // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfStrings: MyArray<null, string>; arrayOfStrings.reduce( (accum, name) => ({ ...accum, [name]: true, }), {} as Record<string, boolean>, );
With these two... It's debatable IMO, and I could be convinced either way what the "correct" behavior is. We're linting for how these arraylikes quack, rather than the exact implementation details. Though (maybe?) an argument maybe could be made that the signature of .reduce()
is an implementation detail rather than a general feature of arraylikes.
In any case I don't think there's a clear cut wrong behavior in any of these that warrants proactively filing issues (versus reacting to user feedback if anyone encounters these edge cases in the wild).
- I updated the PR with the changes described above, would love to hear your thoughts.
I think this current state looks great! I'd say this is a very good "best effort" approach at replicating the getIterationTypesOfIterable
TS API (which really would be nice to have!), and we'll find out from user feedback if the remaining edge cases really are necessary to solve. Thanks for digging so deep into this.
Edit: I think the
main
branch has failing tests which cause this PR to be red too.
Yep, should be fixed now! (after merging from main)
Co-authored-by: Kirk Waiblinger <53019676+kirkwaiblinger@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of small things, mostly around testing, but this otherwise looks great! Oh and one larger question around special-casing array literals.
return false; | ||
} | ||
|
||
function getValueTypesOfArrayLike(type: ts.Type, checker: ts.TypeChecker) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this would benefit from a return type
declare const x: unknown; | ||
Promise.all(x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a TS type error, so, not sure if we need the test. If we keep it let's add a comment that this is a type error, though.
{ | ||
code: ` | ||
declare const x: Array<unknown>; | ||
Promise.all(x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this maybe should be flagged? Debatable I suppose since you don't know x
isn't a promise. But usually, unknown
isn't a promise, I'd say.
For example, we don't flag this with no-floating-promises:
declare function returnsUnknown(): unknown;
returnsUnknown();
since unknown
doesn't "look like" a promise.
Up to your judgement!
{ | ||
code: ` | ||
declare const x: number | Array<Promise<number>>; | ||
Promise.all(x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type error
}, | ||
{ | ||
code: ` | ||
declare const x: number | [Promise<number>]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type error
|
||
{ | ||
code: ` | ||
Promise.all(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type error
}, | ||
{ | ||
code: ` | ||
Promise.all(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type error
{ | ||
code: ` | ||
declare const x: Promise<number>; | ||
Promise.all(x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(testing)
I think let's add some test cases around generators as well? Like
function* generatorOfPromises() {
yield Promise.resolve(1)
yield Promise.resolve(2)
yield Promise.resolve(3)
}
Promise.all(generatorOfPromises()); // should not report
function* generatorOfNumbers() {
yield 1;
yield 2;
yield 3;
}
Promise.all(generatorOfNumbers()); // should report
and/or using the Generator<...>
built-in type.
Also, ReadonlyArray
and friends?
Also, array literals (most likely the primary way Promise.all()
will be used:
Promise.all([Promise.resolve(1), 2, Promise.resolve(3)]);
Ideally this would be special-cased when the []
syntax is found and would report specifically on the 2 rather than on the whole array?
if (tsutils.isTypeReference(part)) { | ||
const typeArguments = checker.getTypeArguments(part); | ||
|
||
// only check the first type argument of `Iterator<...>` or `Array<...>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always appreciate how deeply you investigate things like this, and how clear the examples you come up with are 🙏
Interesting! I didn't consider this edge case!
I took some time trying to figure this out, here are my thoughts:
checker.isArrayType()
doesn't flag this type as array, and I've only managed to compare this kind of value is withchecker.isArrayLikeType()
(which seems to check if the value is assignable toArray<any>
).
Getting the type of the element(s) of the array seems to be a bit trickier, and may be possible through TypeScript's internalgetIterationTypesOfIterable
or similar.
Unless additional APIs are exposed, the best I've managed to come up with is usingchecker.isArrayLikeType()
and getting the value of the array viaarrayType.getNumberIndexType()
. This seems to work OK, though a similar case that extendsIterable
still has this issue (playground link):interface MyIterable<Unused, T> extends Iterable<T> { } declare const x: MyIterable<Promise<void>, null>; // should report but doesn't Promise.all(x);This issue seems to affect additional rules, I was able to create these reproducible ones (should I open issues for them? I'm not sure how often this case gets used in the wild):
no-base-to-string
(link to playground):// normally reports correctly declare const x: Array<object>; `${x}`; // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfObjects: MyArray<null, object>; `${arrayOfObjects}`;
I actually think this behavior is arguably correct here. It's only with a true built-in array that the .toString()
behavior is known to subsequently call its elements' .toString()
s, whereas other "array"s/arraylikes may have different stringification. So in this case, we're reasoning based on implementation details of built-in arrays.
no-floating-promises
(link to playground):// normally reports correctly declare const x: Array<Promise<void>>; x; // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfPromises: MyArray<null, Promise<void>>; arrayOfPromises;prefer-reduce-type-parameter
(link to playground):// normally reports correctly declare const x: Array<string> x.reduce( (accum, name) => ({ ...accum, [name]: true, }), {} as Record<string, boolean>, ); // should report but doesn't interface MyArray<Unused, T> extends Array<T> { }; declare const arrayOfStrings: MyArray<null, string>; arrayOfStrings.reduce( (accum, name) => ({ ...accum, [name]: true, }), {} as Record<string, boolean>, );
With these two... It's debatable IMO, and I could be convinced either way what the "correct" behavior is. We're linting for how these arraylikes quack, rather than the exact implementation details. Though (maybe?) an argument maybe could be made that the signature of .reduce()
is an implementation detail rather than a general feature of arraylikes.
In any case I don't think there's a clear cut wrong behavior in any of these that warrants proactively filing issues (versus reacting to user feedback if anyone encounters these edge cases in the wild).
- I updated the PR with the changes described above, would love to hear your thoughts.
I think this current state looks great! I'd say this is a very good "best effort" approach at replicating the getIterationTypesOfIterable
TS API (which really would be nice to have!), and we'll find out from user feedback if the remaining edge cases really are necessary to solve. Thanks for digging so deep into this.
Edit: I think the
main
branch has failing tests which cause this PR to be red too.
Yep, should be fixed now! (after merging from main)
PR Checklist
Promise.all
,Promise.allSettled
,Promise.race
) #1804Overview
This PR tackles #1804 and adjusts the rule to report on invalid (non-promise) input passed to promise aggregator methods (
Promise.all
,Promise.race
,Promise.allSettled
, andPromise.any
):