From dc52160e1bf6840552fa59e36d80f1dd6642bc5f Mon Sep 17 00:00:00 2001 From: Jeremy Greer Date: Tue, 31 May 2016 14:11:24 -0400 Subject: [PATCH 1/3] read method order from eslintrc, does not read from inline file definition yet --- package.json | 3 +++ transforms/__testfixtures__/.eslintrc | 7 ++++++ .../__testfixtures__/sort-comp3.input.js | 12 ++++++++++ transforms/sort-comp.js | 23 ++++++++++++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) 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__/.eslintrc b/transforms/__testfixtures__/.eslintrc index c52e7db8..03dd816a 100644 --- a/transforms/__testfixtures__/.eslintrc +++ b/transforms/__testfixtures__/.eslintrc @@ -2,3 +2,10 @@ rules: no-undef: 0 no-unused-vars: 0 + react/sort-comp: + - 0 + - order: + - static-methods + - lifecycle + - everything-else + - render diff --git a/transforms/__testfixtures__/sort-comp3.input.js b/transforms/__testfixtures__/sort-comp3.input.js index 76dcb639..8c0c20fa 100644 --- a/transforms/__testfixtures__/sort-comp3.input.js +++ b/transforms/__testfixtures__/sort-comp3.input.js @@ -1,3 +1,15 @@ +/* +eslint "react/sort-comp": [ + 1, + { + order: [ + "render", + "everything-else" + ] + } +] +*/ + import React, { Component } from 'react/addons'; const propTypes = {}; diff --git a/transforms/sort-comp.js b/transforms/sort-comp.js index f615c0db..4b27f75d 100644 --- a/transforms/sort-comp.js +++ b/transforms/sort-comp.js @@ -36,7 +36,8 @@ module.exports = function(fileInfo, api, options) { const printOptions = options.printOptions || {quote: 'single', trailingComma: true}; - const methodsOrder = options.methodsOrder || defaultMethodsOrder; + const methodsOrder = getMethodsOrder(fileInfo, options); + console.log('using methodsOrder', methodsOrder); const root = j(fileInfo.source); @@ -173,3 +174,23 @@ 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; +} From b5e48c756fe8c260fa0c69cab6f48ad1defd6a18 Mon Sep 17 00:00:00 2001 From: Jeremy Greer Date: Tue, 31 May 2016 16:24:24 -0400 Subject: [PATCH 2/3] 44: pick up method order from .eslintrc if possible --- transforms/__testfixtures__/.eslintrc | 7 ------- .../__testfixtures__/custom-sort/.eslintrc | 11 ++++++++++ .../custom-sort/custom-sort.input.js | 21 +++++++++++++++++++ .../custom-sort/custom-sort.output.js | 21 +++++++++++++++++++ .../__testfixtures__/sort-comp3.input.js | 12 ----------- transforms/__tests__/custom-sort.js | 16 ++++++++++++++ transforms/sort-comp.js | 2 -- 7 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 transforms/__testfixtures__/custom-sort/.eslintrc create mode 100644 transforms/__testfixtures__/custom-sort/custom-sort.input.js create mode 100644 transforms/__testfixtures__/custom-sort/custom-sort.output.js create mode 100644 transforms/__tests__/custom-sort.js diff --git a/transforms/__testfixtures__/.eslintrc b/transforms/__testfixtures__/.eslintrc index 03dd816a..c52e7db8 100644 --- a/transforms/__testfixtures__/.eslintrc +++ b/transforms/__testfixtures__/.eslintrc @@ -2,10 +2,3 @@ rules: no-undef: 0 no-unused-vars: 0 - react/sort-comp: - - 0 - - order: - - static-methods - - lifecycle - - everything-else - - render 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/__testfixtures__/sort-comp3.input.js b/transforms/__testfixtures__/sort-comp3.input.js index 8c0c20fa..76dcb639 100644 --- a/transforms/__testfixtures__/sort-comp3.input.js +++ b/transforms/__testfixtures__/sort-comp3.input.js @@ -1,15 +1,3 @@ -/* -eslint "react/sort-comp": [ - 1, - { - order: [ - "render", - "everything-else" - ] - } -] -*/ - import React, { Component } from 'react/addons'; const propTypes = {}; 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 4b27f75d..7ebba7ac 100644 --- a/transforms/sort-comp.js +++ b/transforms/sort-comp.js @@ -37,7 +37,6 @@ module.exports = function(fileInfo, api, options) { options.printOptions || {quote: 'single', trailingComma: true}; const methodsOrder = getMethodsOrder(fileInfo, options); - console.log('using methodsOrder', methodsOrder); const root = j(fileInfo.source); @@ -182,7 +181,6 @@ function getMethodsOrderFromEslint(filePath) { }); const config = cli.getConfigForFile(filePath); - const { rules } = config; const sortCompRules = rules['react/sort-comp']; From c278ac1ca8a07d834ec3487c7532c95adf36d709 Mon Sep 17 00:00:00 2001 From: Jeremy Greer Date: Tue, 31 May 2016 18:52:01 -0400 Subject: [PATCH 3/3] 42: remove spaces to make more compact --- transforms/sort-comp.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/transforms/sort-comp.js b/transforms/sort-comp.js index 7ebba7ac..14df8079 100644 --- a/transforms/sort-comp.js +++ b/transforms/sort-comp.js @@ -176,14 +176,10 @@ function getCorrectIndex(methodsOrder, method) { function getMethodsOrderFromEslint(filePath) { const CLIEngine = require('eslint').CLIEngine; - const cli = new CLIEngine({ - useEslintrc: true, - }); - + const cli = new CLIEngine({ useEslintrc: true }); const config = cli.getConfigForFile(filePath); - const { rules } = config; + const {rules} = config; const sortCompRules = rules['react/sort-comp']; - return sortCompRules && sortCompRules[1].order; }