forked from antfu/eslint-config
/
factory.ts
173 lines (156 loc) · 3.8 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
import {
command,
comments,
ignores,
imports,
javascript,
jsdoc,
jsonc,
node,
perfectionist,
sortPackageJson,
sortTsconfig,
svelte,
typescript,
unicorn,
} from "./configs";
import type {
Awaitable,
ConfigNames,
OptionsConfig,
TypedFlatConfigItem,
} from "./types";
import { interopDefault } from "./utils";
import type { Linter } from "eslint";
import { FlatConfigComposer } from "eslint-flat-config-utils";
import { isPackageExists } from "local-pkg";
import fs from "node:fs";
const flatConfigProps: Array<keyof TypedFlatConfigItem> = [
"name",
"files",
"ignores",
"languageOptions",
"linterOptions",
"processor",
"plugins",
"rules",
"settings",
];
export const defaultPluginRenaming = {
"@typescript-eslint": "ts",
"import-x": "import",
"n": "node",
};
/**
* Construct an array of ESLint flat config items.
* @param options - The options for generating the ESLint configurations.
* @param userConfigs - The user configurations to be merged with the generated configurations.
* @returns
* The merged ESLint configurations.
*/
export function iz7n(
options: OptionsConfig & TypedFlatConfigItem = {},
...userConfigs: Array<
Awaitable<
| TypedFlatConfigItem
| TypedFlatConfigItem[]
| FlatConfigComposer<any, any>
| Linter.FlatConfig[]
>
>
): FlatConfigComposer<TypedFlatConfigItem, ConfigNames> {
const {
autoRenamePlugins = true,
componentExts = [],
gitignore: enableGitignore = true,
svelte: enableSvelte = isPackageExists("svelte"),
typescript: enableTypeScript = isPackageExists("typescript"),
} = options;
const configs: Array<Awaitable<TypedFlatConfigItem[]>> = [];
if (enableGitignore) {
if (typeof enableGitignore === "boolean") {
if (fs.existsSync(".gitignore"))
configs.push(
interopDefault(import("eslint-config-flat-gitignore")).then(r => [
r(),
]),
);
} else {
configs.push(
interopDefault(import("eslint-config-flat-gitignore")).then(r => [
r(enableGitignore),
]),
);
}
}
// Base configs
configs.push(
ignores(),
javascript({
overrides: getOverrides(options, "javascript"),
}),
comments(),
node(),
jsdoc(),
imports(),
unicorn(),
command(),
perfectionist(),
);
if (enableTypeScript) {
configs.push(
typescript({
...resolveSubOptions(options, "typescript"),
componentExts,
overrides: getOverrides(options, "typescript"),
}),
);
}
if (enableSvelte) {
configs.push(
svelte({
overrides: getOverrides(options, "svelte"),
typescript: !!enableTypeScript,
}),
);
}
if (options.jsonc ?? true) {
configs.push(
jsonc({
overrides: getOverrides(options, "jsonc"),
}),
sortPackageJson(),
sortTsconfig(),
);
}
// User can optionally pass a flat config item to the first argument
// We pick the known keys as ESLint would do schema validation
const fusedConfig = flatConfigProps.reduce((acc, key) => {
if (key in options) acc[key] = options[key] as any;
return acc;
}, {} as TypedFlatConfigItem);
if (Object.keys(fusedConfig).length) configs.push([fusedConfig]);
let composer = new FlatConfigComposer<TypedFlatConfigItem, ConfigNames>();
composer = composer.append(...configs, ...(userConfigs as any));
if (autoRenamePlugins) {
composer = composer.renamePlugins(defaultPluginRenaming);
}
return composer;
}
export type ResolvedOptions<T> = T extends boolean ? never : NonNullable<T>;
export function resolveSubOptions<K extends keyof OptionsConfig>(
options: OptionsConfig,
key: K,
): ResolvedOptions<OptionsConfig[K]> {
return typeof options[key] === "boolean" ? ({} as any) : options[key] || {};
}
export function getOverrides<K extends keyof OptionsConfig>(
options: OptionsConfig,
key: K,
) {
const sub = resolveSubOptions(options, key);
return {
...(options.overrides as any)?.[key],
...("overrides" in sub ? sub.overrides : {}),
};
}