From e2590740faf770bb286180eb4ed0551ac06c8bd0 Mon Sep 17 00:00:00 2001 From: Jamie Rolfs Date: Mon, 30 Sep 2019 15:15:24 -0700 Subject: [PATCH] feat(config/eslint): provide React-specific configuration The existing configuration attempts to auto-detect whether React-specific configurations should be loaded, however this detection does not work properly in monorepos. This provides 'eslint-react' which unconditionally loads React-specific configurations. --- eslint-react.js | 1 + src/config/__tests__/umbrella.js | 1 + src/config/eslintrc-react.js | 3 ++ src/config/eslintrc.js | 45 ++-------------------------- src/config/helpers/eslint.js | 50 ++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 43 deletions(-) create mode 100644 eslint-react.js create mode 100644 src/config/eslintrc-react.js create mode 100644 src/config/helpers/eslint.js diff --git a/eslint-react.js b/eslint-react.js new file mode 100644 index 00000000..76fd11a5 --- /dev/null +++ b/eslint-react.js @@ -0,0 +1 @@ +module.exports = require('./dist/config/eslintrc-react') diff --git a/src/config/__tests__/umbrella.js b/src/config/__tests__/umbrella.js index c1e9c065..9a161d3b 100644 --- a/src/config/__tests__/umbrella.js +++ b/src/config/__tests__/umbrella.js @@ -2,6 +2,7 @@ test('requiring some files does not blow up', () => { require('../babel-transform') require('../babelrc') require('../eslintrc') + require('../eslintrc-react') require('../jest.config') require('../lintstagedrc') require('../prettierrc') diff --git a/src/config/eslintrc-react.js b/src/config/eslintrc-react.js new file mode 100644 index 00000000..608378db --- /dev/null +++ b/src/config/eslintrc-react.js @@ -0,0 +1,3 @@ +const {buildConfig} = require('./helpers/eslint') + +module.exports = buildConfig({withReact: true}) diff --git a/src/config/eslintrc.js b/src/config/eslintrc.js index 580d232b..bdf9ebf6 100644 --- a/src/config/eslintrc.js +++ b/src/config/eslintrc.js @@ -1,44 +1,3 @@ -const {rules} = require('eslint-config-airbnb-typescript/lib/shared') +const {buildConfig} = require('./helpers/eslint') -const {ifAnyDep} = require('../utils') -const {testMatch} = require('./jest.config') - -const withBaseConfig = base => variant => - require.resolve(base + (variant ? `/${variant}` : '')) - -const airbnb = withBaseConfig('eslint-config-airbnb-typescript') -const prettier = withBaseConfig('eslint-config-prettier') - -module.exports = { - plugins: ['prettier', 'jest', ifAnyDep('react', 'react-hooks')].filter( - Boolean, - ), - extends: [ - ifAnyDep('react', airbnb(), airbnb('base')), - 'plugin:jest/recommended', - prettier(), - prettier('@typescript-eslint'), - ifAnyDep('react', prettier('react')), - ].filter(Boolean), - rules: { - 'prettier/prettier': 'error', - 'import/prefer-default-export': 'off', - 'import/no-extraneous-dependencies': [ - 'error', - { - devDependencies: rules[ - 'import/no-extraneous-dependencies' - ][1].devDependencies.concat('jest/**'), - optionalDependencies: false, - }, - ], - }, - overrides: [ - { - files: testMatch, - rules: { - 'no-empty': ['error', {allowEmptyCatch: true}], - }, - }, - ], -} +module.exports = buildConfig() diff --git a/src/config/helpers/eslint.js b/src/config/helpers/eslint.js new file mode 100644 index 00000000..f1d95fbe --- /dev/null +++ b/src/config/helpers/eslint.js @@ -0,0 +1,50 @@ +const {rules} = require('eslint-config-airbnb-typescript/lib/shared') + +const {hasAnyDep} = require('../../utils') +const {testMatch} = require('../jest.config') + +const withBaseConfig = base => variant => + require.resolve(base + (variant ? `/${variant}` : '')) + +const airbnb = withBaseConfig('eslint-config-airbnb-typescript') +const prettier = withBaseConfig('eslint-config-prettier') + +const hasReact = hasAnyDep('react') + +const buildConfig = ({withReact = false} = {}) => { + const ifReact = (t, f) => (withReact || hasReact ? t : f) + + return { + plugins: ['prettier', 'jest', ifReact('react-hooks')].filter(Boolean), + extends: [ + ifReact(airbnb(), airbnb('base')), + 'plugin:jest/recommended', + prettier(), + prettier('@typescript-eslint'), + ifReact(prettier('react')), + ].filter(Boolean), + rules: { + 'prettier/prettier': 'error', + 'import/prefer-default-export': 'off', + 'import/no-extraneous-dependencies': [ + 'error', + { + devDependencies: rules[ + 'import/no-extraneous-dependencies' + ][1].devDependencies.concat('jest/**'), + optionalDependencies: false, + }, + ], + }, + overrides: [ + { + files: testMatch, + rules: { + 'no-empty': ['error', {allowEmptyCatch: true}], + }, + }, + ], + } +} + +module.exports = {buildConfig}