-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint.config.mjs
426 lines (380 loc) · 11.7 KB
/
eslint.config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// ESLint confit compatible with flat config ESLint v9
// From <https://github.com/sveltejs/eslint-plugin-svelte/issues/732#issuecomment-2156240189>
// @ts-check
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import globals from 'globals';
// Plugins
const { plugin: tsPlugin } = tseslint;
import sveltePlugin from 'eslint-plugin-svelte';
// import eslintImport from 'eslint-plugin-import';
import pluginImportX from 'eslint-plugin-import-x';
import playwright from 'eslint-plugin-playwright';
import vitest from 'eslint-plugin-vitest';
// Parsers
const { parser: tsParser } = tseslint;
import svelteParser from 'svelte-eslint-parser';
import * as tsExtraFilesParser from 'typescript-eslint-parser-for-extra-files';
import { fixupConfigRules, fixupPluginRules } from '@eslint/compat';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { FlatCompat } from '@eslint/eslintrc';
import fs from 'fs';
import { parse } from 'comment-json';
// const project = ["./tsconfig.json"];
const project = ['./tsconfig.json', './tsconfig.lint.json', './tsconfig.configs.json'];
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// ? const __dirname = import.meta.dirname,
function readJson(file) {
const file_path = path.join(__dirname, file);
try {
// console.log(`Reading "${file_path}"`);
const blob = fs.readFileSync(file_path);
const res = parse(blob.toString(), null, true); // Remove comments
// console.log(`DONE Reading "${file_path}"`);
// console.debug(` + File "${file_path}" content:\n${JSON.stringify(res)}`);
return res;
} catch (e) {
console.error(`FAIL Reading "${file_path}", error ${e}`);
throw e;
}
}
// We have return statements in arrayFilePathsDeprefix() and patchFilesPrefix() throwing
// eslint errors @typescript-eslint/no-unsafe-return, but they don't show errors in the
// main branch, so VSCode removes eslint-disable-next-line directives on save.
// Instead, we disable all eslint rules for the whole block:
// BEGIN BLOCK
/* eslint-disable -- because eslint does not have rule for removing unused directives. */
{
} // Dummy code block to force keeping `eslint-disable`
/** @type (obj: Object, prop: string, old_prefix: string, new_prefix?: string) => string[] */
function arrayFilePathsDeprefix(obj, prop, old_prefix, new_prefix = '') {
if (obj instanceof Object && prop in obj && Array.isArray(obj[prop]))
// May get @typescript-eslint/no-unsafe-return
return (
obj[prop]?.map((ss) => {
const s = typeof ss === 'string' ? ss : '';
return s.startsWith(old_prefix) ? new_prefix + s.slice(old_prefix.length) : s;
}) ?? []
);
return [];
}
/** @type (obj_arr: Object[]) => typeof obj_arr */
function patchFilesPrefix(obj_arr) {
// May get @typescript-eslint/no-unsafe-return
return (
obj_arr?.map((obj) => {
const prop = 'files';
if (obj instanceof Object && prop in obj && Array.isArray(obj[prop])) {
obj[prop] = arrayFilePathsDeprefix(obj, prop, '*.', '**/*.');
}
// May get @typescript-eslint/no-unsafe-return
return obj;
}) ?? obj_arr
);
}
/* eslint-enable */
// END BLOCK
// Data from tsconfig.*.json
// import tsConfigForConfigFiles from './tsconfig.configs.json' assert { type: 'json' };
// import tsConfigForLintFiles from './tsconfig.lint.json' assert { type: 'json' };
// import tsConfigForTsFiles from './tsconfig.json' assert { type: 'json' };
const tsConfigForTsFiles = readJson('tsconfig.json');
const tsConfigForTsFilesExclude = arrayFilePathsDeprefix(tsConfigForTsFiles, 'exclude', './');
// console.debug(`tsConfigForTsFilesExclude=${tsConfigForTsFilesExclude}`);
const tsConfigForConfigFiles = readJson('./tsconfig.configs.json');
const tsConfigForConfigFilesInclude = arrayFilePathsDeprefix(
tsConfigForConfigFiles,
'include',
'./'
);
const tsConfigForLintFiles = readJson('./tsconfig.lint.json');
const tsConfigForLintFilesInclude = arrayFilePathsDeprefix(tsConfigForLintFiles, 'include', './');
// Method 3 to import JSON
// import { createRequire } from 'module';
// const require = createRequire(import.meta.url);
// const tsConfigForTsFiles2 = require('./tsconfig.json');
/** @type {import('@eslint/eslintrc').FlatCompat} */
const compat = new FlatCompat({
baseDirectory: __dirname,
resolvePluginsRelativeTo: __dirname,
recommendedConfig: eslint.configs.recommended,
allConfig: eslint.configs.all
});
/** @typedef {import('@eslint/compat').FixupPluginDefinition } FixupPluginDefinition */
/** @type {(name: string, alias: string) => FixupPluginDefinition} */
function legacyPlugin(name, alias = name) {
/** @type {FixupPluginDefinition} */
const plugin = compat.plugins(name)[0]?.plugins?.[alias];
if (!plugin) {
throw new Error(`Unable to resolve plugin ${name} and/or alias ${alias}`);
}
return fixupPluginRules(plugin);
}
// Rules for different languages
/** @typedef {import('@typescript-eslint/utils').TSESLint.SharedConfig.RulesRecord} RulesRecord */
/** @type Record<string, RulesRecord> */
const RULES = {
ts: {
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_'
}
],
// TODO: (soon) Fix all these rules below. Turned off when upgraded eslint to v9 to pass `pnpm run lint`.
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/restrict-plus-operands': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/no-redundant-type-constituents': 'off',
// import-x:
'import-x/no-named-as-default-member': 'off',
'import-x/no-unresolved': [2, { ignore: ['^\\$env/'] }]
}
};
export default tseslint.config(
{
// Recommended configs
name: 'config-recommended',
languageOptions: {
ecmaVersion: 'latest',
parserOptions: {
// project: './tsconfig.json',
project,
extraFileExtensions: ['.Xrecommended'],
tsconfigRootDir: __dirname
}
},
settings: {
'import-x/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx']
},
'import-x/resolver': {
// Load <rootdir>/tsconfig.json
typescript: true,
node: true
}
}
},
// Rules from legacy plugins
...fixupConfigRules(
compat.extends(
// 'plugin:import/recommended',
// 'plugin:import/typescript',
'plugin:import-x/recommended',
'plugin:import-x/typescript'
)
),
eslint.configs.recommended,
...tseslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
...sveltePlugin.configs['flat/recommended'],
{
// Basic js files
name: 'config-basic-js',
files: ['**/*.js', '**/*.cjs', '**/*.mjs']
},
{
// Ignore on all levels
name: 'config-ignores',
ignores: [
// because of import assertion of tsconfig.json
'eslint.config.mjs',
// Use `.exclude` section from ./tsconfig.ts
// Apparently, `project: 'tsconfig..json'` does not honor excludes.
...tsConfigForTsFilesExclude,
// '**/*.cjs', // Various glue files?
// OS
'**/.DS_Store',
'**/Thumbs.db',
'.logo/',
// Env
'**/.env',
'**/.env.*',
'!**/.env.EXAMPLE',
// Outputs
'.output/',
'.netlify/',
'.vercel/',
'.svelte-kit/',
'build/',
'package/',
'static/vendor/',
'dist/',
// Generated
'**/log.*',
'**/*.log',
'test-results/',
'**/test-results.json',
'src-tauri/gen/',
'src-tauri/target/',
'src-tauri/WixTools/',
'src-tauri/Cargo.lock',
'playwright-report/',
'/.trunk/tmp',
'/.VSCodeCounter',
// Vite
'**/vite.config.js.timestamp-*',
'**/vite.config.ts.timestamp-*',
// Local Files
'**/*.local',
'**/local.*',
'**/*.nogit.*',
// Package Managers
// Ignore files for PNPM, NPM and YARN
'**/package-lock.json',
'**/pnpm-lock.yaml',
'**/yarn.lock',
'patches/'
]
},
{
// Globals for nodejs files
name: 'config-nodejs',
files: ['**/*.cjs', '**/*.cts'],
plugins: {
import: legacyPlugin('eslint-plugin-import-x', 'import-x')
},
languageOptions: {
globals: {
...globals.node
},
parserOptions: {
project,
extraFileExtensions: ['.Xnodejs'],
tsconfigRootDir: __dirname
}
},
rules: {
...RULES.ts
}
},
{
// Globals for browser files
name: 'config-browser',
files: ['**/*.js', '**/*.mjs', '**/*.ts', '**/*.mts', '**/*.svelte'],
plugins: {
import: legacyPlugin('eslint-plugin-import-x', 'import-x')
},
languageOptions: {
globals: {
...globals.browser
}
},
rules: {
...RULES.ts
}
},
{
// Tools configs: *.config.ts files in root
name: 'config-tools',
files: tsConfigForConfigFilesInclude, // Use tsconfig.configs.json
plugins: {
'@typescript-eslint': tsPlugin,
import: legacyPlugin('eslint-plugin-import-x', 'import-x')
},
languageOptions: {
globals: {
...globals.node
},
parser: tsParser,
sourceType: 'module',
parserOptions: {
project: './tsconfig.configs.json',
extraFileExtensions: ['.Xtools'],
tsconfigRootDir: __dirname
}
},
rules: RULES.ts
},
{
// Typescript
name: 'config-typescript',
files: ['**/*.ts', '**/*.cts', '**/*.mts'],
ignores: tsConfigForConfigFilesInclude,
plugins: {
'@typescript-eslint': tsPlugin,
import: legacyPlugin('eslint-plugin-import-x', 'import-x')
},
languageOptions: {
parser: tsExtraFilesParser,
sourceType: 'module',
parserOptions: {
project,
extraFileExtensions: ['.svelte', '.Xtypescript'],
tsconfigRootDir: __dirname
}
},
rules: RULES.ts
},
{
// Svelte
name: 'config-svelte',
plugins: {
svelte: sveltePlugin,
'@typescript-eslint': tsPlugin,
import: legacyPlugin('eslint-plugin-import-x', 'import-x')
},
files: ['**/*.svelte'],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: tsExtraFilesParser,
svelteFeatures: {
experimentalGenerics: true
},
project,
extraFileExtensions: ['.svelte', '.Xsvelte'],
tsconfigRootDir: __dirname
}
},
rules: {
...sveltePlugin.configs.recommended.rules,
...RULES.ts
}
},
{
// Playwright and Vitest
name: 'config-playwright',
...playwright.configs['flat/recommended'],
files: ['tests/**'], // or any other pattern
plugins: {
import: legacyPlugin('eslint-plugin-import-x', 'import-x'),
vitest
},
rules: {
...vitest.configs.recommended.rules
},
settings: {
'import/resolver': {
typescript: {
alwaysTryTypes: true,
project,
extraFileExtensions: ['.svelte', '.Xplaywright'],
tsconfigRootDir: __dirname
}
},
vitest: {
typecheck: true
}
},
languageOptions: {
globals: {
...vitest.environments.env.globals
}
}
}
);