Skip to content

Commit 2f56e47

Browse files
committed
sort-comp Allow methods to belong to any matching group.
1 parent f80e744 commit 2f56e47

File tree

2 files changed

+221
-111
lines changed

2 files changed

+221
-111
lines changed

lib/rules/sort-comp.js

Lines changed: 95 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -131,84 +131,108 @@ 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 = [];
134+
const methodGroupIndexes = [];
139135

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-
}
136+
for (let groupIndex = 0; groupIndex < methodsOrder.length; groupIndex++) {
137+
const currentGroup = methodsOrder[groupIndex];
153138

154-
if (method.typeAnnotation) {
155-
const annotationIndex = methodsOrder.indexOf('type-annotations');
156-
if (annotationIndex >= 0) {
157-
indexes.push(annotationIndex);
158-
}
159-
}
160-
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;
139+
switch (currentGroup) {
140+
case 'getters': {
141+
if (method.getter) {
142+
methodGroupIndexes.push(groupIndex);
143+
}
144+
break;
168145
}
169-
if (matching) {
170-
indexes.push(i);
146+
case 'setters': {
147+
if (method.setter) {
148+
methodGroupIndexes.push(groupIndex);
149+
}
150+
break;
171151
}
172-
}
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-
}
195-
196-
// 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);
152+
case 'type-annotations': {
153+
if (method.typeAnnotation) {
154+
methodGroupIndexes.push(groupIndex);
155+
}
156+
break;
157+
}
158+
case 'static-methods': {
159+
if (method.static) {
160+
methodGroupIndexes.push(groupIndex);
161+
}
162+
break;
163+
}
164+
case 'instance-variables': {
165+
if (method.instanceVariable) {
166+
methodGroupIndexes.push(groupIndex);
167+
}
168+
break;
169+
}
170+
case 'instance-methods': {
171+
if (method.instanceMethod) {
172+
methodGroupIndexes.push(groupIndex);
173+
}
174+
break;
175+
}
176+
case 'displayName':
177+
case 'propTypes':
178+
case 'contextTypes':
179+
case 'childContextTypes':
180+
case 'mixins':
181+
case 'statics':
182+
case 'defaultProps':
183+
case 'constructor':
184+
case 'getDefaultProps':
185+
case 'state':
186+
case 'getInitialState':
187+
case 'getChildContext':
188+
case 'getDerivedStateFromProps':
189+
case 'componentWillMount':
190+
case 'UNSAFE_componentWillMount':
191+
case 'componentDidMount':
192+
case 'componentWillReceiveProps':
193+
case 'UNSAFE_componentWillReceiveProps':
194+
case 'shouldComponentUpdate':
195+
case 'componentWillUpdate':
196+
case 'UNSAFE_componentWillUpdate':
197+
case 'getSnapshotBeforeUpdate':
198+
case 'componentDidUpdate':
199+
case 'componentDidCatch':
200+
case 'componentWillUnmount':
201+
case 'render': {
202+
if (currentGroup === method.name) {
203+
methodGroupIndexes.push(groupIndex);
204+
}
205+
break;
206+
}
207+
default: {
208+
// Is the group a regex?
209+
const isRegExp = currentGroup.match(regExpRegExp);
210+
if (isRegExp) {
211+
const isMatching = new RegExp(isRegExp[1], isRegExp[2]).test(method.name);
212+
if (isMatching) {
213+
methodGroupIndexes.push(groupIndex);
214+
}
215+
} else if (currentGroup === method.name) {
216+
methodGroupIndexes.push(groupIndex);
217+
}
201218
break;
202219
}
203220
}
204221
}
205222

206-
// No matching pattern and no 'everything-else' group
207-
if (indexes.length === 0) {
208-
indexes.push(Infinity);
223+
// No matching pattern, return 'everything-else' index
224+
if (methodGroupIndexes.length === 0) {
225+
const everythingElseIndex = methodsOrder.indexOf('everything-else');
226+
227+
if (everythingElseIndex !== -1) {
228+
methodGroupIndexes.push(everythingElseIndex);
229+
} else {
230+
// No matching pattern and no 'everything-else' group
231+
methodGroupIndexes.push(Infinity);
232+
}
209233
}
210234

211-
return indexes;
235+
return methodGroupIndexes;
212236
}
213237

214238
/**
@@ -409,6 +433,10 @@ module.exports = {
409433

410434
// Loop around the properties a second time (for comparison)
411435
for (k = 0, l = propertiesInfos.length; k < l; k++) {
436+
if (i === k) {
437+
continue;
438+
}
439+
412440
propB = propertiesInfos[k];
413441

414442
// Compare the properties order

0 commit comments

Comments
 (0)