@tim-code/eslint-config 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -69
- package/eslint.config.js +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
# eslint-config
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This config assumes that code can use Node globals unless it is in a `frontend` directory. See below to change this.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Version 2.X.X of this package only supports ESLint 9 (flat config). Use 1.4.X if you are using ESLint 8.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"@tim-code"
|
|
11
|
-
],
|
|
12
|
-
"root": true,
|
|
13
|
-
"ignorePatterns": []
|
|
14
|
-
}
|
|
15
|
-
```
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
`npm install --save-dev @tim-code/eslint-config`
|
|
16
10
|
|
|
17
|
-
|
|
11
|
+
Then create `eslint.config.js` at project root with the following:
|
|
18
12
|
|
|
19
|
-
|
|
13
|
+
```js
|
|
14
|
+
export { default } from "@tim-code/eslint-config"
|
|
15
|
+
```
|
|
20
16
|
|
|
21
17
|
## React
|
|
22
18
|
|
|
@@ -24,72 +20,44 @@ To fully support React/JSX, install:
|
|
|
24
20
|
|
|
25
21
|
`npm install --save-dev eslint-plugin-react`
|
|
26
22
|
|
|
27
|
-
Then in `
|
|
23
|
+
Then in `eslint.config.js` in package.json:
|
|
28
24
|
|
|
29
|
-
```
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
]
|
|
34
|
-
```
|
|
25
|
+
```js
|
|
26
|
+
import base from "@tim-code/eslint-config"
|
|
27
|
+
import reactPlugin from "eslint-plugin-react"
|
|
28
|
+
import { defineConfig } from "eslint/config"
|
|
35
29
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
]
|
|
30
|
+
export default defineConfig([
|
|
31
|
+
reactPlugin.configs.flat.recommended,
|
|
32
|
+
...base,
|
|
33
|
+
// optionally, allow null in React code
|
|
34
|
+
{
|
|
35
|
+
files: ["**/*.{jsx,tsx}"],
|
|
36
|
+
rules: { "no-restricted-syntax": "off" },
|
|
37
|
+
},
|
|
38
|
+
])
|
|
42
39
|
```
|
|
43
40
|
|
|
44
41
|
## Make src directory use "browser" globals instead of "node" globals
|
|
45
42
|
|
|
46
|
-
In `
|
|
43
|
+
In `eslint.config.js` in package.json:
|
|
47
44
|
|
|
48
|
-
```
|
|
49
|
-
|
|
45
|
+
```js
|
|
46
|
+
export default defineConfig([
|
|
47
|
+
...base,
|
|
50
48
|
{
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
Instead, if there are no JS files in the project directory that require Node:
|
|
63
|
-
|
|
64
|
-
```json
|
|
65
|
-
"env": {
|
|
66
|
-
"node": false,
|
|
67
|
-
"browser": true
|
|
68
|
-
}
|
|
49
|
+
files: ["src/**"],
|
|
50
|
+
languageOptions: {
|
|
51
|
+
globals: {
|
|
52
|
+
...globals.browser,
|
|
53
|
+
// to turn off previously added Node globals:
|
|
54
|
+
...Object.fromEntries(Object.keys(globals.node).map((key) => [key, "off"])),
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
])
|
|
69
59
|
```
|
|
70
60
|
|
|
71
61
|
## Why not eslint-babel?
|
|
72
62
|
|
|
73
63
|
`eslint-babel` is a more flexible parser but using it is a bit more complex (requires configuration) and introduces another dependency.
|
|
74
|
-
|
|
75
|
-
## ESLint 9
|
|
76
|
-
|
|
77
|
-
ESLint 9, released 2024-04-05, switched to using "flat config". This enabled a more script-based integration with plugins (similar to other build tools like Webpack) at the expense of supporting different ways to specify the config as a JSON object. As a result, ESLint 9 dropped support for "eslintConfig" in package.json. See: https://github.com/eslint/eslint/discussions/18131.
|
|
78
|
-
|
|
79
|
-
ESLint 8 reached end-of-life on 2024-10-05. The last 8.X.X release was 2024-09-16. How should projects that followed this README migrate to ESLint 9?
|
|
80
|
-
|
|
81
|
-
### Option 1 - Create an eslint.config.js
|
|
82
|
-
|
|
83
|
-
It's intended that projects that use ESLint 9 will have their own `eslint.config.js` file, even if they are ultimately just using some other ESLint config. See: https://eslint.org/docs/latest/use/getting-started.
|
|
84
|
-
|
|
85
|
-
This does result in cluttering the top-level of the project with yet-another config file.
|
|
86
|
-
|
|
87
|
-
### Option 2 - Disable Flat Config
|
|
88
|
-
|
|
89
|
-
It is possible to use old config resolution by setting the environment variable, `ESLINT_USE_FLAT_CONFIG=false`, or when using VSCode ESLint extension, `{ "eslint.useFlatConfig": false }`. At first glance, it seems like this provides a way forward: old repos can use ESLint 9 without rewriting their configs. Unfortunately, this has a significant drawback. Setting this at a user-level scope in VSCode will result in not being able to use flat config in all open projects. This is because when this is false, flat config is completely ignored. Basically, it's impossible to work in some repos that are using ESLint 9 with legacy config and some repos that are using ESLint 9 with flat config, without repo-level overrides.
|
|
90
|
-
|
|
91
|
-
A better approach would be for repos that want to use ESLint 9 with legacy config to specify `.vscode/settings.json`. However, if a repo doesn't have that file already, this still amounts to the same as Option 1: adding a config file at the top-level of the project.
|
|
92
|
-
|
|
93
|
-
### Other Options?
|
|
94
|
-
|
|
95
|
-
Purposefully so, there are less ways to specify ESLint config in ESLint 9. You could try to pass a command line argument specifying a different config file. However, the VSCode extension doesn't run ESLint through the command line, so you would have to specify it through preferences, similar to Option 2.
|
package/eslint.config.js
CHANGED
|
@@ -67,6 +67,12 @@ export default defineConfig([
|
|
|
67
67
|
message:
|
|
68
68
|
'Usage of "null" is discouraged except when used because of legacy APIs; use "undefined" instead',
|
|
69
69
|
},
|
|
70
|
+
{
|
|
71
|
+
selector:
|
|
72
|
+
'VariableDeclarator[init.type="ObjectExpression"][init.properties.length=0]',
|
|
73
|
+
message:
|
|
74
|
+
"Initializing a variable to an empty object is indicative of an object-as-map pattern; use `Object.create(null)` instead",
|
|
75
|
+
},
|
|
70
76
|
],
|
|
71
77
|
|
|
72
78
|
// modified rules
|
|
@@ -165,6 +171,7 @@ export default defineConfig([
|
|
|
165
171
|
files: ["**/frontend/**"],
|
|
166
172
|
languageOptions: {
|
|
167
173
|
globals: {
|
|
174
|
+
...Object.fromEntries(Object.keys(globals.node).map((key) => [key, "off"])),
|
|
168
175
|
...globals.browser,
|
|
169
176
|
},
|
|
170
177
|
},
|