Skip to content
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: 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