-
-
Notifications
You must be signed in to change notification settings - Fork 404
Add prefer-class-fields
rule
#2512
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
base: main
Are you sure you want to change the base?
Conversation
What should we do if the assignment is unreachable? class Foo {
constructor(x) {
if (x) {return}
this.foo = 'foo';
}
} |
It's dynamically set, so it cannot be statically defined. It should not be reported by this rule. |
Hey @fregante! Or maybe we can just agree that once the work in this PR is done you'll maybe just run your linter on it once? |
06d643a
to
3a9c5f8
Compare
Handled stopping of the static analysis on unsupported case: 3376845 |
d907827
to
77f4954
Compare
Okay, I fixed all test/linting errors connected with my rule - should I fix other as well? They seems to be connected with some default options missing in already existing rules? |
275195a
to
77f4954
Compare
Hey! I've merged last changes from main to the branch of this PR - now it's ready to be reviewed again |
Change the test file extension from |
@sindresorhus done: 8358a43 I'm not sure yet why the tests are failing - locally they're working just fine. |
c7632d9
to
8358a43
Compare
fixup "contructor" typos
@sindresorhus Can you reopen this PR please? I've just pushed changes |
8bfc614
to
68aeaf2
Compare
2a55d23
to
c4479a4
Compare
Don't worry about the snapshot files, I can take care. |
rules/prefer-class-fields.js
Outdated
return; | ||
} | ||
|
||
const constructorBody = constructor.value.body?.body; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move the body check into L103 too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done: 242410a
constructorBody
is used later on, so I needed to use additional optional chaining operators. In terms of typing of the constructorBody
- to achieve the same effect (correct constructorBody
typing for later usage) I needed to create a type guard.
c4479a4
to
9898da3
Compare
Thanks, @fisker my macbook broke down (is serviced - that's why I have time to work on this PR again) and I'm working through stackblitz. But it seems that the stackblitz supports only Node 20, so I'm not able to regenerate snapshots properly 😄 |
@FRSgit I pushed some changes
@FRSgit This rule should work for private fields, do you think you can continue? |
@fisker Thanks for your changes! Regarding
I think ti's not the case, is it? Check the playground here (it's typescript playground, but the result will be the same in the developer console in your browser). It looks like both ways: defining value in constructor and directly as a class field override what is set by parent constructor. Do I miss something? Everything else is clear to me, thanks! |
Sorry, you are right about this. Maybe we can also skip Anyway, it's just an wrong example, I mean other nodes can change the result.
is not safe to fix to
Since that function call may override |
Since it's not easy to say what can be skipped, I'm not sure what we can do about these totally unrelated expressions like this: class A {
constructor() {
const x = 2; // Can skip, but can't skip `const x = call()`
this.foo = 1;
}
} |
@fisker Yes, you're right with the example using function call - I bet there are many other cases that might potentially break unless rules' autofix is pretty restrictive. IMO to not over complicate, we should allow only EmptyExpressions and other Ofc, there are probably other cases where autofix could be applied just like that, but that's something for next iterations. When somebody comes with a proposition for improvement of this algo in the future. |
Adds
prefer-class-fields
rule which turns:into:
Fixes #314
IssueHunt Summary
Referenced issues
This pull request has been submitted to:
prefer-class-fields