-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [prefer-optional-chain] should report case that can be converted to optional void function call ?.()
#11272
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, @nayounsang! 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 791461b.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #11272 +/- ##
=======================================
Coverage 90.91% 90.91%
=======================================
Files 501 501
Lines 50869 50878 +9
Branches 8382 8382
=======================================
+ Hits 46248 46257 +9
Misses 4606 4606
Partials 15 15
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
?.()
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 generally looks great! Just one big question on the implementation approach we want to take. Looking forward to hearing your thoughts!
if (options.checkBigInt === true) { | ||
allowedFlags |= ts.TypeFlags.BigIntLike; | ||
} | ||
|
||
if (options.checkVoid === true) { | ||
allowedFlags |= ts.TypeFlags.Void; | ||
} | ||
|
||
return types.every(t => isTypeFlagSet(t, allowedFlags)); | ||
} |
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'm wondering if this section should be flipped to only specify the flags that aren't checked instead of the ones that are?
For now, no tests fail if I switch the logic to the following, where we don't bother with explicit options for or handling of void
:
let flagsToExcludeFromCheck = 0;
if (options.checkAny !== true) {
flagsToExcludeFromCheck |= ts.TypeFlags.Any;
}
if (options.checkUnknown !== true) {
flagsToExcludeFromCheck |= ts.TypeFlags.Unknown;
}
if (options.checkString !== true) {
flagsToExcludeFromCheck |= ts.TypeFlags.StringLike;
}
if (options.checkNumber !== true) {
flagsToExcludeFromCheck |= ts.TypeFlags.NumberLike;
}
if (options.checkBoolean !== true) {
flagsToExcludeFromCheck |= ts.TypeFlags.BooleanLike;
}
if (options.checkBigInt !== true) {
flagsToExcludeFromCheck |= ts.TypeFlags.BigIntLike;
}
return !types.some(t => isTypeFlagSet(t, flagsToExcludeFromCheck));
Can we come up with some test cases that would
- demonstrate which approach is preferable
- solidify that approach rather than passing with either choice of logic
I haven't thought deeply about which logic makes more sense, so it would be lovely if you have time to give this some thought and justify with examples which way is preferable. Thanks!
For reference, I ask this because:
- prefer-nullish-coalescing does have the "only ignore specific types" approach, rather than "only check some hardcoded types plus allowed primitives" as in the existing implementation of this rule.
- the answer will determine whether creation of a new option is warranted
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 will think about it more as I work, but I will leave a quick comment first. The more immediate the communication, the better. Even if it's not accurate!
- I would recommend adding
void
options. My philosophy is that the more open source users can customize with options, the more use cases users can cover. (Adding options would likely require some additional documentation changes beyond the current commit.) - Since the defaults are given as true, it seems more natural to change to the suggested code. Even better, it prevents optional chaining from being skipped on unexpected types.
- I guess I need to consider side effects when
flagsToExcludeFromCheck = 0
.
let allowedFlags = NULLISH_FLAGS | ts.TypeFlags.Object;
All tests will pass if flagsToExcludeFromCheck = 0
for now and I don't think there's a problem at all.(Due to 2), but something feels off because allowedFlags
has default value...
Perhaps the test case would be to determine what error/suggestion occurs depending on whether the option is turned off.
PR Checklist
?.()
#11270Overview
prefer-optional-chain
checkVoid