@purea/eslint-config 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/factory.ts ADDED
@@ -0,0 +1,70 @@
1
+ import type { Linter } from 'eslint';
2
+ import type { Awaitable, ConfigOptions, FlatConfigItem } from './types';
3
+
4
+ import { isPackageExists } from 'local-pkg';
5
+ import { ignores, imports, javascript, typescript, stylistic, jsonc, vue } from './configs';
6
+ import { isObject } from './utils';
7
+
8
+ /**
9
+ * Create ESLint configuration based on provided options
10
+ *
11
+ * @param options - Configuration options
12
+ * @returns Array of ESLint configurations
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { useConfig } from '@purea/eslint-config'
17
+ *
18
+ * export default useConfig({
19
+ * typescript: true,
20
+ * })
21
+ * ```
22
+ */
23
+ export async function useConfig(
24
+ options: ConfigOptions = {},
25
+ ...extraConfigs: Awaitable<FlatConfigItem | FlatConfigItem[] | Linter.Config[]>[]
26
+ ): Promise<FlatConfigItem[]> {
27
+ const {
28
+ ignores: userIgnores = [],
29
+ imports: enableImports = true,
30
+ typescript: enableTypeScript = isPackageExists('typescript'),
31
+ jsonc: enableJsonc = true,
32
+ vue: enableVue = isPackageExists('vue'),
33
+ } = options;
34
+
35
+ const configs: Awaitable<FlatConfigItem | FlatConfigItem[] | Linter.Config[]>[] = [];
36
+
37
+ configs.push(
38
+ ignores(userIgnores),
39
+ javascript(),
40
+ stylistic()
41
+ );
42
+
43
+ // Imports config
44
+ enableImports && configs.push(imports());
45
+
46
+ // TypeScript config
47
+ if (enableTypeScript) {
48
+ const typescriptOptions = isObject(enableTypeScript) ? enableTypeScript : {};
49
+ configs.push(typescript({ ...typescriptOptions }));
50
+ }
51
+
52
+ // Vue config
53
+ if (enableVue) {
54
+ const vueOptions = isObject(enableVue) ? enableVue : {};
55
+ configs.push(vue({
56
+ typescript: !!enableTypeScript,
57
+ ...vueOptions,
58
+ }));
59
+ }
60
+
61
+ // JSONC config
62
+ enableJsonc && configs.push(jsonc());
63
+
64
+ // Extra configs
65
+ configs.push(...extraConfigs);
66
+
67
+ const resolvedConfigs = await Promise.all(configs);
68
+
69
+ return resolvedConfigs.flat().filter(Boolean);
70
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { useConfig } from './factory';
2
+
3
+ export type * from './types';
4
+
5
+ export * from './configs';
6
+ export * from './factory';
7
+ export * from './utils';
8
+
9
+ export default useConfig;
package/src/types.ts ADDED
@@ -0,0 +1,82 @@
1
+ import type { Linter } from 'eslint';
2
+
3
+ /**
4
+ * A type that represents a value that can be either a Promise of T or T itself.
5
+ */
6
+ export type Awaitable<T> = Promise<T> | T;
7
+
8
+ /** Configuration options for the ESLint config */
9
+ export interface ConfigOptions {
10
+ /**
11
+ * User ignores patterns
12
+ * @default []
13
+ */
14
+ ignores?: string[]
15
+ /**
16
+ * Enable imports rules
17
+ * @default true
18
+ */
19
+ imports?: boolean
20
+ /**
21
+ * Enable TypeScript rules
22
+ * @default true (if TypeScript is detected in the project)
23
+ */
24
+ typescript?: boolean | OptionsTypeScriptWithTypes
25
+ /**
26
+ * Enable JSONC rules
27
+ * @default true
28
+ */
29
+ jsonc?: boolean
30
+ /**
31
+ * Enable Vue rules
32
+ * @default true (if Vue is detected in the project)
33
+ */
34
+ vue?: boolean | VueOptions
35
+ }
36
+
37
+ export type FlatConfigItem = Omit<Linter.Config, 'plugins'> & {
38
+ /**
39
+ * An object containing a name-value mapping of plugin names to plugin objects.
40
+ * When `files` is specified, these plugins are only available to the matching files.
41
+ *
42
+ * @see [Using plugins in your configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-plugins-in-your-configuration)
43
+ */
44
+ plugins?: Record<string, unknown>
45
+ };
46
+
47
+ /** TypeScript options. */
48
+ export type TypeScriptOptions = OptionsTypeScriptWithTypes;
49
+
50
+ /** Options for TypeScript with types. */
51
+ interface OptionsTypeScriptWithTypes {
52
+ /**
53
+ * When this options is provided, type aware rules will be enabled.
54
+ * @see https://typescript-eslint.io/linting/typed-linting/
55
+ */
56
+ tsconfigPath?: string
57
+
58
+ /**
59
+ * Override type aware rules.
60
+ */
61
+ overridesTypeAwareRules?: FlatConfigItem['rules']
62
+ }
63
+
64
+ /** Vue options. */
65
+ export interface VueOptions {
66
+ /**
67
+ * Vue version
68
+ * @default 3
69
+ */
70
+ vueVersion?: 2 | 3
71
+
72
+ /**
73
+ * Override Vue-specific rules.
74
+ */
75
+ overridesRules?: FlatConfigItem['rules']
76
+
77
+ /**
78
+ * Enable TypeScript rules for Vue SFC
79
+ * @default false
80
+ */
81
+ typescript?: boolean
82
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { Awaitable } from './types';
2
+
3
+ /**
4
+ * 检查值是否为非空对象
5
+ * @param value - 要检查的值
6
+ * @returns 如果值是非空对象则返回 true,否则返回 false
7
+ */
8
+ export const isObject = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null;
9
+
10
+ /**
11
+ * 导入默认导出
12
+ * @param importable - 动态导入的 Promise
13
+ * @returns 返回模块的默认导出或整个模块对象
14
+ */
15
+ export async function importDefault<T>(importable: Awaitable<T>): Promise<T extends { default: infer U } ? U : T> {
16
+ const resolved = await importable;
17
+ if (isObject(resolved) && ('default' in resolved)) return resolved.default as T extends { default: infer U } ? U : T;
18
+ return resolved as T extends { default: infer U } ? U : T;
19
+ }