diff --git a/package.json b/package.json index 8d5a28cd..b749aaa8 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,8 @@ "testPathDirs": [ "transforms/__tests__" ] + }, + "devDependencies": { + "eslint-plugin-react": "^5.1.1" } } diff --git a/transforms/__testfixtures__/custom-sort/.eslintrc b/transforms/__testfixtures__/custom-sort/.eslintrc new file mode 100644 index 00000000..3fd957f9 --- /dev/null +++ b/transforms/__testfixtures__/custom-sort/.eslintrc @@ -0,0 +1,11 @@ +--- +plugins: + - react +rules: + no-undef: 0 + no-unused-vars: 0 + react/sort-comp: + - 0 + - order: + - everything-else + - render diff --git a/transforms/__testfixtures__/custom-sort/custom-sort.input.js b/transforms/__testfixtures__/custom-sort/custom-sort.input.js new file mode 100644 index 00000000..ad0764d4 --- /dev/null +++ b/transforms/__testfixtures__/custom-sort/custom-sort.input.js @@ -0,0 +1,21 @@ +var React = require('react/addons'); + +// comment above createClass +var MyComponent = React.createClass({ + render: function() { + return
; + }, + + mixins: [PureRenderMixin], + + // comment on componentDidMount + componentDidMount() {}, + + myOwnMethod(foo) { + // comment within method + }, + +}); + +/* comment at end */ +module.exports = MyComponent; diff --git a/transforms/__testfixtures__/custom-sort/custom-sort.output.js b/transforms/__testfixtures__/custom-sort/custom-sort.output.js new file mode 100644 index 00000000..7822c3fe --- /dev/null +++ b/transforms/__testfixtures__/custom-sort/custom-sort.output.js @@ -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 ; + }, + +}); + +/* comment at end */ +module.exports = MyComponent; diff --git a/transforms/__tests__/custom-sort.js b/transforms/__tests__/custom-sort.js new file mode 100644 index 00000000..f7b04510 --- /dev/null +++ b/transforms/__tests__/custom-sort.js @@ -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'); diff --git a/transforms/sort-comp.js b/transforms/sort-comp.js index f615c0db..14df8079 100644 --- a/transforms/sort-comp.js +++ b/transforms/sort-comp.js @@ -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); @@ -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; +}