Skip to content

Commit 8c14c68

Browse files
committed
Set up ESLint to avoid errors (#2850)
1 parent f84206b commit 8c14c68

File tree

4 files changed

+5876
-1827
lines changed

4 files changed

+5876
-1827
lines changed

assets/eslint.config.js

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
import globals from 'globals';
5+
import jsPlugin from '@eslint/js';
6+
import tsPlugin from 'typescript-eslint';
7+
import reactPlugin from 'eslint-plugin-react';
8+
import reactCompilerPlugin from 'eslint-plugin-react-compiler';
9+
import reactHooksPlugin from 'eslint-plugin-react-hooks';
10+
import jsxA11yPlugin from 'eslint-plugin-jsx-a11y';
11+
import promisePlugin from 'eslint-plugin-promise';
12+
import compatPlugin from 'eslint-plugin-compat';
13+
import commentsPlugin from '@eslint-community/eslint-plugin-eslint-comments/configs';
14+
import importPlugin from 'eslint-plugin-import';
15+
16+
const __dirname = dirname(fileURLToPath(import.meta.url));
17+
18+
const javascriptExtensions = ['js', 'jsx'];
19+
const typescriptExtensions = ['ts', 'tsx'];
20+
const jsxExtensions = ['jsx', 'tsx'];
21+
const baseExtensions = [...javascriptExtensions, ...typescriptExtensions];
22+
23+
const commonJsExtensions = (
24+
/** @type readonly string[] */
25+
exts
26+
) => exts.map(ext => `c${ext}`);
27+
const esmExtensions = (
28+
/** @type readonly string[] */
29+
exts
30+
) => exts.map(ext => `m${ext}`);
31+
32+
const allExtensions = [
33+
...baseExtensions,
34+
...commonJsExtensions(baseExtensions),
35+
...esmExtensions(baseExtensions),
36+
];
37+
38+
const commonJsFiles = commonJsExtensions(baseExtensions).map(
39+
ext => `**/*.${ext}`
40+
);
41+
const javascriptFiles = [
42+
...javascriptExtensions,
43+
...commonJsExtensions(javascriptExtensions),
44+
...esmExtensions(javascriptExtensions),
45+
].map(ext => `**/*.${ext}`);
46+
const nodeFiles = allExtensions.map(ext => `*.${ext}`);
47+
const browserFiles = allExtensions.flatMap(ext =>
48+
['js', 'vendor', 'dev-server'].map(dir => `${dir}/**/*.${ext}`)
49+
);
50+
const reactFiles = [
51+
...jsxExtensions,
52+
...commonJsExtensions(jsxExtensions),
53+
...esmExtensions(jsxExtensions),
54+
].map(ext => `**/*.${ext}`);
55+
56+
/** @type import("eslint").Linter.Config[] */
57+
export default [
58+
{
59+
ignores: ['vendor/'],
60+
},
61+
...[
62+
jsPlugin.configs['recommended'],
63+
importPlugin.flatConfigs['recommended'],
64+
importPlugin.flatConfigs['typescript'],
65+
...tsPlugin.configs['recommendedTypeChecked'],
66+
].map(conf => ({
67+
files: nodeFiles,
68+
...conf,
69+
})),
70+
...[
71+
jsPlugin.configs['recommended'],
72+
importPlugin.flatConfigs['recommended'],
73+
importPlugin.flatConfigs['typescript'],
74+
...tsPlugin.configs['strictTypeChecked'],
75+
].map(conf => ({
76+
files: browserFiles,
77+
...conf,
78+
})),
79+
{
80+
files: ['**/*.d.ts'],
81+
rules: {
82+
'@typescript-eslint/no-floating-promises': 'off',
83+
},
84+
},
85+
...[
86+
importPlugin.flatConfigs['recommended'],
87+
tsPlugin.configs['disableTypeChecked'],
88+
].map(conf => ({
89+
files: javascriptFiles,
90+
...conf,
91+
})),
92+
{
93+
languageOptions: {
94+
globals: globals.builtin,
95+
parserOptions: {
96+
projectService: true,
97+
tsconfigRootDir: __dirname,
98+
},
99+
},
100+
settings: {
101+
'import/cache': {
102+
// If you never use `eslint_d` or `eslint-loader`, you may set the cache lifetime to Infinity and everything should be fine:
103+
// https://github.com/import-js/eslint-plugin-import/blob/main/README.md#importcache
104+
lifetime: Infinity,
105+
},
106+
'import/resolver': {
107+
typescript: {
108+
alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
109+
project: ['tsconfig.node.json', 'tsconfig.browser.json'],
110+
},
111+
},
112+
},
113+
},
114+
promisePlugin.configs['flat/recommended'],
115+
compatPlugin.configs['flat/recommended'],
116+
commentsPlugin['recommended'],
117+
{
118+
files: commonJsFiles,
119+
languageOptions: { globals: globals.commonjs },
120+
},
121+
{
122+
files: nodeFiles,
123+
languageOptions: {
124+
globals: globals.node,
125+
},
126+
},
127+
{
128+
files: browserFiles,
129+
languageOptions: { globals: globals.browser },
130+
},
131+
{
132+
files: reactFiles,
133+
...reactPlugin.configs.flat['recommended'],
134+
settings: { react: { version: 'detect' } },
135+
rules: {
136+
'react/prop-types': 'off',
137+
},
138+
},
139+
{
140+
files: reactFiles,
141+
...reactPlugin.configs.flat['jsx-runtime'],
142+
},
143+
{
144+
files: reactFiles,
145+
...reactHooksPlugin.configs['recommended-latest'],
146+
},
147+
{
148+
files: reactFiles,
149+
...reactCompilerPlugin.configs['recommended'],
150+
},
151+
{
152+
files: reactFiles,
153+
...jsxA11yPlugin.flatConfigs['strict'],
154+
},
155+
];

0 commit comments

Comments
 (0)