Skip to content

Commit 85f6f1e

Browse files
committedSep 25, 2023
fix: Refactor ESLint configurations for improved readability and consistency
This commit conducts a significant restructuring of the ESLint configurations. Numerous rule descriptions were modified for better clarity and consistency with the currently applied guidelines. Some of the rules were probably disabled or altered based on the current requirements. Also, introduced the usage of 'packageIsTypeModule' property from '@anolilab/package-json-utils' to manage the behavior of several rules depending on whether the module is a TypeScript module or not. This would help in making the linting process more adaptable to the type of module used. Signed-off-by: prisis <d.bannert@anolilab.de>
1 parent afee72c commit 85f6f1e

File tree

2 files changed

+131
-106
lines changed

2 files changed

+131
-106
lines changed
 

‎packages/eslint-config/src/config/plugins/import.ts

+125-105
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromRoot, hasTypescript } from "@anolilab/package-json-utils";
1+
import { fromRoot, hasTypescript, packageIsTypeModule } from "@anolilab/package-json-utils";
22
import type { Linter } from "eslint";
33

44
import { createConfigs } from "../../utils/create-config";
@@ -16,18 +16,16 @@ const config: Linter.Config = createConfigs([
1616
},
1717
plugins: ["import"],
1818
rules: {
19-
// Static analysis:
20-
21-
// ensure imports point to files/modules that can be resolved
19+
// enforce a consistent style for type specifiers (inline or top-level)
2220
// https://github.com/un-es/eslint-plugin-i/blob/d5fc8b670dc8e6903dbb7b0894452f60c03089f5/docs/rules/consistent-type-specifier-style.md
2321
"import/consistent-type-specifier-style": ["error", "prefer-top-level"],
2422

2523
// ensure named imports coupled with named exports
26-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/default.md#when-not-to-use-it
24+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/default.md#when-not-to-use-it
2725
"import/default": "off",
2826

29-
// ensure default import coupled with default export
30-
// https://github.com/benmosher/eslint-plugin-import/blob/ebafcbf59ec9f653b2ac2a0156ca3bcba0a7cf57/docs/rules/dynamic-import-chunkname.md
27+
// dynamic imports require a leading comment with a webpackChunkName
28+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/dynamic-import-chunkname.md
3129
"import/dynamic-import-chunkname": [
3230
"off",
3331
{
@@ -36,72 +34,75 @@ const config: Linter.Config = createConfigs([
3634
},
3735
],
3836

39-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/export.md
37+
// disallow invalid exports, e.g. multiple defaults
38+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/export.md
4039
"import/export": "error",
4140

42-
// Helpful warnings:
43-
44-
// disallow invalid exports, e.g. multiple defaults
45-
// https://github.com/benmosher/eslint-plugin-import/blob/98acd6afd04dcb6920b81330114e146dc8532ea4/docs/rules/exports-last.md
41+
// This rule enforces that all exports are declared at the bottom of the file.
42+
// https://github.com/import-js/eslint-plugin-import/blob/98acd6afd04dcb6920b81330114e146dc8532ea4/docs/rules/exports-last.md
4643
"import/exports-last": "error",
4744

48-
// do not allow a default import name to match a named export
49-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/extensions.md
45+
// Ensure consistent use of file extension within the import path
46+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/extensions.md
5047
"import/extensions": [
5148
"error",
5249
"ignorePackages",
53-
{
54-
cjs: "never",
55-
js: "never",
56-
jsx: "never",
57-
mjs: "never",
58-
},
50+
packageIsTypeModule
51+
? {
52+
cjs: "always",
53+
js: "always",
54+
jsx: "always",
55+
mjs: "always",
56+
}
57+
: {
58+
cjs: "never",
59+
js: "never",
60+
jsx: "never",
61+
mjs: "never",
62+
},
5963
],
6064

61-
// warn on accessing default export property names that are also named exports
62-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/first.md
65+
// disallow non-import statements appearing before import statements
66+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/first.md
6367
"import/first": "error",
6468

65-
// disallow use of jsdoc-marked-deprecated imports
66-
// https://github.com/benmosher/eslint-plugin-import/blob/44a038c06487964394b1e15b64f3bd34e5d40cde/docs/rules/group-exports.md
69+
// Reports when named exports are not grouped together in a single export declaration
70+
// or when multiple assignments to CommonJS module.exports or exports object are present
71+
// in a single file.
72+
// https://github.com/import-js/eslint-plugin-import/blob/44a038c06487964394b1e15b64f3bd34e5d40cde/docs/rules/group-exports.md
6773
"import/group-exports": "off",
6874

69-
// Forbid the use of extraneous packages
70-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-extraneous-dependencies.md
75+
// disallow non-import statements appearing before import statements
76+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/imports-first.md
7177
// deprecated: use `import/first`
7278
"import/imports-first": "off",
7379

74-
// Forbid mutable exports
75-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/max-dependencies.md
80+
// Forbid modules to have too many dependencies
81+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/max-dependencies.md
7682
"import/max-dependencies": ["off", { max: 10 }],
7783

78-
// Module systems:
79-
8084
// disallow require()
81-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/named.md#when-not-to-use-it
85+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/named.md#when-not-to-use-it
8286
"import/named": "error",
8387

8488
// disallow AMD require/define
85-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/namespace.md
89+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/namespace.md
8690
"import/namespace": "off",
8791

88-
// No Node.js builtin modules
89-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/newline-after-import.md
92+
// Require a newline after the last import/require in a group
93+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/newline-after-import.md
9094
"import/newline-after-import": "error",
9195

92-
// Style guide:
93-
94-
// disallow non-import statements appearing before import statements
95-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-absolute-path.md
96+
// Forbid import of modules using absolute paths
97+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-absolute-path.md
9698
"import/no-absolute-path": "error",
9799

98-
// disallow non-import statements appearing before import statements
99-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/imports-first.md
100-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-amd.md
100+
// disallow AMD require/define
101+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-amd.md
101102
"import/no-amd": "error",
102103

103-
// disallow duplicate imports
104-
// https://github.com/benmosher/eslint-plugin-import/blob/d9b712ac7fd1fddc391f7b234827925c160d956f/docs/rules/no-anonymous-default-export.md
104+
// Reports if a module's default export is unnamed
105+
// https://github.com/import-js/eslint-plugin-import/blob/d9b712ac7fd1fddc391f7b234827925c160d956f/docs/rules/no-anonymous-default-export.md
105106
"import/no-anonymous-default-export": [
106107
"off",
107108
{
@@ -114,37 +115,37 @@ const config: Linter.Config = createConfigs([
114115
},
115116
],
116117

117-
// disallow namespace imports
118-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-commonjs.md
119-
"import/no-commonjs": "off",
118+
// disallow require()
119+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-commonjs.md
120+
"import/no-commonjs": packageIsTypeModule ? "error" : "off",
120121

121-
// Ensure consistent use of file extension within the import path
122+
// Forbid cyclical dependencies between modules
122123
// https://medium.com/@steven-lemon182/are-typescript-barrel-files-an-anti-pattern-72a713004250
123124
"import/no-cycle": ["error", { maxDepth: "∞" }],
124125

125-
// ensure absolute imports are above relative imports and that unassigned imports are ignored
126-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md
127-
// https://github.com/benmosher/eslint-plugin-import/blob/44a038c06487964394b1e15b64f3bd34e5d40cde/docs/rules/no-default-export.md
126+
// forbid default exports. this is a terrible rule, do not use it.
127+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-default-export.md
128128
"import/no-default-export": "off",
129129

130-
// Require a newline after the last import/require in a group
131-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-deprecated.md
130+
// disallow use of jsdoc-marked-deprecated imports
131+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-deprecated.md
132132
"import/no-deprecated": "off",
133133

134-
// Require modules with a single export to use a default export
135-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-duplicates.md
134+
// disallow duplicate imports
135+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-duplicates.md
136136
"import/no-duplicates": "error",
137137

138-
// Restrict which files can be imported in a given folder
139-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-dynamic-require.md
138+
// Forbid require() calls with expressions
139+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-dynamic-require.md
140140
"import/no-dynamic-require": "error",
141141

142-
// Forbid modules to have too many dependencies
142+
// Reports the use of empty named import blocks.
143143
// https://github.com/un-es/eslint-plugin-i/blob/d5fc8b670dc8e6903dbb7b0894452f60c03089f5/docs/rules/no-empty-named-blocks.md
144144
"import/no-empty-named-blocks": "error",
145145

146-
// Forbid import of modules using absolute paths
147-
// are treated both as absolute paths, and relative to process.cwd()
146+
// Forbid the use of extraneous packages
147+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-extraneous-dependencies.md
148+
// paths are treated both as absolute paths, and relative to process.cwd()
148149
"import/no-extraneous-dependencies": [
149150
"error",
150151
{
@@ -184,99 +185,118 @@ const config: Linter.Config = createConfigs([
184185
},
185186
],
186187

187-
// Forbid require() calls with expressions
188-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-internal-modules.md
188+
// prevent importing the submodules of other modules
189+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-internal-modules.md
189190
"import/no-internal-modules": [
190191
"off",
191192
{
192193
allow: [],
193194
},
194195
],
195196

196-
// prevent importing the submodules of other modules
197-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-mutable-exports.md
197+
// Forbid mutable exports
198+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-mutable-exports.md
198199
"import/no-mutable-exports": "error",
199200

200201
// Warn if a module could be mistakenly parsed as a script by a consumer
201202
// leveraging Unambiguous JavaScript Grammar
202-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/unambiguous.md
203+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/unambiguous.md
203204
// this should not be enabled until this proposal has at least been *presented* to TC39.
204-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-named-as-default.md
205+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-named-as-default.md
205206
"import/no-named-as-default": "error",
206207

207-
// Forbid Webpack loader syntax in imports
208-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-named-as-default-member.md
208+
// warn on accessing default export property names that are also named exports
209+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-named-as-default-member.md
209210
"import/no-named-as-default-member": "error",
210211

211-
// Prevent unassigned imports
212-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-unassigned-import.md
213-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-named-default.md
212+
// Prevent importing the default as if it were named
213+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-named-default.md
214214
"import/no-named-default": "error",
215215

216-
// Prevent importing the default as if it were named
217-
// https://github.com/benmosher/eslint-plugin-import/blob/1ec80fa35fa1819e2d35a70e68fb6a149fb57c5e/docs/rules/no-named-export.md
216+
// Prohibit named exports. this is a terrible rule, do not use it.
217+
// https://github.com/import-js/eslint-plugin-import/blob/1ec80fa35fa1819e2d35a70e68fb6a149fb57c5e/docs/rules/no-named-export.md
218218
"import/no-named-export": "off",
219219

220-
// Reports if a module's default export is unnamed
221-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-namespace.md
220+
// disallow namespace imports
221+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-namespace.md
222222
"import/no-namespace": "error",
223223

224-
// This rule enforces that all exports are declared at the bottom of the file.
225-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-nodejs-modules.md
224+
// No Node.js builtin modules
225+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-nodejs-modules.md
226+
// TODO: enable?
226227
"import/no-nodejs-modules": "off",
227228

228-
// Reports when named exports are not grouped together in a single export declaration
229-
// or when multiple assignments to CommonJS const config or exports object are present
230-
// in a single file.
231-
// https://github.com/benmosher/eslint-plugin-import/blob/c34f14f67f077acd5a61b3da9c0b0de298d20059/docs/rules/no-relative-parent-imports.md
229+
// Use this rule to prevent imports to folders in relative parent paths.
230+
// https://github.com/import-js/eslint-plugin-import/blob/c34f14f67f077acd5a61b3da9c0b0de298d20059/docs/rules/no-relative-parent-imports.md
232231
"import/no-relative-parent-imports": "off",
233232

234-
// forbid default exports. this is a terrible rule, do not use it.
235-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-restricted-paths.md
233+
// Restrict which files can be imported in a given folder
234+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-restricted-paths.md
236235
"import/no-restricted-paths": "off",
237236

238-
// Prohibit named exports. this is a terrible rule, do not use it.
239-
// https://github.com/benmosher/eslint-plugin-import/blob/44a038c06487964394b1e15b64f3bd34e5d40cde/docs/rules/no-self-import.md
237+
// Forbid a module from importing itself
238+
// https://github.com/import-js/eslint-plugin-import/blob/44a038c06487964394b1e15b64f3bd34e5d40cde/docs/rules/no-self-import.md
240239
"import/no-self-import": "error",
241240

242241
// Forbid a module from importing itself
243242
// importing for side effects is perfectly acceptable, if you need side effects.
244243
"import/no-unassigned-import": "off",
245244

246-
// Forbid cyclical dependencies between modules
247-
// https://github.com/benmosher/eslint-plugin-import/blob/d81f48a2506182738409805f5272eff4d77c9348/docs/rules/no-cycle.md
248-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-unresolved.md
245+
// ensure imports point to files/modules that can be resolved
246+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-unresolved.md
249247
"import/no-unresolved": ["error", { caseSensitive: true, commonjs: true }],
250248

251-
// Ensures that there are no useless path segments
252-
// Note: you must disable the base rule as it can report incorrect errors
253-
"import/no-unused-modules": "off",
249+
// Reports modules without any exports, or with unused exports
250+
// https://github.com/import-js/eslint-plugin-import/blob/f63dd261809de6883b13b6b5b960e6d7f42a7813/docs/rules/no-unused-modules.md
251+
"import/no-unused-modules": [
252+
packageIsTypeModule ? "error" : "off",
253+
{
254+
ignoreExports: [],
255+
missingExports: true,
256+
unusedExports: true,
257+
},
258+
],
254259

255-
// dynamic imports require a leading comment with a webpackChunkName
256-
// https://github.com/benmosher/eslint-plugin-import/blob/ebafcbf59ec9f653b2ac2a0156ca3bcba0a7cf57/docs/rules/no-useless-path-segments.md
257-
"import/no-useless-path-segments": ["error", { commonjs: true, noUselessIndex: true }],
260+
// Reports the use of import declarations with CommonJS exports in any module except for the main module.
261+
// https://github.com/import-js/eslint-plugin-import/blob/1012eb951767279ce3b540a4ec4f29236104bb5b/docs/rules/no-import-module-exports.md
262+
"import/no-import-module-exports": [
263+
packageIsTypeModule ? "off" : "error",
264+
{
265+
exceptions: [],
266+
},
267+
],
258268

259-
// Use this rule to prevent imports to folders in relative parent paths.
260-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-webpack-loader-syntax.md
269+
// Use this rule to prevent importing packages through relative paths.
270+
// https://github.com/import-js/eslint-plugin-import/blob/1012eb951767279ce3b540a4ec4f29236104bb5b/docs/rules/no-relative-packages.md
271+
"import/no-relative-packages": "error",
272+
273+
// Ensures that there are no useless path segments
274+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-useless-path-segments.md
275+
"import/no-useless-path-segments": ["error", { commonjs: !packageIsTypeModule, noUselessIndex: true }],
276+
277+
// Forbid Webpack loader syntax in imports
278+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-webpack-loader-syntax.md
261279
"import/no-webpack-loader-syntax": "error",
262280

263-
// Reports modules without any exports, or with unused exports
264-
// https://github.com/benmosher/eslint-plugin-import/blob/f63dd261809de6883b13b6b5b960e6d7f42a7813/docs/rules/no-unused-modules.md
281+
// ensure absolute imports are above relative imports and that unassigned imports are ignored
282+
// https://github.com/import-js/eslint-plugin-import/blob/f63dd261809de6883b13b6b5b960e6d7f42a7813/docs/rules/no-unused-modules.md
265283
// simple-import-sort does this better
266284
"import/order": "off",
267285

268-
// enforce a consistent style for type specifiers (inline or top-level)
269-
// https://github.com/un-es/eslint-plugin-i/blob/d5fc8b670dc8e6903dbb7b0894452f60c03089f5/docs/rules/consistent-type-specifier-style.md
270-
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/prefer-default-export.md
286+
// Require modules with a single export to use a default export
287+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/prefer-default-export.md
271288
"import/prefer-default-export": "error",
272289

273-
// Reports the use of empty named import blocks.
290+
// Warn if a module could be mistakenly parsed as a script by a consumer
291+
// leveraging Unambiguous JavaScript Grammar
292+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/unambiguous.md
293+
// this should not be enabled until this proposal has at least been *presented* to TC39.
274294
// At the moment, it's not a thing.
275295
"import/unambiguous": "off",
276296
},
277297
settings: {
278298
"import/core-modules": [],
279-
// https://github.com/un-es/eslint-plugin-i/blob/master/docs/rules/extensions.md
299+
// https://github.com/un-es/eslint-plugin-i/blob/main/docs/rules/extensions.md
280300
"import/extensions": [".js", ".cjs", ".mjs", ".jsx"],
281301
// Ensure consistent use of file extension within the import path
282302
"import/ignore": ["\\.(coffee|scss|css|less|hbs|svg|json)$"],
@@ -304,7 +324,7 @@ const config: Linter.Config = createConfigs([
304324
// Does not work when the TS definition exports a default const.
305325
"import/default": "off",
306326

307-
// Disabled because of https://github.com/benmosher/eslint-plugin-import/issues/1590
327+
// Disabled because of https://github.com/import-js/eslint-plugin-import/issues/1590
308328
"import/export": "off",
309329

310330
// Disabled as it doesn't work with TypeScript.
@@ -320,10 +340,10 @@ const config: Linter.Config = createConfigs([
320340
},
321341
],
322342

323-
// This issue and some others: https://github.com/benmosher/eslint-plugin-import/issues/1341
343+
// This issue and some others: https://github.com/import-js/eslint-plugin-import/issues/1341
324344
"import/named": "off",
325345

326-
// Enforce consistent usage of type imports.
346+
// ensure imports point to files/modules that can be resolved
327347
"import/no-unresolved": "off",
328348
},
329349
settings: {

‎packages/eslint-config/src/config/variables.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ const config: Linter.Config = createConfigs([
3131
message: "Use Number.isNaN instead https://github.com/airbnb/javascript#standard-library--isnan",
3232
name: "isNaN",
3333
},
34-
...confusingBrowserGlobals,
34+
...confusingBrowserGlobals.map((g) => {
35+
return {
36+
name: g,
37+
message: `Use window.${g} instead. https://github.com/facebook/create-react-app/blob/HEAD/packages/confusing-browser-globals/README.md`,
38+
};
39+
}),
3540
],
3641

3742
// disallow declaration of variables already declared in the outer scope

0 commit comments

Comments
 (0)
Please sign in to comment.