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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"testPathDirs": [
"transforms/__tests__"
]
},
"devDependencies": {
"eslint-plugin-react": "^5.1.1"
}
}
11 changes: 11 additions & 0 deletions transforms/__testfixtures__/custom-sort/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
plugins:
- react
rules:
no-undef: 0
no-unused-vars: 0
react/sort-comp:
- 0
- order:
- everything-else
- render
21 changes: 21 additions & 0 deletions transforms/__testfixtures__/custom-sort/custom-sort.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var React = require('react/addons');

// comment above createClass
var MyComponent = React.createClass({
render: function() {
return <div />;
},

mixins: [PureRenderMixin],

// comment on componentDidMount
componentDidMount() {},

myOwnMethod(foo) {
// comment within method
},

});

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

// comment above createClass
var MyComponent = React.createClass({
// comment on componentDidMount
componentDidMount() {},

mixins: [PureRenderMixin],

myOwnMethod(foo) {
// comment within method
},

render: function() {
return <div />;
},

});

/* comment at end */
module.exports = MyComponent;
16 changes: 16 additions & 0 deletions transforms/__tests__/custom-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

const defineTest = require('jscodeshift/dist/testUtils').defineTest;

// The test fixtures for this are in their own dir so it can customize eslint.
defineTest(__dirname, 'sort-comp', null, 'custom-sort/custom-sort');
17 changes: 16 additions & 1 deletion transforms/sort-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = function(fileInfo, api, options) {
const printOptions =
options.printOptions || {quote: 'single', trailingComma: true};

const methodsOrder = options.methodsOrder || defaultMethodsOrder;
const methodsOrder = getMethodsOrder(fileInfo, options);

const root = j(fileInfo.source);

Expand Down Expand Up @@ -173,3 +173,18 @@ function getCorrectIndex(methodsOrder, method) {
return Infinity;
}
}

function getMethodsOrderFromEslint(filePath) {
const CLIEngine = require('eslint').CLIEngine;
const cli = new CLIEngine({ useEslintrc: true });
const config = cli.getConfigForFile(filePath);
const {rules} = config;
const sortCompRules = rules['react/sort-comp'];
return sortCompRules && sortCompRules[1].order;
}

function getMethodsOrder(fileInfo, options) {
return options.methodsOrder
|| getMethodsOrderFromEslint(fileInfo.path)
|| defaultMethodsOrder;
}