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

no pure functions inside of es6 classes #5139

Closed
pwmckenna opened this issue Feb 3, 2016 · 15 comments
Closed

no pure functions inside of es6 classes #5139

pwmckenna opened this issue Feb 3, 2016 · 15 comments
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 feature This change adds a new feature to ESLint help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules

Comments

@pwmckenna
Copy link

I'd like to write a rule that complains if a class method does not use this. Sort of the opposite of no-invalid-this.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@eslintbot
Copy link

@pwmckenna Thanks for the issue! If you're reporting a bug, please be sure to include:

  1. The version of ESLint you are using (run eslint -v)
  2. What you did (the source code and ESLint configuration)
  3. The actual ESLint output complete with numbers
  4. What you expected to happen instead

Requesting a new rule? Please see Proposing a New Rule for instructions.

@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Feb 3, 2016
@michaelficarra
Copy link
Member

👍 That would be great.

@ilyavolodin ilyavolodin added rule Relates to ESLint's core rules feature This change adds a new feature to ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Feb 3, 2016
@ilyavolodin
Copy link
Member

Hmmm.. It seems to be a fairly limiting rule. I'm also not sure what would be a good reason to discourage using pure functions. @pwmckenna could you provide more information about why you are looking for this rule and follow our new rule proposal template?

@michaelficarra
Copy link
Member

@ilyavolodin If it doesn't mention this, it can be safely made static. There's no reason to put a function like this on a prototype.

@lo1tuma
Copy link
Member

lo1tuma commented Feb 3, 2016

I would definitely use such a rule 👍.

@ilyavolodin
Copy link
Member

Ok, sounds good to me, in that case. Thanks @michaelficarra

@ilyavolodin ilyavolodin added accepted There is consensus among the team that this change meets the criteria for inclusion help wanted The team would welcome a contribution from the community for this issue and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Feb 3, 2016
@pwmckenna
Copy link
Author

@ilyavolodin I'll definitely use that template next time, thanks for linking. I'd be happy to make a first pass at implementing this, though I suspect I'll need to ask for help along the way. I'll have something up in the next couple of days.

@nzakas
Copy link
Member

nzakas commented Feb 3, 2016

@pwmckenna I'd still like to see the info from the rule proposal posted to this issue. It really helps both with the evaluation and communicating to others the intention of this rule.

@pwmckenna
Copy link
Author

@nzakas definitely!

When the rules will warn. Include a description as well as sample code.

This rule will warn whenever this is not used from within a class method.

Good:

class A {
    constructor() {}
    static foo() {}
    increment() {this.count++;}
}

Bad:

class A {
    add(a, b) {return a + b;}
}

Whether the rule prevents an error or is stylistic.

This rule is stylistic, but has performance implications. If a function is not using this, it is unnecessarily copied whenever a new instance of the class is created.

Why the rule should be in the core instead of creating a custom rule.

Totally up to the maintainers. I think its a pretty fundamental thing, making a distinction between class members and static functions, but again, its mostly a style issue, so I can make it a custom rule as well.

Are you willing to create the rule yourself?

#5152

I went ahead and wrote it because its interesting, so I'm happy even if it doesn't make it in. Great project. Thanks for all the hard work!

pwmckenna added a commit to pwmckenna/eslint that referenced this issue Feb 5, 2016
pwmckenna added a commit to pwmckenna/eslint that referenced this issue Feb 7, 2016
pwmckenna added a commit to pwmckenna/eslint that referenced this issue Feb 9, 2016
@oriSomething
Copy link

@michaelficarra if there is a reason not to use this inside class instance functions is because classes like React.Components where functions are being called outside the class as an events hook.

@kaicataldo
Copy link
Member

kaicataldo commented Apr 27, 2016

@pwmckenna Would you like to open a PR? Looks like you did a lot of work on this already!

@DrewML
Copy link
Contributor

DrewML commented May 5, 2016

Just my 2 cents, but this rule may make more sense as a part of eslint-plugin-react.

The use-case for React makes sense, but I'm not sure how often this would be desirable with typical ES6 classes.

As an example, a developer might create a class with a method that doesn't use this, but is called from another method within the class. In that scenario, the developer may intend to make it possible for a consumer of the class to override that method for a single instance. Example:

class Foo {
   doSomething() {
      // some code here
      this.onSomething();
   }

   onSomething() {
      console.log('foo');
   }
}

var foo = new Foo();
// Consumer can override
foo.onSomething = () => console.log('bar');

With this rule enabled, it would encourage the developer of the Foo class to move the onSomething function into closure scope, rather than putting it on the prototype. This can obviously be addressed by disabling the rule for that section of the code, but figured I'd throw out the use-case.

@gyandeeps
Copy link
Member

Sounds like it should be named prefer-static. Webstorm has built-in support for this. very helpful 😄

@nzakas
Copy link
Member

nzakas commented Jun 29, 2016

There's been no progress on this issue for months. Unless someone steps forward to implement, it's time to close this issue.

@gyandeeps
Copy link
Member

working on this

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
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 feature This change adds a new feature to ESLint help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

10 participants