-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [no-non-null-asserted-optional-chain] infinite loop problem #5367
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
Conversation
Thanks for the PR, @YeeJone! 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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Codecov Report
@@ Coverage Diff @@
## main #5367 +/- ##
==========================================
+ Coverage 91.36% 93.82% +2.46%
==========================================
Files 132 290 +158
Lines 1494 9993 +8499
Branches 226 3009 +2783
==========================================
+ Hits 1365 9376 +8011
- Misses 65 336 +271
- Partials 64 281 +217
Flags with carried forward coverage won't be shown. Click here to find out more.
|
In an ideal world, this PR would be blocked on #1752, since it adds a couple of logic branches that aren't tested right now. But getting multi-TS-version testing support is going to be pretty tricky. 🤔 |
Indeed, adding multi-TS-version testing capabilities is tricky But this is an obvious problem🐛, and I did some case verification locally with a lower version of TS, and it's fine |
Now I use this package in a project with a low version of TS, and it often gets stuck, which is too uncomfortable😣 |
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 code seems like it's pretty wrong anyways - I don't quite know how (or even if) it ever actually worked to begin with.
the intention is that the break
applies to the outer while
, but instead it's applying to the switch
.
The code should instead be like this to explicitly exit the loop.
const hasOptionalChain = (() => {
let current = child;
while (current) {
switch (current.type) {
case AST_NODE_TYPES.MemberExpression:
if (current.optional) {
// found an optional chain! stop traversing
return true;
}
current = current.object;
continue;
case AST_NODE_TYPES.CallExpression:
if (current.optional) {
// found an optional chain! stop traversing
return true;
}
current = current.callee;
continue;
default:
// something that's not a ChainElement, which means this is not an optional chain we want to check
return false;
}
}
})();
if (!hasOptionalChain) {
return;
}
this is much clearer than the existing code and it explicitly breaks from the loop rather than implicitly exiting
It has been modified, please help to review the code |
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.
Could you add some unit tests verifying the behavior please? There are a lot of "line ... was not covered by tests" comments, which are indicating we can't be sure in CI this won't break in the future.
PR Checklist
Overview
Prior to typescript 3.9, the code in the no-non-null-asserted-optional-chain rule had an infinite loop.
Causes the ESLint process to fail to end