Skip to content

Commit

Permalink
sort-comp Allow methods to belong to any matching group.
Browse files Browse the repository at this point in the history
  • Loading branch information
nosilleg committed Jun 27, 2018
1 parent f80e744 commit 2f56e47
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 111 deletions.
162 changes: 95 additions & 67 deletions lib/rules/sort-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,84 +131,108 @@ module.exports = {
* @returns {Array} The matching patterns indexes. Return [Infinity] if there is no match.
*/
function getRefPropIndexes(method) {
let isRegExp;
let matching;
let i;
let j;
const indexes = [];
const methodGroupIndexes = [];

if (method.getter) {
const getterIndex = methodsOrder.indexOf('getters');
if (getterIndex >= 0) {
indexes.push(getterIndex);
}
}

if (method.setter) {
const setterIndex = methodsOrder.indexOf('setters');
if (setterIndex >= 0) {
indexes.push(setterIndex);
}
}
for (let groupIndex = 0; groupIndex < methodsOrder.length; groupIndex++) {
const currentGroup = methodsOrder[groupIndex];

if (method.typeAnnotation) {
const annotationIndex = methodsOrder.indexOf('type-annotations');
if (annotationIndex >= 0) {
indexes.push(annotationIndex);
}
}

if (indexes.length === 0) {
for (i = 0, j = methodsOrder.length; i < j; i++) {
isRegExp = methodsOrder[i].match(regExpRegExp);
if (isRegExp) {
matching = new RegExp(isRegExp[1], isRegExp[2]).test(method.name);
} else {
matching = methodsOrder[i] === method.name;
switch (currentGroup) {
case 'getters': {
if (method.getter) {
methodGroupIndexes.push(groupIndex);
}
break;
}
if (matching) {
indexes.push(i);
case 'setters': {
if (method.setter) {
methodGroupIndexes.push(groupIndex);
}
break;
}
}
}

if (indexes.length === 0 && method.static) {
const staticIndex = methodsOrder.indexOf('static-methods');
if (staticIndex >= 0) {
indexes.push(staticIndex);
}
}

if (indexes.length === 0 && method.instanceVariable) {
const annotationIndex = methodsOrder.indexOf('instance-variables');
if (annotationIndex >= 0) {
indexes.push(annotationIndex);
}
}

if (indexes.length === 0 && method.instanceMethod) {
const annotationIndex = methodsOrder.indexOf('instance-methods');
if (annotationIndex >= 0) {
indexes.push(annotationIndex);
}
}

// No matching pattern, return 'everything-else' index
if (indexes.length === 0) {
for (i = 0, j = methodsOrder.length; i < j; i++) {
if (methodsOrder[i] === 'everything-else') {
indexes.push(i);
case 'type-annotations': {
if (method.typeAnnotation) {
methodGroupIndexes.push(groupIndex);
}
break;
}
case 'static-methods': {
if (method.static) {
methodGroupIndexes.push(groupIndex);
}
break;
}
case 'instance-variables': {
if (method.instanceVariable) {
methodGroupIndexes.push(groupIndex);
}
break;
}
case 'instance-methods': {
if (method.instanceMethod) {
methodGroupIndexes.push(groupIndex);
}
break;
}
case 'displayName':
case 'propTypes':
case 'contextTypes':
case 'childContextTypes':
case 'mixins':
case 'statics':
case 'defaultProps':
case 'constructor':
case 'getDefaultProps':
case 'state':
case 'getInitialState':
case 'getChildContext':
case 'getDerivedStateFromProps':
case 'componentWillMount':
case 'UNSAFE_componentWillMount':
case 'componentDidMount':
case 'componentWillReceiveProps':
case 'UNSAFE_componentWillReceiveProps':
case 'shouldComponentUpdate':
case 'componentWillUpdate':
case 'UNSAFE_componentWillUpdate':
case 'getSnapshotBeforeUpdate':
case 'componentDidUpdate':
case 'componentDidCatch':
case 'componentWillUnmount':
case 'render': {
if (currentGroup === method.name) {
methodGroupIndexes.push(groupIndex);
}
break;
}
default: {
// Is the group a regex?
const isRegExp = currentGroup.match(regExpRegExp);
if (isRegExp) {
const isMatching = new RegExp(isRegExp[1], isRegExp[2]).test(method.name);
if (isMatching) {
methodGroupIndexes.push(groupIndex);
}
} else if (currentGroup === method.name) {
methodGroupIndexes.push(groupIndex);
}
break;
}
}
}

// No matching pattern and no 'everything-else' group
if (indexes.length === 0) {
indexes.push(Infinity);
// No matching pattern, return 'everything-else' index
if (methodGroupIndexes.length === 0) {
const everythingElseIndex = methodsOrder.indexOf('everything-else');

if (everythingElseIndex !== -1) {
methodGroupIndexes.push(everythingElseIndex);
} else {
// No matching pattern and no 'everything-else' group
methodGroupIndexes.push(Infinity);
}
}

return indexes;
return methodGroupIndexes;
}

/**
Expand Down Expand Up @@ -409,6 +433,10 @@ module.exports = {

// Loop around the properties a second time (for comparison)
for (k = 0, l = propertiesInfos.length; k < l; k++) {
if (i === k) {
continue;
}

propB = propertiesInfos[k];

// Compare the properties order
Expand Down

0 comments on commit 2f56e47

Please sign in to comment.