-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
315 lines (308 loc) · 12.5 KB
/
index.ts
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
/* eslint-disable unicorn/no-await-expression-member,@typescript-eslint/no-unsafe-member-access */
import { fixupPluginRules } from '@eslint/compat';
import eslintJS from '@eslint/js';
import type { Linter } from 'eslint';
// @ts-expect-error, untyped import
import eslintConfigPrettier from 'eslint-config-prettier';
import eslintPluginJSDoc from 'eslint-plugin-jsdoc';
// @ts-expect-error, untyped import
import eslintPluginMarkdown from 'eslint-plugin-markdown';
import eslintPluginNode from 'eslint-plugin-n';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
// @ts-expect-error, untyped import
import eslintPluginPromise from 'eslint-plugin-promise';
// @ts-expect-error, untyped import
import eslintPluginReact from 'eslint-plugin-react';
// @ts-expect-error, untyped import
import eslintPluginReactHooks from 'eslint-plugin-react-hooks';
import eslintPluginSimpleImportSort from 'eslint-plugin-simple-import-sort';
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
// @ts-expect-error, untyped import
import eslintPluginUnusedImports from 'eslint-plugin-unused-imports';
import globals from 'globals';
import eslintTS from 'typescript-eslint';
/**
*
* @param globPatterns - The file patterns to append the provided extensions to.
* @param extensions - The file extensions to append to the provided glob patterns.
* @returns A list of file patterns with the provided extensions.
*/
function createForExtensions(
globPatterns: string[],
extensions: string[],
): string[] {
return extensions.flatMap((ext) =>
globPatterns.flatMap((pattern) => `${pattern}.${ext}`),
);
}
/**
* Asynchronously creates a configuration array for ESLint based on the provided options.
* This configuration is tailored to support JavaScript, TypeScript, and optionally React, Svelte, and browser environments.
* It integrates various ESLint plugins and configurations to enforce code quality and style.
* @param options - The configuration options.
* @param [options.tsconfigRootDir] - The root directory for the TypeScript configuration files.
* @param [options.browser] - Flag to indicate if the environment should be configured for browser globals.
* @param [options.react] - Flag to indicate if React specific linting rules should be applied.
* @param [options.svelte] - Flag to indicate if Svelte specific linting rules should be applied.
* @param [options.node] - Flag to indicate if Node.js specific linting rules should be applied.
* @param [options.project] - The path(s) to the TypeScript configuration file(s).
* @param [options.ecmaVersion] - The ECMAScript version to be used.
* @param options.jsdoc
* @param options.prettier
* @returns A promise that resolves to an array of ESLint configurations.
* @throws {Error} If both React and Svelte options are set to true.
*/
export async function createConfig({
browser = false,
ecmaVersion = 2024,
jsdoc = false,
node = false,
prettier = true,
project = ['./tsconfig.json'],
react = false,
svelte = false,
tsconfigRootDir = '.',
...restParserOptions
}: {
browser?: boolean;
jsdoc?: boolean;
node?: boolean;
prettier?: boolean;
react?: boolean;
svelte?: boolean;
} & Linter.ParserOptions = {}): Promise<Linter.Config[]> {
if (svelte && react) {
throw new Error(
'Cannot use both react and svelte for the same configuration',
);
}
const configNode = node
? [
eslintPluginNode.configs['flat/recommended-module'],
{
rules: {
'n/no-extraneous-import': 'error',
'n/no-missing-import': 'error',
'n/no-process-exit': 'error',
},
},
]
: [];
const configPrettier = svelte
? [
...(await import('eslint-plugin-svelte')).configs[
'flat/recommended'
],
eslintPluginPrettierRecommended,
eslintConfigPrettier,
...(await import('eslint-plugin-svelte')).configs[
'flat/prettier'
],
{
files: ['**/*.svelte'],
languageOptions: {
parserOptions: {
parser: eslintTS.parser,
},
},
},
]
: prettier
? [eslintPluginPrettierRecommended, eslintConfigPrettier]
: [eslintConfigPrettier];
const configReact = react
? [
{
plugins: {
'react': eslintPluginReact,
'react-hooks': fixupPluginRules(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
eslintPluginReactHooks,
),
},
},
]
: [];
const jsdocPlugin = jsdoc
? [
{
...eslintPluginJSDoc.configs['flat/recommended-typescript'],
rules: {
'jsdoc/no-defaults': 'off',
'jsdoc/no-types': 'off',
'jsdoc/require-jsdoc': [
'error',
{
publicOnly: true,
},
],
},
ignores: ['**/*.tsx'],
},
]
: [];
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return [
eslintJS.configs.recommended,
{
ignores: [
'dist/',
'build/',
'.svelte-kit/',
'node_modules/',
'public/',
],
languageOptions: {
globals: {
...(browser || react || svelte ? globals.browser : {}),
...globals.node,
},
},
rules: {
'no-console': 'warn',
'prefer-destructuring': 'error',
'curly': 'error',
'eqeqeq': 'error',
'prefer-const': [
'error',
{
destructuring: 'all',
},
],
'no-unused-vars': 'error',
'object-shorthand': 'error',
'prefer-template': 'warn',
},
},
...eslintTS.configs.strictTypeChecked,
...eslintTS.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
project,
parser: '@typescript-eslint/parser',
tsconfigRootDir,
ecmaVersion,
...restParserOptions,
...(svelte ? { extraFileExtensions: ['.svelte'] } : {}),
...(react ? { ecmaFeatures: { jsx: true } } : {}),
},
},
rules: {
'@typescript-eslint/array-type': [
'error',
{ default: 'array' },
],
'@typescript-eslint/consistent-indexed-object-style': 'error',
'@typescript-eslint/consistent-type-definitions': 'warn',
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'interface',
format: ['PascalCase'],
custom: { regex: '^I[A-Z]', match: false },
},
],
'@typescript-eslint/no-extra-non-null-assertion': 'error',
'@typescript-eslint/no-floating-promises': [
'error',
{ ignoreIIFE: true, ignoreVoid: true },
],
'@typescript-eslint/no-for-in-array': 'error',
'@typescript-eslint/no-inferrable-types': 'error',
'@typescript-eslint/no-redundant-type-constituents': 'warn',
'@typescript-eslint/no-require-imports': 'warn',
'@typescript-eslint/no-this-alias': 'error',
'@typescript-eslint/no-unnecessary-boolean-literal-compare':
'error',
'@typescript-eslint/no-unnecessary-condition': 'error',
'@typescript-eslint/no-unnecessary-qualifier': 'warn',
'@typescript-eslint/no-unnecessary-type-arguments': 'error',
'@typescript-eslint/no-unused-expressions': 'warn',
'@typescript-eslint/no-unused-vars': [
'warn',
{ args: 'all', argsIgnorePattern: '^_' },
],
'@typescript-eslint/no-useless-constructor': 'warn',
'@typescript-eslint/no-useless-empty-export': 'warn',
'@typescript-eslint/prefer-for-of': 'warn',
'@typescript-eslint/prefer-includes': 'warn',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/switch-exhaustiveness-check': 'warn',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/prefer-as-const': 'warn',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'n/no-missing-import': 'off',
},
},
{
files: createForExtensions(
[
'**/*.{spec,test}',
'**/{tests,test,__tests__,__mock__,__mocks__}/*',
],
['ts', react ? 'tsx' : '', svelte ? 'svelte' : ''].filter(
Boolean,
),
),
rules: {
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/prefer-as-const': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-implied-eval': 'off',
'@typescript-eslint/no-magic-numbers': 'off',
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'typescript-eslint/no-confusing-void-expression': 'off',
'unicorn/error-message': 'off',
'unicorn/no-await-expression-member': 'off',
},
},
...configNode,
...jsdocPlugin,
eslintPluginPromise.configs['flat/recommended'],
eslintPluginUnicorn.configs['flat/recommended'],
{
plugins: {
'simple-import-sort': eslintPluginSimpleImportSort,
'unused-imports': eslintPluginUnusedImports,
'markdown': eslintPluginMarkdown,
},
rules: {
'simple-import-sort/exports': 'error',
'simple-import-sort/imports': 'error',
'unicorn/catch-error-name': 'off',
'unicorn/explicit-length-check': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/no-array-for-each': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/no-null': 'off',
'unicorn/no-process-exit': 'off',
'unicorn/no-useless-undefined': 'off',
'unicorn/prefer-module': 'off',
'unicorn/prevent-abbreviations': 'off',
'unused-imports/no-unused-imports': 'error',
},
},
{
files: ['**/*.md'],
processor: 'markdown/markdown',
},
...configReact,
...configPrettier,
] satisfies Linter.Config[];
}