class-methods-use-this documentation is unclear about how to correct the problems
See original GitHub issueWhat 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:
/*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()
because if I change it…
> class A {
static m(){console.log("hi")}
n(){this.m()}
}
[Function: A]
> let a = new A()
undefined
> a.n()
TypeError: this.m is not a function
at A.n (repl:3:10)
at repl:1:3
at sigintHandlersWrap (vm.js:22:35)
at sigintHandlersWrap (vm.js:73:12)
at ContextifyScript.Script.runInThisContext (vm.js:21:12)
at REPLServer.defaultEval (repl.js:340:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:538:10)
at emitOne (events.js:101:20)
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. 😄
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
How can I fix 'warning Expected 'this' to be used by class ...
First, the reason you get this error is because of the ESLint rule https://eslint.org/docs/rules/class-methods-use-this.
Read more >Poor Documentation: Why It Happens and How to Fix It
Before tackling a documentation problem, health care organizations must elucidate whether indeed there is a problem in the first place.
Read more >Rules - ESLint - Pluggable JavaScript Linter
Some problems reported by this rule are automatically fixable by the --fix command ... Disallow confusing multiline expressions ... class-methods-use-this.
Read more >Converse.js API Documentation Source: headless/dist ...
The // definitions of some functions (but in particular, URI.decode) will occasionally change due // to URI.js having ISO8859 and Unicode modes.
Read more >Source code for spacetrack.base
For more information, refer to the `Space-Track documentation`_. ... That said, please open an issue on `GitHub`_ for me to add them to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
this
.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
// eslint-disable-line
comments (or similar) to avoid ESLint reporting an error in specific cases where violating the rule is part of your design. So you could use one of those comments in this case if you believe the rule does not need to be followed in your particular case.Indeed; that documentation should be improved.