Skip to content

Commit 32b751f

Browse files
authored
Merge pull request jsx-eslint#1858 from nosilleg/1821-sort-comp-order
sort-comp Allow methods to belong to any matching group.
2 parents 0ebcb62 + 76a4120 commit 32b751f

File tree

3 files changed

+208
-113
lines changed

3 files changed

+208
-113
lines changed

lib/rules/sort-comp.js

+81-69
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const has = require('has');
88
const util = require('util');
99

1010
const Components = require('../util/Components');
11+
const arrayIncludes = require('array-includes');
1112
const astUtil = require('../util/ast');
1213
const docsUrl = require('../util/docsUrl');
1314

@@ -131,84 +132,91 @@ module.exports = {
131132
* @returns {Array} The matching patterns indexes. Return [Infinity] if there is no match.
132133
*/
133134
function getRefPropIndexes(method) {
134-
let isRegExp;
135-
let matching;
136-
let i;
137-
let j;
138-
const indexes = [];
139-
140-
if (method.getter) {
141-
const getterIndex = methodsOrder.indexOf('getters');
142-
if (getterIndex >= 0) {
143-
indexes.push(getterIndex);
144-
}
145-
}
146-
147-
if (method.setter) {
148-
const setterIndex = methodsOrder.indexOf('setters');
149-
if (setterIndex >= 0) {
150-
indexes.push(setterIndex);
151-
}
152-
}
153-
154-
if (method.typeAnnotation) {
155-
const annotationIndex = methodsOrder.indexOf('type-annotations');
156-
if (annotationIndex >= 0) {
157-
indexes.push(annotationIndex);
158-
}
159-
}
135+
const methodGroupIndexes = [];
160136

161-
if (indexes.length === 0) {
162-
for (i = 0, j = methodsOrder.length; i < j; i++) {
163-
isRegExp = methodsOrder[i].match(regExpRegExp);
164-
if (isRegExp) {
165-
matching = new RegExp(isRegExp[1], isRegExp[2]).test(method.name);
166-
} else {
167-
matching = methodsOrder[i] === method.name;
137+
methodsOrder.forEach((currentGroup, groupIndex) => {
138+
if (currentGroup === 'getters') {
139+
if (method.getter) {
140+
methodGroupIndexes.push(groupIndex);
168141
}
169-
if (matching) {
170-
indexes.push(i);
142+
} else if (currentGroup === 'setters') {
143+
if (method.setter) {
144+
methodGroupIndexes.push(groupIndex);
145+
}
146+
} else if (currentGroup === 'type-annotations') {
147+
if (method.typeAnnotation) {
148+
methodGroupIndexes.push(groupIndex);
149+
}
150+
} else if (currentGroup === 'static-methods') {
151+
if (method.static) {
152+
methodGroupIndexes.push(groupIndex);
153+
}
154+
} else if (currentGroup === 'instance-variables') {
155+
if (method.instanceVariable) {
156+
methodGroupIndexes.push(groupIndex);
157+
}
158+
} else if (currentGroup === 'instance-methods') {
159+
if (method.instanceMethod) {
160+
methodGroupIndexes.push(groupIndex);
161+
}
162+
} else if (arrayIncludes([
163+
'displayName',
164+
'propTypes',
165+
'contextTypes',
166+
'childContextTypes',
167+
'mixins',
168+
'statics',
169+
'defaultProps',
170+
'constructor',
171+
'getDefaultProps',
172+
'state',
173+
'getInitialState',
174+
'getChildContext',
175+
'getDerivedStateFromProps',
176+
'componentWillMount',
177+
'UNSAFE_componentWillMount',
178+
'componentDidMount',
179+
'componentWillReceiveProps',
180+
'UNSAFE_componentWillReceiveProps',
181+
'shouldComponentUpdate',
182+
'componentWillUpdate',
183+
'UNSAFE_componentWillUpdate',
184+
'getSnapshotBeforeUpdate',
185+
'componentDidUpdate',
186+
'componentDidCatch',
187+
'componentWillUnmount',
188+
'render'
189+
], currentGroup)) {
190+
if (currentGroup === method.name) {
191+
methodGroupIndexes.push(groupIndex);
192+
}
193+
} else {
194+
// Is the group a regex?
195+
const isRegExp = currentGroup.match(regExpRegExp);
196+
if (isRegExp) {
197+
const isMatching = new RegExp(isRegExp[1], isRegExp[2]).test(method.name);
198+
if (isMatching) {
199+
methodGroupIndexes.push(groupIndex);
200+
}
201+
} else if (currentGroup === method.name) {
202+
methodGroupIndexes.push(groupIndex);
171203
}
172204
}
173-
}
174-
175-
if (indexes.length === 0 && method.static) {
176-
const staticIndex = methodsOrder.indexOf('static-methods');
177-
if (staticIndex >= 0) {
178-
indexes.push(staticIndex);
179-
}
180-
}
181-
182-
if (indexes.length === 0 && method.instanceVariable) {
183-
const annotationIndex = methodsOrder.indexOf('instance-variables');
184-
if (annotationIndex >= 0) {
185-
indexes.push(annotationIndex);
186-
}
187-
}
188-
189-
if (indexes.length === 0 && method.instanceMethod) {
190-
const annotationIndex = methodsOrder.indexOf('instance-methods');
191-
if (annotationIndex >= 0) {
192-
indexes.push(annotationIndex);
193-
}
194-
}
205+
});
195206

196207
// No matching pattern, return 'everything-else' index
197-
if (indexes.length === 0) {
198-
for (i = 0, j = methodsOrder.length; i < j; i++) {
199-
if (methodsOrder[i] === 'everything-else') {
200-
indexes.push(i);
201-
break;
202-
}
203-
}
204-
}
208+
if (methodGroupIndexes.length === 0) {
209+
const everythingElseIndex = methodsOrder.indexOf('everything-else');
205210

206-
// No matching pattern and no 'everything-else' group
207-
if (indexes.length === 0) {
208-
indexes.push(Infinity);
211+
if (everythingElseIndex !== -1) {
212+
methodGroupIndexes.push(everythingElseIndex);
213+
} else {
214+
// No matching pattern and no 'everything-else' group
215+
methodGroupIndexes.push(Infinity);
216+
}
209217
}
210218

211-
return indexes;
219+
return methodGroupIndexes;
212220
}
213221

214222
/**
@@ -409,6 +417,10 @@ module.exports = {
409417

410418
// Loop around the properties a second time (for comparison)
411419
for (k = 0, l = propertiesInfos.length; k < l; k++) {
420+
if (i === k) {
421+
continue;
422+
}
423+
412424
propB = propertiesInfos[k];
413425

414426
// Compare the properties order

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"homepage": "https://github.com/yannickcr/eslint-plugin-react",
2525
"bugs": "https://github.com/yannickcr/eslint-plugin-react/issues",
2626
"dependencies": {
27+
"array-includes": "^3.0.3",
2728
"doctrine": "^2.1.0",
2829
"has": "^1.0.3",
2930
"jsx-ast-utils": "^2.0.1",

0 commit comments

Comments
 (0)