-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
Rule proposal: class-method-style
#2504
Comments
Makes sense, as long as the method doesn't use
In the 4th step, everything looks fine, but the original intent of the method is lost. I wonder the the rule should go both ways and:
|
After a few days of thinking, I prefer this rule not providing the option to select // Firstly,
// This style is horrible obviously. Ban it.
class Foo {
propertyStyle = function () {
console.log("I am property style method");
};
}
// Secondly,
// User cannot use `this` keyword in this style. Users do not have to put it in a class.
// Just use static method or extract this method to be a function outside of class.
// See https://eslint.org/docs/latest/rules/class-methods-use-this
class Foo {
propertyStyle = () => {
console.log("I am property style method");
};
}
// Thirdly,
// If this arrow function is only one line, maybe we can allow it
class Foo {
propertyStyle = () => console.log("I am property style method");
} To summary, I think this rule should not care about the |
I think you're missing the point of the property style: they are pre-bound. class A {
value = 1;
get = () => console.log(this.value)
}
const a = new A()
setTimeout(a.get, 1000) This will log There are only a few practical reasons to prefer methods over properties:
What you're describing are just stylistic preferences, but this has actual runtime differences. |
@fregante You are right. I've never used property style methods. I forgot that we can use But I still personally prefer this rule to be a |
Description
We have 2 styles to define a method in class:
I recommend method style and disallow property style. Therefore, this rule
class-method-style
accept an option to specify the style,method
(default) orproperty
.Fail
Pass
Proposed rule name
class-method-style
Additional Info
Related rule: https://typescript-eslint.io/rules/method-signature-style.
Related issue: #1737
The text was updated successfully, but these errors were encountered: