Skip to content

Commit 62a5fc4

Browse files
committed
feat(config/eslintrc): allow devDependencies in all *.config.{js,ts} files
1 parent ea7a9c4 commit 62a5fc4

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/config/helpers/build-eslint.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const {rules} = require('eslint-config-airbnb-typescript/lib/shared')
2+
3+
const {hasAnyDep} = require('../../utils')
4+
const {testMatch} = require('../jest.config')
5+
6+
const withBaseConfig = base => variant =>
7+
require.resolve(base + (variant ? `/${variant}` : ''))
8+
9+
const airbnb = withBaseConfig('eslint-config-airbnb-typescript')
10+
const prettier = withBaseConfig('eslint-config-prettier')
11+
12+
const hasReact = hasAnyDep('react')
13+
14+
const parserRules = (typescript = false, react = false) => {
15+
const isOff = off => (off ? 'off' : 'error')
16+
17+
const propTypes = react ? {'react/prop-types': isOff(typescript)} : {}
18+
19+
return {
20+
'no-implied-eval': isOff(typescript),
21+
'no-throw-literal': isOff(typescript),
22+
'@typescript-eslint/no-implied-eval': isOff(!typescript),
23+
'@typescript-eslint/no-throw-literal': isOff(!typescript),
24+
...propTypes,
25+
}
26+
}
27+
28+
const buildConfig = ({withReact = false} = {}) => {
29+
const isReact = withReact || hasReact
30+
const ifReact = (t, f) => (isReact ? t : f)
31+
32+
return {
33+
plugins: ['prettier', 'jest', ifReact('react-hooks')].filter(Boolean),
34+
extends: [
35+
ifReact(airbnb(), airbnb('base')),
36+
'plugin:jest/recommended',
37+
prettier(),
38+
prettier('@typescript-eslint'),
39+
ifReact(prettier('react')),
40+
ifReact('plugin:react-hooks/recommended'),
41+
].filter(Boolean),
42+
rules: {
43+
'import/prefer-default-export': 'off',
44+
'import/no-extraneous-dependencies': [
45+
'error',
46+
{
47+
devDependencies: rules[
48+
'import/no-extraneous-dependencies'
49+
][1].devDependencies.concat([
50+
'jest/**',
51+
'e2e/**',
52+
'**/*.config.{js,ts}',
53+
]),
54+
optionalDependencies: false,
55+
},
56+
],
57+
'import/order': [
58+
'error',
59+
{
60+
alphabetize: {order: 'asc'},
61+
'newlines-between': 'always',
62+
pathGroups: [
63+
{pattern: 'src/**/*', group: 'parent', position: 'before'},
64+
{pattern: 'assets/**/*', group: 'parent', position: 'before'},
65+
],
66+
pathGroupsExcludedImportTypes: ['builtin'],
67+
},
68+
],
69+
'no-void': ['error', {allowAsStatement: true}],
70+
'prettier/prettier': 'error',
71+
'sort-imports': ['error', {ignoreDeclarationSort: true}],
72+
...parserRules(false, isReact),
73+
},
74+
overrides: [
75+
{
76+
files: ['**/*.ts?(x)'],
77+
extends: [
78+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
79+
],
80+
rules: {
81+
...parserRules(true, isReact),
82+
},
83+
},
84+
{
85+
files: testMatch,
86+
rules: {
87+
'no-empty': ['error', {allowEmptyCatch: true}],
88+
},
89+
},
90+
{
91+
files: ['**/*/__tests__/helpers/**/*', '**/*/__tests__/utils/**/*'],
92+
rules: {
93+
'jest/no-export': 'off',
94+
},
95+
},
96+
],
97+
}
98+
}
99+
100+
module.exports = {buildConfig}

0 commit comments

Comments
 (0)