From dccccad3b06a030c48034361a3f7383dca36bcbf Mon Sep 17 00:00:00 2001 From: Alex Kondratyuk Date: Mon, 14 May 2018 23:48:14 +0300 Subject: [PATCH 1/4] sort-comp: enforcing static lifecycle methods order --- lib/rules/sort-comp.js | 2 +- tests/lib/rules/sort-comp.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/rules/sort-comp.js b/lib/rules/sort-comp.js index def7f9a694..31951699c9 100644 --- a/lib/rules/sort-comp.js +++ b/lib/rules/sort-comp.js @@ -172,7 +172,7 @@ module.exports = { } } - if (method.static) { + if (method.static && methodsOrder.indexOf(method.name) === -1) { const staticIndex = methodsOrder.indexOf('static-methods'); if (staticIndex >= 0) { indexes.push(staticIndex); diff --git a/tests/lib/rules/sort-comp.js b/tests/lib/rules/sort-comp.js index 739ee55d73..ab040dfb44 100644 --- a/tests/lib/rules/sort-comp.js +++ b/tests/lib/rules/sort-comp.js @@ -504,6 +504,16 @@ ruleTester.run('sort-comp', rule, { ].join('\n'), parser: 'babel-eslint', errors: [{message: 'render should be placed after displayName'}] + }, { + // Must validate static lifecycle methods + code: [ + 'class Hello extends React.Component {', + ' static getDerivedStateFromProps() {}', + ' constructor() {}', + '}' + ].join('\n'), + parser: 'babel-eslint', + errors: [{message: 'getDerivedStateFromProps should be placed after constructor'}] }, { // Type Annotations should not be at the top by default code: [ From 9c26b382d2cd14bf4a95b2813083fbbf0299d6cd Mon Sep 17 00:00:00 2001 From: Alex Kondratyuk Date: Tue, 15 May 2018 00:09:39 +0300 Subject: [PATCH 2/4] more strict check --- lib/rules/sort-comp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/sort-comp.js b/lib/rules/sort-comp.js index 31951699c9..9f8c570e69 100644 --- a/lib/rules/sort-comp.js +++ b/lib/rules/sort-comp.js @@ -172,7 +172,7 @@ module.exports = { } } - if (method.static && methodsOrder.indexOf(method.name) === -1) { + if (method.static && method.name !== 'getDerivedStateFromProps') { const staticIndex = methodsOrder.indexOf('static-methods'); if (staticIndex >= 0) { indexes.push(staticIndex); From e133db3528a5a1240f4513ac32f53a077261390d Mon Sep 17 00:00:00 2001 From: Alex Kondratyuk Date: Tue, 15 May 2018 07:22:57 +0300 Subject: [PATCH 3/4] check for indexes length --- lib/rules/sort-comp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/sort-comp.js b/lib/rules/sort-comp.js index 9f8c570e69..6befbc0ba8 100644 --- a/lib/rules/sort-comp.js +++ b/lib/rules/sort-comp.js @@ -172,7 +172,7 @@ module.exports = { } } - if (method.static && method.name !== 'getDerivedStateFromProps') { + if (indexes.length === 0 && method.static) { const staticIndex = methodsOrder.indexOf('static-methods'); if (staticIndex >= 0) { indexes.push(staticIndex); From 272ecb983528778f6eb426018cc4b0ed5ddb820f Mon Sep 17 00:00:00 2001 From: Alex Kondratyuk Date: Tue, 15 May 2018 10:15:18 +0300 Subject: [PATCH 4/4] remove unnecessary parser from test --- tests/lib/rules/sort-comp.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/lib/rules/sort-comp.js b/tests/lib/rules/sort-comp.js index ab040dfb44..7468acd42b 100644 --- a/tests/lib/rules/sort-comp.js +++ b/tests/lib/rules/sort-comp.js @@ -512,7 +512,6 @@ ruleTester.run('sort-comp', rule, { ' constructor() {}', '}' ].join('\n'), - parser: 'babel-eslint', errors: [{message: 'getDerivedStateFromProps should be placed after constructor'}] }, { // Type Annotations should not be at the top by default