Skip to content

Commit 3f16492

Browse files
manixateSunil Pai
authored andcommitted
Add support for handling groups for method ordering in sort-comp codemod (#237)
1 parent 880f04e commit 3f16492

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
plugins:
3+
- react
4+
rules:
5+
no-undef: 0
6+
no-unused-vars: 0
7+
react/sort-comp:
8+
- 0
9+
- order:
10+
- lifecycle
11+
- everything-else
12+
- rendering
13+
groups:
14+
lifecycle:
15+
- componentDidMount
16+
rendering:
17+
- "/^render.+$/"
18+
- render
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var React = require('react/addons');
2+
3+
// comment above createClass
4+
var MyComponent = React.createClass({
5+
render: function() {
6+
return renderChild();
7+
},
8+
9+
mixins: [PureRenderMixin],
10+
11+
// comment on componentDidMount
12+
componentDidMount() {},
13+
14+
myOwnMethod(foo) {
15+
// comment within method
16+
},
17+
18+
renderChild: function() {
19+
return <div />;
20+
},
21+
});
22+
23+
/* comment at end */
24+
module.exports = MyComponent;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var React = require('react/addons');
2+
3+
// comment above createClass
4+
var MyComponent = React.createClass({
5+
// comment on componentDidMount
6+
componentDidMount() {},
7+
8+
mixins: [PureRenderMixin],
9+
10+
myOwnMethod(foo) {
11+
// comment within method
12+
},
13+
14+
renderChild: function() {
15+
return <div />;
16+
},
17+
18+
render: function() {
19+
return renderChild();
20+
},
21+
});
22+
23+
/* comment at end */
24+
module.exports = MyComponent;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright 2013-2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
'use strict';
12+
13+
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
14+
15+
// The test fixtures for this are in their own dir so it can customize eslint with method grouping.
16+
defineTest(__dirname, 'sort-comp', null, 'custom-sort-group/custom-sort-group');

transforms/sort-comp.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,35 @@ function getCorrectIndex(methodsOrder, method) {
192192
}
193193

194194
function getMethodsOrderFromEslint(filePath) {
195-
let order;
196195
const CLIEngine = require('eslint').CLIEngine;
197196
const cli = new CLIEngine({ useEslintrc: true });
198197
try {
199198
const config = cli.getConfigForFile(filePath);
200199
const {rules} = config;
201200
const sortCompRules = rules['react/sort-comp'];
202-
order = sortCompRules && sortCompRules[1].order;
201+
const ruleConfig = sortCompRules && sortCompRules[1];
202+
if (!ruleConfig) {
203+
return null;
204+
}
205+
206+
const order = ruleConfig.order;
207+
const groups = ruleConfig.groups || {};
208+
209+
let resolvedOrder = [];
210+
for (let i = 0; i < order.length; i++) {
211+
const entry = order[i];
212+
if (groups[entry]) {
213+
resolvedOrder = resolvedOrder.concat(groups[entry]);
214+
} else {
215+
resolvedOrder.push(entry);
216+
}
217+
}
218+
219+
return resolvedOrder;
203220
} catch (e) {
204221
// unable to get config for file
205222
}
206-
return order;
223+
return null;
207224
}
208225

209226
function getMethodsOrder(fileInfo, options) {

0 commit comments

Comments
 (0)