-
-
Notifications
You must be signed in to change notification settings - Fork 382
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: prefer-class-fields
#314
Comments
Sounds good, but I think we should wait until ESLint supports class fields. Should it also handle: class Foo {
constructor() {
this._foo = 'foo';
}
} to class Foo {
#foo = 'foo';
} ? |
I think a rule for private fields would be awesome, yeah. I don't think we can realistically auto-fix those, however, because people abuse |
So you think private fields should be a separate rule?
👍 |
Yeah. As a general principle, I prefer rules to be have the smallest reasonable scope. In this case, I can also see why a team might want to convert to class fields because of the fixer but not want to enable the private fields rule right away because they'd have to move a bunch of code around. |
Issue for private class fields: #979 |
Does ESLint support class fields yet? |
prefer-class-fields
This rule would be pretty useful on Error constructors like: class MyError extends Error {
constructor(message: string) {
super(message);
this.name = "MyError";
}
} 👇 class MyError extends Error {
name = "MyError"
} |
This is accepted. |
This comment was marked as spam.
This comment was marked as spam.
@fregante has funded $30.00 to this issue.
|
FYI I've implemented this rule here, feel free to review & comment! |
There is a superior pattern for class field declarations that can declutter constructors (often making them totally unnecessary.) The feature is still considered experimental but it works out of the box with babel.
Fail:
Pass:
IssueHunt Summary
Backers (Total: $30.00)
Submitted pull Requests
prefer-class-fields
ruleBecome a backer now!
Or submit a pull request to get the deposits!
Tips
The text was updated successfully, but these errors were encountered: