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: constructor-super
false positives with loops
#18226
Conversation
✅ Deploy Preview for docs-eslint canceled.
|
} else if (isAnySegmentReachable(funcInfo.currentSegments)) { | ||
context.report({ | ||
messageId: "unexpected", | ||
node | ||
}); | ||
} |
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 was intended to report super()
calls in classes that don't have extends
. But such calls already cause parsing errors so that's not something a rule can catch.
segment => { | ||
const info = segInfoMap[segment.id] ?? new SegmentInfo(); | ||
(segment, controller) => { | ||
const info = segInfoMap[segment.id]; | ||
|
||
// skip segments after the loop | ||
if (!info) { | ||
controller.skip(); | ||
return; | ||
} |
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.
There's no need to traverse segments after the loop here.
/** | ||
* Resets state. | ||
* @returns {void} | ||
*/ | ||
"Program:exit"() { | ||
segInfoMap = Object.create(null); | ||
} |
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 don't think there was any benefit from this.
const seenPrevSegments = segment.prevSegments.filter(hasSegmentBeenSeen); | ||
const calledInSomePreviousPaths = seenPrevSegments.some(isCalledInSomePath); | ||
const calledInEveryPreviousPaths = seenPrevSegments.every(isCalledInEveryPath); | ||
|
||
// Updates flags. | ||
info.calledInSomePaths = seenPrevSegments.some(isCalledInSomePath); | ||
info.calledInEveryPaths = seenPrevSegments.every(isCalledInEveryPath); | ||
info.calledInSomePaths ||= calledInSomePreviousPaths; | ||
info.calledInEveryPaths ||= calledInEveryPreviousPaths; | ||
|
||
// If flags become true anew, reports the valid nodes. | ||
if (info.calledInSomePaths || isRealLoop) { | ||
if (calledInSomePreviousPaths) { |
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 that updates segments in loops was causing false positives. It didn't account for the case that the segment could have had a super()
call inside the segment itself.
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.
LGTM. Thanks for digging into this.
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
Tell us about your environment (
npx eslint --env-info
):What parser are you using (place an "X" next to just one item)?
[x]
Default (Espree)
[ ]
@typescript-eslint/parser
[ ]
@babel/eslint-parser
[ ]
vue-eslint-parser
[ ]
@angular-eslint/template-parser
[ ]
Other
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue.
What did you expect to happen?
No errors as
super()
is called in all paths.What actually happened? Please include the actual, raw output from ESLint.
What changes did you make? (Give an overview)
Fixed the problem in the code that handles loops. Also refactored some other parts of this rule. I'll leave comments on the code.
Is there anything you'd like reviewers to focus on?