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: constructor-super false positives with loops #18226

Merged
merged 1 commit into from Mar 25, 2024

Conversation

mdjermanovic
Copy link
Member

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):

  • Node version: 20.9.0
  • npm version: 10.2.0
  • Local ESLint version: v9.0.0-rc.0
  • Global ESLint version: no
  • Operating System: windows

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
module.exports = [];

What did you do? Please include the actual source code causing the issue.

/* eslint constructor-super: "error" */

class A extends B {
    constructor(list) {
        for (const a of list) {
            if (a.foo) {
                super(a);
                return;
            }
        }
        super();
    }
}

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.

  4:5   error  Lacked a call of 'super()' in some code paths  constructor-super
  7:17  error  Unexpected duplicate 'super()'                 constructor-super

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?

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Mar 23, 2024
@mdjermanovic mdjermanovic requested a review from a team as a code owner March 23, 2024 20:50
Copy link

netlify bot commented Mar 23, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit f912b06
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65ff407cb706300008c8f9a7

Comment on lines -431 to 436
} else if (isAnySegmentReachable(funcInfo.currentSegments)) {
context.report({
messageId: "unexpected",
node
});
}
Copy link
Member Author

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.

Comment on lines -355 to +341
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;
}
Copy link
Member Author

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.

Comment on lines -467 to 442
/**
* Resets state.
* @returns {void}
*/
"Program:exit"() {
segInfoMap = Object.create(null);
}
Copy link
Member Author

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.

Comment on lines 343 to +351
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) {
Copy link
Member Author

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.

Copy link
Member

@nzakas nzakas left a 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.

@nzakas nzakas merged commit dadc5bf into main Mar 25, 2024
18 checks passed
@nzakas nzakas deleted the constructorsuper-cleanup branch March 25, 2024 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

None yet

2 participants