Skip to content

Commit 1870758

Browse files
committed
sort-comp rule: fix bug affecting instance-methods group
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.
1 parent d68ef96 commit 1870758

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

lib/rules/sort-comp.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,11 @@ module.exports = {
384384
node.value.type !== 'ArrowFunctionExpression' &&
385385
node.value.type !== 'FunctionExpression',
386386
instanceMethod: !node.static &&
387-
node.type === 'ClassProperty' &&
388387
node.value &&
389-
(node.value.type === 'ArrowFunctionExpression' ||
390-
node.value.type === 'FunctionExpression'),
388+
(
389+
(node.type === 'ClassProperty' && node.value.type === 'ArrowFunctionExpression') ||
390+
(node.type === 'MethodDefinition' && node.value.type === 'FunctionExpression')
391+
),
391392
typeAnnotation: !!node.typeAnnotation && node.value === null
392393
}));
393394

tests/lib/rules/sort-comp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ ruleTester.run('sort-comp', rule, {
315315
code: [
316316
'class Hello extends React.Component {',
317317
' foo = () => {}',
318-
' constructor() {}',
319318
' classMethod() {}',
319+
' constructor() {}',
320320
' static bar = () => {}',
321321
' render() {',
322322
' return <div>{this.props.text}</div>;',
@@ -573,7 +573,7 @@ ruleTester.run('sort-comp', rule, {
573573
'}'
574574
].join('\n'),
575575
parser: 'babel-eslint',
576-
errors: [{message: 'foo should be placed before constructor'}],
576+
errors: [{message: 'classMethod should be placed before constructor'}],
577577
options: [{
578578
order: [
579579
'instance-methods',

0 commit comments

Comments
 (0)