-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
sort-comp: Consider separating static fields & instance fields into separate groups #599
Comments
I'd expect static fields and static methods to both be the last items in the class body, after everything instance-related is done - although I agree that instance fields could belong above the constructor. |
I tried to add this feature with this PR: #685. Will gladly take suggestions and feedback on it. |
Also, to continue in this direction, it could be good to have separate groups for |
@yannickcr I like it, but it raises a couple of questions:
Statics fields: class Hello extends React.Component {
static displayName = 'Hello'; // static-properties
static fetch() {} // static-methods
static process = () => {} // static-methods ?
} Instance fields: class Hello extends React.Component {
count = 0; // instance-properties
handleClick = () => {} // instance-methods
renderSeciton() {} // instance-methods ?
} I think it should be consistent for both
Right now class Hello extends React.Component {
static prop1 = 'Hello';
static fetch() {}
static prop2 = 'Bye';
} but configuration like this: [
'static-properties',
'static-methods'
] wouldn't allow this and users wouldn't be able to go back to the way it was before without applying codemod Thoughts? |
There's actually a difference between a class method and a function-valued property - |
@ljharb I think that it's pretty much the same thing, because functions on the prototype can be considered a property too. It's obvious if you use ES5 syntax: class Hello extends React.Component {
renderSection: function() {
}
} In my opinion we need to determine what
Maybe there can also be a What do you think? |
The difference is that class methods are non-enumerable (properties are enumerable) and they have a |
This is a good point. Didn't look at it from this perspective. For now I don't know how to move further with this. There are multiple approaches that could be taken. |
This may be outside of the scope of this particular PR, but I would love to have more granular control over the ordering without the use of groups. Similar to how regular expressions may be used inside of the Perhaps something along these lines: // .eslintrc
"react/sort-comp": [2, {
"order": [
"foo", // prototype method (default -- already supported)
"/bar/", // prototype method regex (already supported)
"static foo", // static initializer
"static /bar/", // static initializer regex
"foo=", // instance initializer
"/bar/=" // instance initializer regex
]
}] My apologies if this wasn't quite what was meant by "weighing in". Thoughts? Sidenote: Another thought I've had is that this plugin has utility beyond React Component classes. There's not much keeping it from being generally useful for all ES6+ class declarations. If the ambiguities between static/prototype/instance are tackled, that only leaves a few more cases such as getter/setters and symbols. I'd be happy to create a new issue for discussion if this isn't the appropriate thread. |
Strange. It seems like
When I declare
it's OK, but when I add some custom property, e.g.:
it throws an error:
|
@ktaras thanks, could you file that as a separate issue? |
Regarding this issue: jsx-eslint#599 sort-comp: Consider separating static fields & instance fields into separate groups and this PR, released in v7.6.0: jsx-eslint#1470 Add instance-methods and instance-variables to sort-comp. there's a bug in the implementation of the `instance-methods` group. Class instance methods can be declared in two different ways: class Main extends React.Component { // MethodDefinition -> FunctionExpression foo() {} // ClassProperty -> ArrowFunctionExpression bar = () => {} } Using the `babel-eslint` parser, if a class instance method is declared as a `FunctionExpression`, then the parent AST node is a `MethodDefinition`, but the `sort-comp` rule is expecting the parent node to be a `ClassProperty`, which is wrong.
FYI: discussion of this issue is continuing in #1774. |
Currently sort-comp asks me to place instance fields after methods in my class. It's conventional to place instance fields above the constructor, underneath static fields.
Possibly also consider allowing static fields be grouped separately from static methods.
The text was updated successfully, but these errors were encountered: