Skip to content

Commit c6bb825

Browse files
nosillegljharb
authored andcommitted
sort-comp Allow methods to belong to any matching group.
1 parent 8ecc53f commit c6bb825

File tree

2 files changed

+206
-113
lines changed

2 files changed

+206
-113
lines changed

lib/rules/sort-comp.js

+80-69
Original file line numberDiff line numberDiff line change
@@ -131,84 +131,91 @@ module.exports = {
131131
* @returns {Array} The matching patterns indexes. Return [Infinity] if there is no match.
132132
*/
133133
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-
}
134+
const methodGroupIndexes = [];
160135

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

196206
// 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-
}
207+
if (methodGroupIndexes.length === 0) {
208+
const everythingElseIndex = methodsOrder.indexOf('everything-else');
205209

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

211-
return indexes;
218+
return methodGroupIndexes;
212219
}
213220

214221
/**
@@ -409,6 +416,10 @@ module.exports = {
409416

410417
// Loop around the properties a second time (for comparison)
411418
for (k = 0, l = propertiesInfos.length; k < l; k++) {
419+
if (i === k) {
420+
continue;
421+
}
422+
412423
propB = propertiesInfos[k];
413424

414425
// Compare the properties order

0 commit comments

Comments
 (0)