Skip to content

Commit 368346e

Browse files
authored
Merge pull request #1795 from lynxtaa/fix-sort-comp-statics
sort-comp: enforcing static lifecycle methods order
2 parents 602e071 + 272ecb9 commit 368346e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/rules/sort-comp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ module.exports = {
172172
}
173173
}
174174

175-
if (method.static) {
175+
if (indexes.length === 0 && method.static) {
176176
const staticIndex = methodsOrder.indexOf('static-methods');
177177
if (staticIndex >= 0) {
178178
indexes.push(staticIndex);

tests/lib/rules/sort-comp.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,15 @@ ruleTester.run('sort-comp', rule, {
504504
].join('\n'),
505505
parser: 'babel-eslint',
506506
errors: [{message: 'render should be placed after displayName'}]
507+
}, {
508+
// Must validate static lifecycle methods
509+
code: [
510+
'class Hello extends React.Component {',
511+
' static getDerivedStateFromProps() {}',
512+
' constructor() {}',
513+
'}'
514+
].join('\n'),
515+
errors: [{message: 'getDerivedStateFromProps should be placed after constructor'}]
507516
}, {
508517
// Type Annotations should not be at the top by default
509518
code: [

0 commit comments

Comments
 (0)