-
-
Notifications
You must be signed in to change notification settings - Fork 392
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
Add prefer-class-fields
rule
#2512
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
Co-authored-by: Sindre Sorhus <[email protected]>
Co-authored-by: Sindre Sorhus <[email protected]>
fa07ec0
to
ca160be
Compare
Okay, got it working and all of the requested changes are applied. BTW it's weird, but for me the |
ready to be re-reviewed @fisker |
317cd49
to
9898da3
Compare
const constructor = classBody.body.find(x => x.kind === 'constructor'); | ||
|
||
if (!constructor || constructor.type !== 'MethodDefinition') { | ||
return; | ||
} |
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.
const constructor = classBody.body.find(node => node.kind === 'constructor' && node.type !== 'MethodDefinition');
if (!constructor) {
return;
}
Does TypeScript allow multiple constructor
? If so, it will find the correct constructor
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.
Typescript allows multiple constructor declarations (overloads) but only one definition (implementation). See an example here
But I don't think I understand what do you mean here - can you elaborate a bit more?
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.
Example from your link
class Box {
public x: number;
public y: number;
public height: number;
public width: number;
constructor(); // <- Your version will give up when find this
constructor(obj: IBox);
constructor(obj?: IBox) { // <- My version can locate this
this.x = obj?.x ?? 0;
this.y = obj?.y ?? 0;
this.height = obj?.height ?? 0;
this.width = obj?.width ?? 0;
}
}
Sorry, my version has a typo
- const constructor = classBody.body.find(node => node.kind === 'constructor' && node.type !== 'MethodDefinition');
+ const constructor = classBody.body.find(node => node.kind === 'constructor' && node.type === 'MethodDefinition');
if (!constructor) {
return;
}
const validConstructorProperties | ||
= firstInvalidProperty === -1 | ||
? constructorBody | ||
: constructorBody.slice(0, firstInvalidProperty); |
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.
We already use a loop, we can use the index directly, no need slice here.
/** | ||
@param {import('eslint').Rule.RuleFixer} fixer | ||
*/ | ||
* suggest(fixer) { |
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.
suggest
should be an array
.
Adds
prefer-class-fields
rule which turns:into:
Fixes #314
IssueHunt Summary
Referenced issues
This pull request has been submitted to:
prefer-class-fields