Skip to content

Commit 2968db1

Browse files
authoredJul 13, 2023
refactor!: split into separate configs (#246)
1 parent e139a04 commit 2968db1

File tree

6 files changed

+223
-120
lines changed

6 files changed

+223
-120
lines changed
 

‎README.md

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# bjerk/eslint-config
22

3-
These are Bjerk's eslint config, used on most of our projects.
4-
5-
We'd **actually** love more users of our config, so if you have ideas on
6-
improvements – please file an issue or pull request!
3+
A reusable Eslint config built by and maintained by Bjerk.
74

85
```shell
96
yarn add @bjerk/eslint-config
@@ -35,31 +32,47 @@ To make all this config work, you only need to add this to `package.json`:
3532
}
3633
```
3734

35+
You can also use parts of this configuration if you want to. For example, if you
36+
only want to use our base config, you can do this:
37+
38+
```json
39+
{
40+
"extends": "@bjerk/eslint-config/base"
41+
}
42+
```
43+
44+
These are the available configs:
45+
46+
- `@bjerk/eslint-config`
47+
- `@bjerk/eslint-config/base`
48+
- `@bjerk/eslint-config/import`
49+
- `@bjerk/eslint-config/typescript`
50+
51+
**Note**: The main `@bjerk/eslint-config` config includes all the others, but
52+
also `prettier` (and `eslint-config-prettier`).
53+
3854
Tip: We often use this along with `@simenandre/prettier`, a shared Prettier
39-
config.
55+
config. Typically, we recommend letting `prettier` handle all formatting, and
56+
`eslint` handle all linting.
4057

41-
## Migrate from v2?
58+
## Motivation
4259

43-
We removed `jest`-related rules in v3, essentially making `jest` an optional
44-
dependency. In our experience, we don't necessarily want to use Jest for every
45-
project anymore. Versions below v3 **require** `jest`.
60+
We want to have a consistent code style, and we want to promote readable and
61+
maintainable code. We also want to avoid bugs and errors, and we want to have a
62+
good developer experience.
4663

47-
Here are the steps to get it working [again]:
64+
This is our take on making that happen with Eslint!
4865

49-
Run this in your terminal:
66+
## Contributing
5067

51-
```shell
52-
yarn add -D eslint-plugin-jest
53-
```
68+
In comparison to many other eslint configurations, we welcome contributions to
69+
this config. If you have any ideas on how to improve it, please open an issue or
70+
a pull request!
5471

55-
```json
56-
{
57-
"eslintConfig": {
58-
"extends": ["@bjerk/eslint-config", "plugin:jest/recommended"],
59-
"plugins": ["jest"]
60-
}
61-
}
62-
```
72+
This isn't supposed to be a _Bjerk only_ config, but rather a config that
73+
everyone can use. We want to make it as good as possible, and we want to make it
74+
as useful as possible. If you agree with our motivation, we'd love to have you
75+
on board!
6376

6477
## Thanks
6578

‎base.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const eslintConfigBasic = {
2+
extends: [
3+
'eslint:recommended',
4+
'plugin:eslint-comments/recommended',
5+
'plugin:promise/recommended',
6+
],
7+
plugins: ['unicorn'],
8+
rules: {
9+
/**
10+
* Enforce consistent brace style for all control statements
11+
* @see https://eslint.org/docs/rules/curly
12+
*/
13+
curly: 'error',
14+
15+
/**
16+
* Require the use of === and !==
17+
* @see https://eslint.org/docs/rules/eqeqeq
18+
*/
19+
eqeqeq: ['error', 'smart'],
20+
21+
'no-console': 'error',
22+
'no-restricted-globals': [
23+
'error',
24+
'event',
25+
'isNaN',
26+
'location',
27+
'name',
28+
'parseInt',
29+
],
30+
'no-return-await': 'error',
31+
32+
/**
33+
* Enforce the consistent use of either backticks, double, or single quotes
34+
*
35+
* In our case, we want to use single quotes for strings.
36+
* @see https://eslint.org/docs/latest/rules/quotes
37+
*/
38+
quotes: [
39+
'error',
40+
'single',
41+
{
42+
avoidEscape: true,
43+
allowTemplateLiterals: false,
44+
},
45+
],
46+
47+
/**
48+
* Enforce consistent spacing after the \// or \/* in a comment
49+
*
50+
* @see https://eslint.org/docs/latest/rules/spaced-comment
51+
*/
52+
'spaced-comment': [
53+
'error',
54+
'always',
55+
{
56+
exceptions: ['-', '=', '#__PURE__'],
57+
markers: ['/'],
58+
},
59+
],
60+
61+
/**
62+
* Enforces all linted files to have their names in kebab-case.
63+
* E.g. my-component.tsx
64+
* @see https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/filename-case.md
65+
*/
66+
'unicorn/filename-case': [
67+
'error',
68+
{
69+
case: 'kebabCase',
70+
},
71+
],
72+
73+
'eslint-comments/no-unused-disable': 'error',
74+
},
75+
overrides: [
76+
{
77+
files: ['**/*.test.ts'],
78+
rules: {
79+
quotes: ['error', 'single', { allowTemplateLiterals: true }],
80+
},
81+
},
82+
],
83+
};
84+
85+
// eslint-disable-next-line no-undef
86+
module.exports = eslintConfigBasic;

‎import.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const eslintConfigImport = {
2+
plugins: ['import'],
3+
rules: {
4+
'import/no-default-export': 'error',
5+
'import/no-duplicates': 'error',
6+
'import/no-extraneous-dependencies': 'error',
7+
'import/no-unassigned-import': 'error',
8+
'import/order': [
9+
'error',
10+
{
11+
alphabetize: {
12+
order: 'asc',
13+
},
14+
'newlines-between': 'never',
15+
},
16+
],
17+
'sort-imports': [
18+
'error',
19+
{
20+
ignoreDeclarationSort: true,
21+
},
22+
],
23+
},
24+
overrides: [
25+
{
26+
files: 'jest.config.*',
27+
rules: {
28+
'import/no-default-export': 'off',
29+
},
30+
},
31+
],
32+
};
33+
34+
// eslint-disable-next-line no-undef
35+
module.exports = eslintConfigImport;

‎index.js

+1-98
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,5 @@
11
const eslintConfig = {
2-
extends: [
3-
'eslint:recommended',
4-
'plugin:@typescript-eslint/eslint-recommended',
5-
'plugin:@typescript-eslint/recommended',
6-
'plugin:eslint-comments/recommended',
7-
'plugin:promise/recommended',
8-
'prettier',
9-
],
10-
parser: '@typescript-eslint/parser',
11-
plugins: ['@typescript-eslint', 'import', 'unicorn'],
12-
rules: {
13-
curly: 'error',
14-
'@typescript-eslint/explicit-module-boundary-types': 'warn',
15-
'@typescript-eslint/member-ordering': 'error',
16-
'@typescript-eslint/no-explicit-any': 'error',
17-
'@typescript-eslint/no-unused-expressions': 'error',
18-
'@typescript-eslint/no-unused-vars': [
19-
'error',
20-
{
21-
argsIgnorePattern: '^_',
22-
ignoreRestSiblings: true,
23-
caughtErrors: 'all',
24-
},
25-
],
26-
'@typescript-eslint/prefer-optional-chain': 'error',
27-
'@typescript-eslint/prefer-ts-expect-error': 'error',
28-
eqeqeq: ['error', 'smart'],
29-
'eslint-comments/no-unused-disable': 'error',
30-
'import/no-default-export': 'error',
31-
'import/no-duplicates': 'error',
32-
'import/no-extraneous-dependencies': 'error',
33-
'import/no-unassigned-import': 'error',
34-
'import/order': [
35-
'error',
36-
{
37-
alphabetize: {
38-
order: 'asc',
39-
},
40-
'newlines-between': 'never',
41-
},
42-
],
43-
'no-console': 'error',
44-
'no-restricted-globals': [
45-
'error',
46-
'event',
47-
'isNaN',
48-
'location',
49-
'name',
50-
'parseInt',
51-
],
52-
'no-return-await': 'error',
53-
quotes: [
54-
'error',
55-
'single',
56-
{
57-
avoidEscape: true,
58-
allowTemplateLiterals: false,
59-
},
60-
],
61-
'sort-imports': [
62-
'error',
63-
{
64-
ignoreDeclarationSort: true,
65-
},
66-
],
67-
'spaced-comment': [
68-
'error',
69-
'always',
70-
{
71-
exceptions: ['-', '=', '#__PURE__'],
72-
markers: ['/'],
73-
},
74-
],
75-
'unicorn/filename-case': [
76-
'error',
77-
{
78-
case: 'kebabCase',
79-
},
80-
],
81-
},
82-
overrides: [
83-
{
84-
files: ['**/*.test.ts'],
85-
rules: {
86-
'@typescript-eslint/ban-ts-comment': 'off',
87-
'@typescript-eslint/no-explicit-any': 'off',
88-
'@typescript-eslint/no-unused-vars': 'off',
89-
'@typescript-eslint/no-var-requires': 'off',
90-
quotes: ['error', 'single', { allowTemplateLiterals: true }],
91-
},
92-
},
93-
{
94-
files: 'jest.config.*',
95-
rules: {
96-
'import/no-default-export': 'off',
97-
},
98-
},
99-
],
2+
extends: ['./base', './typescript', './import', 'prettier'],
1003
};
1014

1025
// eslint-disable-next-line no-undef

‎package.json

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"author": "Bjerk AS",
66
"repository": "github.com:bjerkio/eslint-config",
77
"main": "index.js",
8+
"files": [
9+
"base.js",
10+
"import.js",
11+
"typescript.js"
12+
],
813
"scripts": {
914
"format": "prettier --write .",
1015
"lint": "eslint -c index.js --ext .ts,.tsx,.js,.jsx ."

‎typescript.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const eslintConfigTypescript = {
2+
extends: [
3+
'plugin:@typescript-eslint/eslint-recommended',
4+
'plugin:@typescript-eslint/recommended',
5+
],
6+
parser: '@typescript-eslint/parser',
7+
plugins: ['@typescript-eslint'],
8+
rules: {
9+
/**
10+
* Require explicit return types on functions and class methods
11+
*
12+
* @see https://typescript-eslint.io/rules/explicit-module-boundary-types
13+
*/
14+
'@typescript-eslint/explicit-module-boundary-types': 'warn',
15+
16+
/**
17+
* Require a consistent member declaration order.
18+
*
19+
* @see https://typescript-eslint.io/rules/member-ordering
20+
*/
21+
'@typescript-eslint/member-ordering': 'error',
22+
23+
/**
24+
* Disallow unused variables.
25+
*
26+
* Exceptions:
27+
* - Variables that start with an underscore
28+
* - Rest siblings
29+
* - Caught errors
30+
*
31+
* @see https://typescript-eslint.io/rules/no-unused-vars
32+
*/
33+
'@typescript-eslint/no-unused-vars': [
34+
'error',
35+
{
36+
argsIgnorePattern: '^_',
37+
ignoreRestSiblings: true,
38+
caughtErrors: 'all',
39+
},
40+
],
41+
42+
'@typescript-eslint/no-explicit-any': 'error',
43+
'@typescript-eslint/no-unused-expressions': 'error',
44+
'@typescript-eslint/prefer-optional-chain': 'error',
45+
'@typescript-eslint/prefer-ts-expect-error': 'error',
46+
},
47+
overrides: [
48+
{
49+
files: ['**/*.test.ts'],
50+
rules: {
51+
'@typescript-eslint/ban-ts-comment': 'off',
52+
'@typescript-eslint/no-explicit-any': 'off',
53+
'@typescript-eslint/no-unused-vars': 'off',
54+
'@typescript-eslint/no-var-requires': 'off',
55+
},
56+
},
57+
],
58+
};
59+
60+
// eslint-disable-next-line no-undef
61+
module.exports = eslintConfigTypescript;

0 commit comments

Comments
 (0)
Please sign in to comment.