-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.ts
260 lines (224 loc) · 7.88 KB
/
factory.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
import type { Linter } from 'eslint';
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from './types/index.ts';
import { FlatConfigComposer } from 'eslint-flat-config-utils';
import { isPackageExists } from 'local-pkg';
import {
astro,
command,
comments,
disables,
formatters,
gitignore,
ignores,
imports,
javascript,
jsdoc,
jsonc,
jsx,
markdown,
node,
perfectionist,
query,
react,
regexp,
schema,
solid,
sortPackageJson,
sortTsConfig,
stylistic,
svelte,
test,
toml,
typescript,
unicorn,
unocss,
vue,
yaml,
} from './configs/index.ts';
import { getOverrides, isInEditorEnv, resolveSubOptions } from './utils.ts';
const flatConfigProps = ['languageOptions', 'linterOptions', 'name', 'plugins', 'processor', 'rules', 'settings'] satisfies Array<keyof TypedFlatConfigItem>;
const VUE_PACKAGES = ['@slidev/cli', 'nuxt', 'vitepress', 'vue'];
const SOLID_PACKAGES = ['solid-js', 'solid-refresh', 'vite-plugin-solid'];
const SVELTE_PACKAGES = ['@sveltejs/kit', '@sveltejs/package', '@sveltejs/vite-plugin-svelte', 'svelte'];
const REACT_PACKAGES = ['react', 'react-dom', 'react-native', 'next'];
const TYPESCRIPT_PACKAGES = ['typescript', 'tsc', 'tslib'];
const QUERY_PACKAGES = ['react', 'vue', 'solid', 'svelte'].map(i => `@tanstack/${i}-query`);
const pkgSort = (i: string): boolean => isPackageExists(i);
export const DEFAULT_PLUGIN_RENAMING = {
'@eslint-react': 'react',
'@eslint-react/dom': 'react-dom',
'@eslint-react/hooks-extra': 'react-hooks-extra',
'@eslint-react/naming-convention': 'react-naming-convention',
'@stylistic': 'style',
'@typescript-eslint': 'ts',
'import-x': 'import',
'json-schema-validator': 'schema',
'n': 'node',
'vitest': 'test',
'vuejs-accessibility': 'vue-a11y',
'yml': 'yaml',
};
export type FactoryOptions = OptionsConfig & Omit<TypedFlatConfigItem, 'files'>;
export type UserConfig = Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[] | FlatConfigComposer | Linter.Config[]>;
export type FactoryComposer = FlatConfigComposer<TypedFlatConfigItem, ConfigNames>;
/**
* Construct a Petal ESLint config.
*
* @param {FactoryOptions} options The options for generating the ESLint configurations.
* @param {UserConfig[]} userConfigs The user configurations to be merged with the generated configurations.
* @returns {FactoryComposer} The merged ESLint configurations.
* @public
*/
// eslint-disable-next-line ts/promise-function-async -- `FlatConfigComposer` has `Promise`-like methods, but isnt async
export function defineConfig(options: FactoryOptions = {}, ...userConfigs: UserConfig[]): FactoryComposer {
const configs: Array<Awaitable<TypedFlatConfigItem[]>> = [];
const isInEditor = options.isInEditor ?? isInEditorEnv();
const stylisticOptions = options.stylistic === false ? false : typeof options.stylistic === 'object' ? options.stylistic : {};
const componentExts = options.componentExts ?? [];
const typescriptEnabled = options.typescript ?? TYPESCRIPT_PACKAGES.some(pkgSort);
const tsconfigPath = 'tsconfigPath' in resolveSubOptions(options, 'typescript')
? resolveSubOptions(options, 'typescript').tsconfigPath
: undefined;
if (isInEditor)
// eslint-disable-next-line no-console -- debug editor message
console.info('[@flowr/eslint]: running in editor - some rules are disabled');
if (stylisticOptions && !('jsx' in stylisticOptions))
stylisticOptions.jsx = options.jsx ?? true;
if (options.gitignore ?? true)
if (typeof options.gitignore !== 'boolean')
configs.push(gitignore(options.gitignore));
else configs.push(gitignore({ strict: false }));
configs.push(ignores(options.ignores));
configs.push(javascript({ isInEditor, overrides: getOverrides(options, 'javascript') }));
configs.push(comments());
configs.push(node());
configs.push(jsdoc({ stylistic: stylisticOptions }));
configs.push(imports({ stylistic: stylisticOptions }));
configs.push(command());
configs.push(perfectionist());
if (options.unicorn ?? true)
configs.push(unicorn(resolveSubOptions(options, 'unicorn')));
if (options.vue ?? VUE_PACKAGES.some(pkgSort))
componentExts.push('vue');
if (options.jsx ?? true)
configs.push(jsx());
if (typescriptEnabled)
configs.push(typescript({
...resolveSubOptions(options, 'typescript'),
componentExts,
overrides: getOverrides(options, 'typescript'),
type: options.type,
}));
if (stylisticOptions)
configs.push(stylistic({
...stylisticOptions,
opinionated: options.opinionated,
overrides: getOverrides(options, 'stylistic'),
}));
if (options.test ?? true)
configs.push(test({
...resolveSubOptions(options, 'test'),
overrides: getOverrides(options, 'test'),
}));
if (options.regexp ?? true)
configs.push(regexp({
...resolveSubOptions(options, 'regexp'),
overrides: getOverrides(options, 'regexp'),
}));
if (options.vue ?? VUE_PACKAGES.some(pkgSort))
configs.push(vue({
...resolveSubOptions(options, 'vue'),
overrides: getOverrides(options, 'vue'),
stylistic: stylisticOptions,
typescript: !!typescriptEnabled,
}));
if (options.solid ?? SOLID_PACKAGES.some(pkgSort))
configs.push(solid({
...resolveSubOptions(options, 'solid'),
overrides: getOverrides(options, 'solid'),
tsconfigPath,
typescript: !!typescriptEnabled,
}));
if (options.astro ?? isPackageExists('astro'))
configs.push(astro({
...resolveSubOptions(options, 'astro'),
overrides: getOverrides(options, 'astro'),
}));
if (options.react ?? REACT_PACKAGES.some(pkgSort))
configs.push(react({
...resolveSubOptions(options, 'typescript'),
...resolveSubOptions(options, 'react'),
overrides: getOverrides(options, 'react'),
tsconfigPath,
}));
if (options.svelte ?? SVELTE_PACKAGES.some(pkgSort))
configs.push(svelte({
...resolveSubOptions(options, 'svelte'),
overrides: getOverrides(options, 'svelte'),
stylistic: stylisticOptions,
typescript: !!typescriptEnabled,
}));
if (options.unocss ?? false)
configs.push(unocss({
...resolveSubOptions(options, 'unocss'),
overrides: getOverrides(options, 'unocss'),
}));
if (options.query ?? QUERY_PACKAGES.some(pkgSort))
configs.push(query({
...resolveSubOptions(options, 'query'),
overrides: getOverrides(options, 'query'),
}));
if (options.jsonc ?? true)
configs.push(
jsonc({
...resolveSubOptions(options, 'jsonc'),
overrides: getOverrides(options, 'jsonc'),
stylistic: stylisticOptions,
}),
sortPackageJson(),
sortTsConfig(),
);
if (options.yaml ?? true)
configs.push(yaml({
...resolveSubOptions(options, 'yaml'),
overrides: getOverrides(options, 'yaml'),
stylistic: stylisticOptions,
}));
if (options.toml ?? true)
configs.push(toml({
...resolveSubOptions(options, 'toml'),
overrides: getOverrides(options, 'toml'),
stylistic: stylisticOptions,
}));
if (options.schema ?? false)
configs.push(schema({
...resolveSubOptions(options, 'schema'),
overrides: getOverrides(options, 'schema'),
}));
if (options.markdown ?? true)
configs.push(markdown({
...resolveSubOptions(options, 'markdown'),
componentExts,
overrides: getOverrides(options, 'markdown'),
}));
if (options.formatters)
configs.push(formatters(
options.formatters,
typeof stylisticOptions === 'boolean' ? {} : stylisticOptions,
));
configs.push(disables());
if ('files' in options)
throw new Error('[@flowr/eslint]: the first argument should not contain the "files" property as options are supposed to be global. place it in a different config block instead.');
const fusedConfig = flatConfigProps.reduce((curr, key) => {
if (key in options)
curr[key] = options[key] as any;
return curr;
}, {} as TypedFlatConfigItem);
if (Object.keys(fusedConfig).length)
configs.push([fusedConfig]);
const composer = new FlatConfigComposer<TypedFlatConfigItem, ConfigNames>()
.append(...configs, ...userConfigs as TypedFlatConfigItem[]);
if (options.autoRenamePlugins ?? true)
return composer.renamePlugins(DEFAULT_PLUGIN_RENAMING);
return composer;
}