Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teach sort-comp to put static methods at the top #31

Merged
merged 1 commit into from
Feb 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Reorders React component methods to match the [ESLint](http://eslint.org/)
[react/sort-comp
rule](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md),
specifically with the [tighter constraints of the Airbnb style
guide](https://github.com/airbnb/javascript/blob/6c89f958/packages/eslint-config-airbnb/rules/react.js#L47-L57).
guide](https://github.com/airbnb/javascript/blob/7684892951ef663e1c4e62ad57d662e9b2748b9e/packages/eslint-config-airbnb/rules/react.js#L122-L134).

```sh
jscodeshift -t react-codemod/transforms/sort-comp.js <file>
Expand Down
4 changes: 4 additions & 0 deletions test/sort-comp-test2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class MyComponent extends React.Component {
componentDidMount() {
}

static someStaticThing() {
// should come first
}

renderFoo() {
// other render* function
}
Expand Down
4 changes: 4 additions & 0 deletions test/sort-comp-test2.output.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const propTypes = {};

// comment above class
class MyComponent extends React.Component {
static someStaticThing() {
// should come first
}

// comment on componentDidMount
componentDidMount() {
}
Expand Down
42 changes: 30 additions & 12 deletions transforms/sort-comp.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Reorders React component methods to match the [ESLint](http://eslint.org/)
* [react/sort-comp rule](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md),
* specifically with the [tighter constraints of the Airbnb style guide](https://github.com/airbnb/javascript/blob/6c89f9587f96688bf0d4d536adb8eb4ed9bf0002/packages/eslint-config-airbnb/rules/react.js#L47-L57),
* specifically with the [tighter constraints of the Airbnb style guide](https://github.com/airbnb/javascript/blob/7684892951ef663e1c4e62ad57d662e9b2748b9e/packages/eslint-config-airbnb/rules/react.js#L122-L134),
*
* 'react/sort-comp': [2, {
* 'order': [
* 'static-methods',
* 'lifecycle',
* '/^on.+$/',
* '/^(get|set)(?!(InitialState$|DefaultProps$|ChildContext$)).+$/',
Expand All @@ -28,8 +29,8 @@ module.exports = function(fileInfo, api, options) {
const nameA = a.key.name;
const nameB = b.key.name;

const indexA = getRefPropIndexes(nameA);
const indexB = getRefPropIndexes(nameB);
const indexA = getRefPropIndexes(a);
const indexB = getRefPropIndexes(b);

const sameLocation = indexA.length == 1 && indexB.length == 1 && indexA[0] == indexB[0];

Expand Down Expand Up @@ -86,6 +87,7 @@ module.exports = function(fileInfo, api, options) {

// Hard-coded for Airbnb style
const methodsOrder = [
'static-methods',
'displayName',
'propTypes',
'contextTypes',
Expand Down Expand Up @@ -119,24 +121,40 @@ const regExpRegExp = /\/(.*)\/([g|y|i|m]*)/;

/**
* Get indexes of the matching patterns in methods order configuration
* @param {String} method - Method name.
* @param {Object} method
* @returns {Array} The matching patterns indexes. Return [Infinity] if there is no match.
*/
function getRefPropIndexes(method) {
var methodName = method.key.name;
var isRegExp;
var matching;
var i;
var j;
var indexes = [];
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);
} else {
matching = methodsOrder[i] === method;
// Check for static methods
if (indexes.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexes.length has to equal zero at this point. it just initialized to empty array in the line above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I decided to add the check here to make it so we don't have to change this again if we add something before it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. What do you think of #32?

if (method.static) {
for (i = 0, j = methodsOrder.length; i < j; i++) {
if (methodsOrder[i] === 'static-methods') {
indexes.push(i);
}
}
}
if (matching) {
indexes.push(i);
}

// This is not a staic method, so we try to determine where it should go based
// on method name.
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(methodName);
} else {
matching = methodsOrder[i] === methodName;
}
if (matching) {
indexes.push(i);
}
}
}

Expand Down