Skip to content

Commit

Permalink
Merge pull request facebook#28 from lencioni/sort-comp-es6-class
Browse files Browse the repository at this point in the history
Add support for ES6 classes in sort-comp
  • Loading branch information
cpojer committed Feb 4, 2016
2 parents 166713e + 898ea76 commit c07bbb7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
2 changes: 2 additions & 0 deletions test/__tests__/sort-comp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ describe('sort-comp', () => {

it('transforms correctly', () => {
test('sort-comp', 'sort-comp-test');

test('sort-comp', 'sort-comp-test2');
});

});
35 changes: 35 additions & 0 deletions test/sort-comp-test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var React = require('react/addons');

const propTypes = {};

// comment above class
class MyComponent extends React.Component {
// comment at top of createClass
// this will be attached to first method

render() {
return <div />;
}

// comment on componentDidMount
componentDidMount() {
}

renderFoo() {
// other render* function
}

renderBar() {
// should come before renderFoo
}

myOwnMethod(foo) {
// comment within method
}

}

MyComponent.propTypes = propTypes;

/* comment at end */
module.exports = MyComponent;
35 changes: 35 additions & 0 deletions test/sort-comp-test2.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var React = require('react/addons');

const propTypes = {};

// comment above class
class MyComponent extends React.Component {
// comment on componentDidMount
componentDidMount() {
}

myOwnMethod(foo) {
// comment within method
}

renderBar() {
// should come before renderFoo
}

renderFoo() {
// other render* function
}

// comment at top of createClass
// this will be attached to first method

render() {
return <div />;
}

}

MyComponent.propTypes = propTypes;

/* comment at end */
module.exports = MyComponent;
27 changes: 22 additions & 5 deletions transforms/sort-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* 'render'
* ]
* }],
*
* NOTE: only works on React.createClass() syntax, not ES6 class.
*/
module.exports = function(fileInfo, api, options) {
const j = api.jscodeshift;
Expand Down Expand Up @@ -52,14 +50,33 @@ module.exports = function(fileInfo, api, options) {
}
};

const sortClassProperties = classPath => {
const spec = ReactUtils.getClassExtendReactSpec(classPath);

if (spec) {
spec.body.sort(propertyComparator);
}
};

if (
options['explicit-require'] === false ||
ReactUtils.hasReact(root)
) {
const sortCandidates = ReactUtils.findReactCreateClass(root);
const createClassSortCandidates = ReactUtils.findReactCreateClass(root);
const es6ClassSortCandidates = ReactUtils.findReactES6ClassDeclaration(root);

if (createClassSortCandidates.size() > 0) {
createClassSortCandidates.forEach(sortComponentProperties);
}

if (es6ClassSortCandidates.size() > 0) {
es6ClassSortCandidates.forEach(sortClassProperties);
}

if (sortCandidates.size() > 0) {
sortCandidates.forEach(sortComponentProperties);
if (
createClassSortCandidates.size() > 0 ||
es6ClassSortCandidates.size() > 0
) {
return root.toSource(printOptions);
}
}
Expand Down
3 changes: 3 additions & 0 deletions transforms/utils/ReactUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ module.exports = function(j) {
}
};

const getClassExtendReactSpec = classPath => classPath.value.body;

const createCreateReactClassCallExpression = properties =>
j.callExpression(
j.memberExpression(
Expand All @@ -155,6 +157,7 @@ module.exports = function(j) {
findReactCreateClassExportDefault,
getComponentName,
getReactCreateClassSpec,
getClassExtendReactSpec,
hasMixins,
hasModule,
hasReact,
Expand Down

0 comments on commit c07bbb7

Please sign in to comment.