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: allow instance and proto def in no-dupe-class-members (refs #14857) #14979

Closed
wants to merge 1 commit into from

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 autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

refs #14857, fixes #14591 (comment)

What changes did you make? (Give an overview)

Fixed the no-dupe-class-members to not report instance field and method/getter/setter with the same name as duplicates.

class Foo {
  bar; // defines property 'bar' on instances of Foo
  bar() { } // defines property 'bar' on Foo.prototype
}

class Foo {
  bar; // defines property 'bar' on instances of Foo
  get bar() { } // defines property 'bar' on Foo.prototype
}

class Foo {
  bar; // defines property 'bar' on instances of Foo
  set bar(value) { } // defines property 'bar' on Foo.prototype
}

Is there anything you'd like reviewers to focus on?

  • Does everyone agree that this shouldn't be reported, or at least not by default? Unlike all other combinations, these definitions do not target the same property. It can be argued that field definition mistakenly overrides prototype definitions in lookups, but reporting this might flag valid code such as:
class Foo {
    handleClick = this.handleClick.bind(this);
    handleClick() {
        // ....
    }
}
  • All added invalid tests are regression tests, this change produces only fewer warnings. Some tests that appear as deleted in the diff were just moved to a different position.

@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 Aug 26, 2021
@nzakas
Copy link
Member

nzakas commented Aug 27, 2021

I think we should report duplicate fields by default and have an option to disable the check. From the docs:

If there are declarations of the same name in class members, the last declaration overwrites other declarations silently. It can cause unexpected behaviors.

I think the same type of hazard exists when a field and a method have the same name, only now, you may not know that the field wins. I think doing this is most likely an error so I’d prefer to warn by default.

@mdjermanovic mdjermanovic marked this pull request as draft August 27, 2021 13:04
@mdjermanovic
Copy link
Member Author

I think the same type of hazard exists when a field and a method have the same name, only now, you may not know that the field wins. I think doing this is most likely an error so I’d prefer to warn by default.

That's a good point. My only concern is this pattern:

class Foo {
    handleClick = this.handleClick.bind(this);
    handleClick() {}
}

It's a possible variant of the common pattern to bind some methods in the constructor (e.g., https://reactjs.org/docs/handling-events.html recommends binding event handlers in the constructor).

This rule is classified as type: "problem" so it aims to report possible errors, but this is a valid code. It could be just a stylistic issue.

I have another proposal:

  • Allow this and only this particular combination by default: method foo and instance field foo initialized to this.foo.bind(this).
  • Start without an option to disable instance vs prototype check. I'm not aware of any other cases that would be common enough to warrant disabling this check in entire codebase, so using eslint-disable-* comments might be more appropriate.

@nzakas
Copy link
Member

nzakas commented Aug 31, 2021

That pattern still seems like an edge case to me. I think it’s fine to flag it and let people use disable comments to work around it. If we start getting feedback indicating that pattern is becoming common, we can always add an option later.

@mdjermanovic
Copy link
Member Author

Okay, I still think the rule just shouldn't report this pattern by default, but we can reconsider that later.

@mdjermanovic mdjermanovic deleted the nodupeclassmembers-classfields branch September 19, 2021 16:27
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Feb 28, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants