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
class-methods-use-this documentation is unclear about how to correct the problems #8910
Comments
Hi @fega, thanks for the issue. It looks like there's not enough information for us to know how to help you. If you're reporting a bug, please be sure to include:
Requesting a new rule? Please see Proposing a New Rule for instructions. Requesting a rule change? Please see Proposing a Rule Change for instructions. If it's something else, please just provide as much additional information as possible. Thanks! |
In this case, your code is incorrect - Your only options here are This also is a runtime error, and not an issue with eslint, so I'm not sure what rule modification would help you here. |
@ljharb yeah, this doesn't work because is an static method. but the rule 'class-methods-use-this' is warning if I don't use the keyword 'static' in the method m... My point is that eslint shouldn't be warning in the following case: /*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/
> class A {
m(){console.log("hi")} // this shouln't warn
n(){this.m()}
}
[Function: A]
> let a = new A()
undefined
> a.n() Is in that way more clear? |
@fega Yes, much more clear, thanks! In that case, that's the sole purpose of the rule - to warn when it needn't be a class method. You could do it this way, and both satisfy the rule and minimize your class's API surface: function m() {
console.log("hi");
}
class A {
n(){m()}
} |
@ljharb yeah, but this is only an example of a warning case, but sometimes is necessary maintain the Since I'm using and refactoring a code base with a large use of classes, I've had a lot of awful surprises due this behavior |
@fega As @ljharb noted, this rule is slightly opinionated and encourages you to reduce your API surface by avoiding exposing instance methods that don't use For this rule (and any other ESLint rule), we recognize that there are times where a rule does not apply exactly 100% of the time. In those cases where a rule configuration option does not exist to control when the rule should or should not be enforced, you can always use |
@platinumazure well, I don't see why should be a problem give to the rule a little bit more flexibility. For the other side, I made some mistakes refactoring due this rigid rule behavior (that, I think, shouldn't happen when you use a linter). Also, I'm using those kinds of comments, but you know, that an overpopulation of comments adds more noise in the code and could leads into forget to refactor some methods that really could need changed in order to reduce the API surface. Another point, is that the rule is not encouraging to reduce the API surface, is encouraging to add the "static" keyword, that could introduce a lot of unexpected problems when this method is being used in others parts of the code. |
I think you might be misunderstanding the purpose of the rule. It's not trying to say that you should simply be able to add the |
Sticking |
@not-an-aardvark, @ljharb, maybe the problem was in the documentation, it states
That's not true for all of the use cases. |
I see what you mean. Maybe that should be reworded to avoid implying that you can add |
Indeed; that documentation should be improved. |
What rule do you want to change?
class-methods-use-this
Does this change cause the rule to produce more or fewer warnings?
fewer warning
Please provide some example code that this change will affect:
In javascript we cannot call static methods from non-static methods, ie:
because if I change it...
I'm aware that as mdn states, we can call using the className or this.constructor, but this structure looks for me weird and verbose.
How will the change be implemented? (New option, new default behavior, etc.)?
I propose that this Rule should have an option to disable it if the method is being called from another method.
😄
The text was updated successfully, but these errors were encountered: