-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[sort-comp] Fix bug affecting instance-methods
group
#1774
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: master
Are you sure you want to change the base?
Conversation
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.
@michaelhogg an arrow function in a class property is resoundingly not an instance method - it's simply a data property that happens to have an arrow function (which, for the record, is a bad practice - never use arrow functions in class properties). Thus, a class field should not be sorted along with actual instance methods. |
@ljharb: The bug fix in this PR isn't regarding arrow functions in class properties. This is the bug I've fixed: class Hello extends React.Component {
// instanceMethod == false
classMethod() {}
}
|
@ljharb: Your point about arrow functions in React component class properties is actually an interesting topic for discussion. Currently, class Hello extends React.Component {
// instanceMethod == true
bar = () => {}
} TL;DRThere are basically two perspectives on this situation regarding arrow functions in class properties:
Regarding the But to avoid the debate/uncertainty about whether they're
1.
|
@michaelhogg thanks for the in-depth response :-) ok, so:
This PR should only include maybe item 1, and definitely item 2. The others should be filed as separate issues and discussed separately :-) |
Hey guys how will this affect users of fat-arrow / instance-bound-functions in the future? |
There is a potential issue with including arrow functions in If we were to add arrow functions to this sorting group without the option to separate them, then we would essentially be forcing users to choose between the broken behavior of defining all class properties after If we want to encourage users to separate their arrow function methods from other methods in order to inform developers that they are stored differently on instances, I would prefer it if we could create a separate group for arrow function methods (regardless of the name chosen) and place them either directly after We could also note in the documentation that arrow function methods are created and stored separately on each instance of a class etc. if we want to educate developers about the difference. |
59af733
to
865ed16
Compare
069314a
to
181c68f
Compare
380e32c
to
51d342b
Compare
Many thanks to @yannickcr and all contributors for developing this great plugin 🙂
Regarding issue #599 ("
sort-comp
: Consider separating static fields & instance fields into separate groups"), and PR #1470 ("Addinstance-methods
andinstance-variables
tosort-comp
") released in v7.6.0, there's a bug in the implementation of theinstance-methods
group 🐛Class instance methods can be declared in two different ways (AST Explorer snippet):
Using the
babel-eslint
parser, if a class instance method is declared as aFunctionExpression
, then the parent AST node is aMethodDefinition
, but thesort-comp
rule is expecting the parent node to be aClassProperty
, which is wrong ❌Unfortunately the unit tests didn't detect this bug. Here's the source code for the
valid
unit test, which I've annotated with comments showing the AST nodes and the relevantpropertiesInfos
(AST Explorer snippet):For the
constructor()
,classMethod()
andrender()
methods,instanceMethod
isfalse
but should betrue
❌The group order for that unit test is:
instance-methods
lifecycle
– group includesconstructor()
everything-else
render
so the expected order of methods is:
The test passes, but it should fail, because
constructor()
is placed beforeclassMethod()
in the source code, but it's expected to be after it.The reason why the test passes is because
classMethod()
is being wrongly included in theeverything-else
group (it should be ininstance-methods
), and so the order of methods is:This PR fixes the bug affecting
instanceMethod
incheckPropsOrder()
, and updates the corresponding unit tests (valid
andinvalid
).PS: I was the 1,000th forker of this repo 🙂