@pawover/kit-eslint-rules 0.1.0-alpha.2 → 0.2.0-alpha.0

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +271 -31
  2. package/dist/index.js +360 -81
  3. package/package.json +2 -1
package/dist/index.d.ts CHANGED
@@ -1,82 +1,272 @@
1
+ import { FlatConfigComposer } from "eslint-flat-config-utils";
1
2
  import { Linter } from "eslint";
3
+ /**
4
+ * React Compiler 规则预设
5
+ *
6
+ * - `"recommended"`:eslint-plugin-react-hooks 官方稳定预设
7
+ * - `"latest"`:官方实验预设,额外包含 `react-hooks/void-use-memo`
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const preset: ReactCompilerPreset = "latest";
12
+ * ```
13
+ */
14
+ type ReactCompilerPreset = "recommended" | "latest";
15
+ /**
16
+ * flat config 规则表类型(规则映射统一转换的目标类型)
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import type { RuleMap } from "@pawover/kit-eslint-rules";
21
+ *
22
+ * const rules: RuleMap = { "no-console": "error" };
23
+ * ```
24
+ */
25
+ type RuleMap = NonNullable<Linter.Config["rules"]>;
26
+ /**
27
+ * 单个 flat config 配置层
28
+ *
29
+ * 每个配置层都带唯一 `name`,可配合 composer 的 `override(name, config)` 精确定位并覆盖。
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * import type { ConfigLayer } from "@pawover/kit-eslint-rules";
34
+ *
35
+ * const layer: ConfigLayer = {
36
+ * name: "custom/rules",
37
+ * rules: { "no-console": "error" },
38
+ * };
39
+ * ```
40
+ */
41
+ type ConfigLayer = Linter.Config;
42
+ /** 层名字典:预设产出的全部唯一配置层名称。 */
43
+ declare const LAYER_NAMES: {
44
+ readonly ignores: "pawover/ignores";
45
+ readonly javascript: {
46
+ readonly parser: "pawover/javascript/parser";
47
+ readonly rules: "pawover/javascript/rules";
48
+ };
49
+ readonly typescript: {
50
+ readonly setup: "pawover/typescript/setup";
51
+ readonly parser: "pawover/typescript/parser";
52
+ readonly rules: "pawover/typescript/rules";
53
+ readonly typeAware: "pawover/typescript/type-aware";
54
+ };
55
+ readonly stylistic: {
56
+ readonly setup: "pawover/stylistic/setup";
57
+ readonly rules: "pawover/stylistic/rules";
58
+ };
59
+ readonly antfu: {
60
+ readonly setup: "pawover/antfu/setup";
61
+ readonly rules: "pawover/antfu/rules";
62
+ };
63
+ readonly imports: {
64
+ readonly setup: "pawover/imports/setup";
65
+ readonly rules: "pawover/imports/rules";
66
+ };
67
+ readonly importsSort: {
68
+ readonly setup: "pawover/imports-sort/setup";
69
+ readonly rules: "pawover/imports-sort/rules";
70
+ };
71
+ readonly react: {
72
+ readonly setup: "pawover/react/setup";
73
+ readonly rules: "pawover/react/rules";
74
+ readonly typeAware: "pawover/react/type-aware";
75
+ };
76
+ readonly vue: {
77
+ readonly setup: "pawover/vue/setup";
78
+ readonly parser: "pawover/vue/parser";
79
+ readonly rules: "pawover/vue/rules";
80
+ };
81
+ };
82
+ /** 递归提取对象字面量的全部值类型。 */
83
+ type DeepValue<T> = T extends object ? { [K in keyof T]: DeepValue<T[K]>; }[keyof T] : T;
84
+ /**
85
+ * 各配置层名称
86
+ *
87
+ * 每层配置都带唯一 `name`,可配合 composer 的 `override(name, config)` 精确定位并覆盖。
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * import type { ConfigNames } from "@pawover/kit-eslint-rules";
92
+ *
93
+ * const name: ConfigNames = "pawover/typescript/rules";
94
+ * ```
95
+ */
96
+ type ConfigNames = DeepValue<typeof LAYER_NAMES>;
97
+ /**
98
+ * 用户追加的 flat config
99
+ *
100
+ * 可以是单个配置对象、配置数组、composer,或配置数组的 Promise。
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * import type { PawoverUserConfig } from "@pawover/kit-eslint-rules";
105
+ *
106
+ * const config: PawoverUserConfig = { rules: { "no-console": "error" } };
107
+ * ```
108
+ */
109
+ type PawoverUserConfig = ConfigLayer | ConfigLayer[] | FlatConfigComposer<any> | Promise<ConfigLayer[]>;
110
+ /**
111
+ * 规则组覆盖选项
112
+ *
113
+ * 覆盖规则会与预设规则表合并,同名规则以 `overrides` 为准。
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * const options: RulesOverrideOptions = {
118
+ * overrides: { "ts/consistent-type-definitions": ["error", "interface"] },
119
+ * };
120
+ * ```
121
+ */
122
+ interface RulesOverrideOptions {
123
+ /** 覆盖该规则组的规则(与预设规则表浅合并,同名规则以本参数为准) */
124
+ overrides?: RuleMap;
125
+ }
126
+ /**
127
+ * TypeScript 规则组选项
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * const options: TypeScriptOptions = {
132
+ * overrides: { "ts/consistent-type-definitions": ["error", "interface"] },
133
+ * overridesTypeAware: { "ts/no-unsafe-assignment": "warn" },
134
+ * parserOptions: { tsconfigRootDir: import.meta.dirname },
135
+ * };
136
+ * ```
137
+ */
138
+ interface TypeScriptOptions extends RulesOverrideOptions {
139
+ /** 仅覆盖类型感知规则(作用于 `filesTypeAware` 限定的文件) */
140
+ overridesTypeAware?: RuleMap;
141
+ /** 类型感知规则的文件范围;默认 {@link GLOB_TYPESCRIPT} */
142
+ filesTypeAware?: string[];
143
+ /** 透传给 TypeScript parser 的解析选项;与默认值浅合并,同名键以本参数为准 */
144
+ parserOptions?: Record<string, unknown>;
145
+ }
146
+ /**
147
+ * React 规则组选项
148
+ *
149
+ * @example
150
+ * ```ts
151
+ * const options: ReactOptions = {
152
+ * compiler: "latest",
153
+ * overrides: { "react/no-implicit-key": "warn" },
154
+ * overridesTypeAware: { "react/no-unused-props": "off" },
155
+ * };
156
+ * ```
157
+ */
158
+ interface ReactOptions extends RulesOverrideOptions {
159
+ /**
160
+ * React Compiler 规则切换
161
+ *
162
+ * react 组启用时默认开启(`true` 等价 `"recommended"`):将 10 条与 `@eslint-react` 重叠的
163
+ * Compiler 规则切换为 `eslint-plugin-react-hooks` 官方实现,并关闭 `@eslint-react` 等价规则;
164
+ * 传 `false` 回退到 `@eslint-react` 实现,传 `"latest"` 额外启用实验性 `react-hooks/void-use-memo`。
165
+ *
166
+ * 仅控制这 10 条重叠规则;官方独有的 `config` / `gating` / `incompatible-library` /
167
+ * `preserve-manual-memoization` 始终由 `react-hooks` 提供。
168
+ *
169
+ * @default true
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * const options: ReactOptions = { compiler: "latest" };
174
+ * const legacy: ReactOptions = { compiler: false };
175
+ * ```
176
+ */
177
+ compiler?: boolean | ReactCompilerPreset;
178
+ /** 仅覆盖依赖类型信息的 react 规则(仅在开启 `typescript` 时生效) */
179
+ overridesTypeAware?: RuleMap;
180
+ }
2
181
  /**
3
182
  * 预设工厂选项
4
183
  *
5
- * 除 `javascript` 规则组常开外,其余规则组均通过对应布尔字段显式开启;`typescript` 开启时自动接入 TypeScript parser 与 type-aware 解析。
184
+ * 除 `javascript` 规则组常开外,其余规则组均通过对应字段显式开启。
185
+ * 每个规则组都接受 `{ overrides }` 形式,在预设内部完成规则覆盖,避免后置配置层与 flat config 插件作用域的问题。
6
186
  *
7
187
  * @example
8
188
  * ```ts
9
189
  * const options: PawoverOptions = {
10
- * typescript: true,
190
+ * typescript: {
191
+ * overrides: { "ts/strict-void-return": [1, { allowReturnAny: true }] },
192
+ * },
11
193
  * react: true,
12
- * parserOptions: { tsconfigRootDir: import.meta.dirname },
13
194
  * };
14
195
  * ```
15
196
  */
16
197
  interface PawoverOptions {
198
+ /** javascript 规则组覆盖选项(该组常开) */
199
+ javascript?: RulesOverrideOptions;
17
200
  /** 启用 `typescript` 规则组(ts / tsx 文件;同时注册 TypeScript parser,默认启用 type-aware 解析) */
18
- typescript?: boolean;
201
+ typescript?: boolean | TypeScriptOptions;
19
202
  /** 启用 `stylistic` 规则组 */
20
- stylistic?: boolean;
203
+ stylistic?: boolean | RulesOverrideOptions;
21
204
  /** 启用 `antfu` 规则组 */
22
- antfu?: boolean;
205
+ antfu?: boolean | RulesOverrideOptions;
23
206
  /** 启用 `imports` 规则组(eslint-plugin-import-lite) */
24
- imports?: boolean;
207
+ imports?: boolean | RulesOverrideOptions;
25
208
  /** 启用 `importsSort` 规则组(eslint-plugin-simple-import-sort) */
26
- importsSort?: boolean;
27
- /** 启用 `react` 规则组(注册 `@eslint-react` 与 `eslint-plugin-react-hooks` 两个插件);与 `typescript` 同开时,依赖类型信息的规则作用于 ts/tsx/jsx(jsx 的类型可用性由项目 tsconfig 的 include/allowJs 决定,不在项目内会报错);未开启 `typescript` 时自动关闭这些规则并注册为无操作实现(后置覆盖也无法强制开启) */
28
- react?: boolean;
209
+ importsSort?: boolean | RulesOverrideOptions;
210
+ /** 启用 `react` 规则组(注册 `@eslint-react` 与 `eslint-plugin-react-hooks` 两个插件,默认开启 React Compiler 规则切换);与 `typescript` 同开时,依赖类型信息的规则作用于 ts/tsx/jsx */
211
+ react?: boolean | ReactOptions;
29
212
  /** 启用 `vue` 规则组(vue / vue+ts 单文件组件;同时接入 vue-eslint-parser) */
30
- vue?: boolean;
31
- /** 透传给 TypeScript parser 的解析选项;与默认值浅合并,同名键以本参数为准(默认 `{ projectService: true }`) */
32
- parserOptions?: Record<string, unknown>;
213
+ vue?: boolean | RulesOverrideOptions;
33
214
  }
34
215
  /**
35
216
  * 创建 ESLint flat config 预设
36
217
  *
37
- * 返回可直接用于 `eslint.config.js` 的 flat config 数组:首项为全局忽略(`GLOB_EXCLUDE`),随后为常开的 `javascript` 基础层与按选项启用的各规则组层。
38
- * 全部上游插件已作为本包依赖精确锁定并自动注册,无需用户另行安装或注册插件。
218
+ * 返回 composer(继承 Promise,可直接用于 `eslint.config.js` 的默认导出),也可链式调用
219
+ * `append` / `prepend` / `override` / `renamePlugins` 等能力继续组合。
220
+ *
221
+ * 各规则组拆为 `setup` 层(只注册插件,不带 `files`)、`parser` 层(解析器)与
222
+ * `rules` / `type-aware` 层(规则与文件范围),因此后置配置可以直接使用预设已注册的插件规则;
223
+ * 若需注册自定义插件,在自己的配置层中挂载即可。
39
224
  *
40
- * @param options 预设选项;缺省时仅返回忽略层与 `javascript` 基础层
41
- * @returns flat config 配置数组
225
+ * @param options 预设选项;缺省时返回忽略层与 `javascript` parser/rules 层
226
+ * @param userConfigs 追加在预设之后的 flat config(单个对象、数组、composer 或 Promise)
227
+ * @returns 可直接导出或继续链式组合的 flat config composer
42
228
  *
43
229
  * @example
44
230
  * ```ts
45
- * // TypeScript + React 项目
46
- * import pawover from "@pawover/kit-eslint-rules";
231
+ * // TypeScript + React 项目,使用按组 overrides 覆盖规则
232
+ * import pawover from "@pawover/kit/eslint-rules";
47
233
  *
48
234
  * export default pawover({
49
- * typescript: true,
235
+ * typescript: {
236
+ * overrides: { "ts/strict-void-return": [1, { allowReturnAny: true }] },
237
+ * },
50
238
  * react: true,
51
- * parserOptions: { tsconfigRootDir: import.meta.dirname },
52
239
  * });
53
240
  * ```
54
241
  *
55
242
  * @example
56
243
  * ```ts
57
- * // Vue 项目(Vue SFC + TypeScript)
244
+ * // 追加自定义配置层
58
245
  * import pawover from "@pawover/kit-eslint-rules";
59
246
  *
60
- * export default pawover({ typescript: true, vue: true });
247
+ * export default pawover(
248
+ * { typescript: true },
249
+ * { rules: { "no-console": "error" } },
250
+ * );
61
251
  * ```
62
252
  *
63
253
  * @example
64
254
  * ```ts
65
- * // 后置覆盖:在预设之后追加自定义配置层
255
+ * // 按层名称覆盖
66
256
  * import pawover from "@pawover/kit-eslint-rules";
67
257
  *
68
- * export default [
69
- * ...pawover({ typescript: true }),
70
- * { rules: { "ts/no-explicit-any": 0 } },
71
- * ];
258
+ * export default pawover({ typescript: true })
259
+ * .override("pawover/typescript/rules", {
260
+ * rules: { "ts/consistent-type-definitions": ["error", "interface"] },
261
+ * });
72
262
  * ```
73
263
  */
74
- declare function pawover(options?: PawoverOptions): Linter.Config[];
264
+ declare function pawover(options?: PawoverOptions, ...userConfigs: PawoverUserConfig[]): FlatConfigComposer<ConfigLayer, ConfigNames>;
75
265
  /**
76
266
  * 全局忽略目录列表
77
267
  *
78
268
  * 在 ESLint 配置中直接忽略的目录与文件 glob 集合,包含依赖、构建产物、锁文件、编辑器目录、缓存等。
79
- * 预设工厂(`src/config.ts`)将其作为返回数组的首项。
269
+ * 预设工厂(`src/config.ts`)将其作为 `pawover/ignores` 配置层的 `ignores` 值。
80
270
  *
81
271
  * @example
82
272
  * ```ts
@@ -89,6 +279,56 @@ declare function pawover(options?: PawoverOptions): Linter.Config[];
89
279
  * ```
90
280
  */
91
281
  declare const GLOB_EXCLUDE: string[];
282
+ /**
283
+ * 全部源文件(js / cjs / mjs / jsx / ts / tsx)
284
+ *
285
+ * 各规则组共享的基础文件范围;启用 `vue` 时工厂会在其后追加 {@link GLOB_VUE}。
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * import { GLOB_SOURCE, GLOB_VUE } from "@pawover/kit-eslint-rules";
290
+ *
291
+ * const files = [...GLOB_SOURCE, GLOB_VUE];
292
+ * ```
293
+ */
294
+ declare const GLOB_SOURCE: string[];
295
+ /**
296
+ * TypeScript 源文件(ts / tsx)
297
+ *
298
+ * 类型感知规则与 TypeScript parser 的默认作用范围;后置覆盖 `ts/*` 类型感知规则时应使用该常量限定 `files`。
299
+ *
300
+ * @example
301
+ * ```ts
302
+ * import { GLOB_TYPESCRIPT } from "@pawover/kit-eslint-rules";
303
+ *
304
+ * const files = GLOB_TYPESCRIPT;
305
+ * ```
306
+ */
307
+ declare const GLOB_TYPESCRIPT: string[];
308
+ /**
309
+ * Vue 单文件组件
310
+ *
311
+ * @example
312
+ * ```ts
313
+ * import { GLOB_VUE } from "@pawover/kit-eslint-rules";
314
+ *
315
+ * const files = [GLOB_VUE];
316
+ * ```
317
+ */
318
+ declare const GLOB_VUE = "**/*.vue";
319
+ /**
320
+ * JSX 组件文件
321
+ *
322
+ * react 的 type-aware 规则在启用 `typescript` 时作用于 jsx;类型是否可用由项目 tsconfig 的 include / allowJs 决定。
323
+ *
324
+ * @example
325
+ * ```ts
326
+ * import { GLOB_JSX } from "@pawover/kit-eslint-rules";
327
+ *
328
+ * const files = [GLOB_JSX];
329
+ * ```
330
+ */
331
+ declare const GLOB_JSX = "**/*.jsx";
92
332
  /**
93
333
  * 规则严重级别(数字形式)
94
334
  *
@@ -137,4 +377,4 @@ export type Severity = SeverityName | SeverityLevel;
137
377
  * ```
138
378
  */
139
379
  export type RuleConfig<RuleOptions extends unknown[] = unknown[]> = Severity | [Severity, ...Partial<RuleOptions>];
140
- export { GLOB_EXCLUDE, type PawoverOptions, pawover as default };
380
+ export { type ConfigLayer, type ConfigNames, GLOB_EXCLUDE, GLOB_JSX, GLOB_SOURCE, GLOB_TYPESCRIPT, GLOB_VUE, type PawoverOptions, type PawoverUserConfig, type ReactCompilerPreset, type ReactOptions, type RuleMap, type RulesOverrideOptions, type TypeScriptOptions, pawover as default };
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { composer } from "eslint-flat-config-utils";
1
2
  import eslintReact from "@eslint-react/eslint-plugin";
2
3
  import stylistic from "@stylistic/eslint-plugin";
3
4
  import antfu from "eslint-plugin-antfu";
@@ -16,6 +17,8 @@ import typescriptEslint from "typescript-eslint";
16
17
  var preset_default = {
17
18
  /** 缩进大小 */
18
19
  indent: 2,
20
+ /** 常规文件行宽 */
21
+ printWidth: 120,
19
22
  /** TypeScript 类成员排序 */
20
23
  tsMemberOrder: [
21
24
  "signature",
@@ -175,7 +178,12 @@ const rules$8 = {
175
178
  * @descEN Enforce multiline objects, arrays and ternaries when they are complex
176
179
  * @see https://github.com/antfu/eslint-plugin-antfu/blob/main/src/rules/reasonable-newline.md
177
180
  */
178
- "antfu/reasonable-newline": 0,
181
+ "antfu/reasonable-newline": [2, {
182
+ minObjectProperties: 4,
183
+ objectLineLength: preset_default.printWidth,
184
+ minArrayItems: 4,
185
+ arrayLineLength: preset_default.printWidth
186
+ }],
179
187
  /**
180
188
  * @desc 强制顶层函数使用 function 关键字声明
181
189
  * @descEN Enforce top-level functions to be declared with the `function` keyword
@@ -1508,7 +1516,7 @@ const rules$4 = {
1508
1516
  * @descEN Validates against assignment/mutation of globals during render
1509
1517
  * @see https://www.eslint-react.xyz/docs/rules/globals
1510
1518
  */
1511
- "react/globals": 0,
1519
+ "react/globals": 2,
1512
1520
  /**
1513
1521
  * @desc 禁止修改 props、state 等不可变值
1514
1522
  * @descEN Validates against mutating props, state, and other values that are immutable
@@ -2074,7 +2082,7 @@ const rules$3 = {
2074
2082
  * @descEN Validates against usage of libraries which are incompatible with memoization
2075
2083
  * @see https://react.dev/reference/eslint-plugin-react-hooks/lints/incompatible-library
2076
2084
  */
2077
- "react-hooks/incompatible-library": 2,
2085
+ "react-hooks/incompatible-library": 1,
2078
2086
  /**
2079
2087
  * @desc 验证现有手动的记忆化被编译器保留
2080
2088
  * @descEN Validates that existing manual memoization is preserved by the compiler
@@ -2122,7 +2130,13 @@ const rules$3 = {
2122
2130
  * @descEN Validates usage of the useMemo hook without a return value
2123
2131
  * @see https://react.dev/reference/eslint-plugin-react-hooks/lints/use-memo
2124
2132
  */
2125
- "react-hooks/use-memo": 0
2133
+ "react-hooks/use-memo": 0,
2134
+ /**
2135
+ * @desc 验证 useMemo 始终返回值且结果被组件或 Hook 使用
2136
+ * @descEN Validates that useMemos always return a value and that the result of the useMemo is used by the component/hook
2137
+ * @see https://react.dev/reference/eslint-plugin-react-hooks/lints/void-use-memo
2138
+ */
2139
+ "react-hooks/void-use-memo": 0
2126
2140
  };
2127
2141
  // src/core.stylistic.ts
2128
2142
  /**
@@ -3644,7 +3658,7 @@ const rules$1 = {
3644
3658
  * @descEN Disallow passing a value-returning function in a position accepting a void function
3645
3659
  * @see https://typescript-eslint.io/rules/strict-void-return
3646
3660
  */
3647
- "ts/strict-void-return": 1,
3661
+ "ts/strict-void-return": [1, { allowReturnAny: true }],
3648
3662
  /**
3649
3663
  * @desc 要求 switch-case 语句穷尽所有可能
3650
3664
  * @descEN Require switch-case statements to be exhaustive
@@ -4022,7 +4036,7 @@ const rules = {
4022
4036
  * 全局忽略目录列表
4023
4037
  *
4024
4038
  * 在 ESLint 配置中直接忽略的目录与文件 glob 集合,包含依赖、构建产物、锁文件、编辑器目录、缓存等。
4025
- * 预设工厂(`src/config.ts`)将其作为返回数组的首项。
4039
+ * 预设工厂(`src/config.ts`)将其作为 `pawover/ignores` 配置层的 `ignores` 值。
4026
4040
  *
4027
4041
  * @example
4028
4042
  * ```ts
@@ -4068,6 +4082,57 @@ const GLOB_EXCLUDE = [
4068
4082
  "**/auto-component?(s).d.ts",
4069
4083
  "**/auto-router?(s).d.ts"
4070
4084
  ];
4085
+ // src/globs.ts
4086
+ /**
4087
+ * 全部源文件(js / cjs / mjs / jsx / ts / tsx)
4088
+ *
4089
+ * 各规则组共享的基础文件范围;启用 `vue` 时工厂会在其后追加 {@link GLOB_VUE}。
4090
+ *
4091
+ * @example
4092
+ * ```ts
4093
+ * import { GLOB_SOURCE, GLOB_VUE } from "@pawover/kit-eslint-rules";
4094
+ *
4095
+ * const files = [...GLOB_SOURCE, GLOB_VUE];
4096
+ * ```
4097
+ */
4098
+ const GLOB_SOURCE = ["**/*.{js,cjs,mjs,jsx,ts,tsx}"];
4099
+ /**
4100
+ * TypeScript 源文件(ts / tsx)
4101
+ *
4102
+ * 类型感知规则与 TypeScript parser 的默认作用范围;后置覆盖 `ts/*` 类型感知规则时应使用该常量限定 `files`。
4103
+ *
4104
+ * @example
4105
+ * ```ts
4106
+ * import { GLOB_TYPESCRIPT } from "@pawover/kit-eslint-rules";
4107
+ *
4108
+ * const files = GLOB_TYPESCRIPT;
4109
+ * ```
4110
+ */
4111
+ const GLOB_TYPESCRIPT = ["**/*.{ts,tsx}"];
4112
+ /**
4113
+ * Vue 单文件组件
4114
+ *
4115
+ * @example
4116
+ * ```ts
4117
+ * import { GLOB_VUE } from "@pawover/kit-eslint-rules";
4118
+ *
4119
+ * const files = [GLOB_VUE];
4120
+ * ```
4121
+ */
4122
+ const GLOB_VUE = "**/*.vue";
4123
+ /**
4124
+ * JSX 组件文件
4125
+ *
4126
+ * react 的 type-aware 规则在启用 `typescript` 时作用于 jsx;类型是否可用由项目 tsconfig 的 include / allowJs 决定。
4127
+ *
4128
+ * @example
4129
+ * ```ts
4130
+ * import { GLOB_JSX } from "@pawover/kit-eslint-rules";
4131
+ *
4132
+ * const files = [GLOB_JSX];
4133
+ * ```
4134
+ */
4135
+ const GLOB_JSX = "**/*.jsx";
4071
4136
  // src/plugins.ts
4072
4137
  /**
4073
4138
  * 内嵌插件注册表
@@ -4127,7 +4192,120 @@ const reactWithoutTypeChecked = {
4127
4192
  }))
4128
4193
  }
4129
4194
  };
4195
+ // src/reactCompiler.ts
4196
+ /**
4197
+ * `react-hooks` 与 `@eslint-react` 共享的 Compiler 规则映射
4198
+ *
4199
+ * 键为 `eslint-plugin-react-hooks` 的规则名,值为 `@eslint-react` 的等价规则名;
4200
+ * 切换时关闭后者、启用前者,避免同一问题重复报告。
4201
+ */
4202
+ const REACT_COMPILER_RULE_MAP = {
4203
+ "error-boundaries": "error-boundaries",
4204
+ "globals": "globals",
4205
+ "immutability": "immutability",
4206
+ "purity": "purity",
4207
+ "refs": "refs",
4208
+ "set-state-in-effect": "set-state-in-effect",
4209
+ "set-state-in-render": "set-state-in-render",
4210
+ "static-components": "static-components",
4211
+ "unsupported-syntax": "unsupported-syntax",
4212
+ "use-memo": "use-memo"
4213
+ };
4214
+ /** 仅官方 `recommended-latest` 预设包含的实验性 Compiler 规则。 */
4215
+ const REACT_COMPILER_LATEST_ONLY_RULES = ["void-use-memo"];
4216
+ /** 官方 flat config 预设名。 */
4217
+ const REACT_HOOKS_FLAT_CONFIG_NAMES = {
4218
+ recommended: "recommended",
4219
+ latest: "recommended-latest"
4220
+ };
4221
+ /** 将官方严重级别归一化为 preset 使用的数字形式。 */
4222
+ function normalizeRuleLevel(level) {
4223
+ if (level === "error") return 2;
4224
+ if (level === "warn") return 1;
4225
+ if (level === "off") return 0;
4226
+ return level;
4227
+ }
4228
+ /** 读取官方 `eslint-plugin-react-hooks` 预设的规则表。 */
4229
+ function getReactHooksPresetRules(preset) {
4230
+ const configs = plugins.reactHooks.configs.flat;
4231
+ const configName = REACT_HOOKS_FLAT_CONFIG_NAMES[preset];
4232
+ const rules = configs[configName]?.rules;
4233
+ if (!rules) throw new Error(`[pawover] 无法读取 eslint-plugin-react-hooks 的 "${configName}" 预设规则。`);
4234
+ return rules;
4235
+ }
4236
+ /**
4237
+ * 生成 React Compiler 切换规则
4238
+ *
4239
+ * 返回 `off`(需关闭的 `@eslint-react` 等价规则)与 `on`(需开启的 `react-hooks` 规则):
4240
+ * 严重级别直接取官方预设,避免与上游版本漂移。
4241
+ */
4242
+ function getReactCompilerRuleMap(preset) {
4243
+ const presetRules = getReactHooksPresetRules(preset);
4244
+ const off = {};
4245
+ const on = {};
4246
+ for (const [hooksRule, eslintReactRule] of Object.entries(REACT_COMPILER_RULE_MAP)) {
4247
+ const ruleId = `react-hooks/${hooksRule}`;
4248
+ const level = presetRules[ruleId];
4249
+ if (level === void 0) throw new Error(`[pawover] eslint-plugin-react-hooks 的 "${preset}" 预设缺少规则 ${ruleId}。`);
4250
+ off[`react/${eslintReactRule}`] = 0;
4251
+ on[ruleId] = normalizeRuleLevel(level);
4252
+ }
4253
+ if (preset === "latest") for (const rule of REACT_COMPILER_LATEST_ONLY_RULES) {
4254
+ const ruleId = `react-hooks/${rule}`;
4255
+ const level = presetRules[ruleId];
4256
+ if (level === void 0) throw new Error(`[pawover] eslint-plugin-react-hooks 的 "latest" 预设缺少规则 ${ruleId}。`);
4257
+ on[ruleId] = normalizeRuleLevel(level);
4258
+ }
4259
+ return {
4260
+ off,
4261
+ on
4262
+ };
4263
+ }
4264
+ /** 关闭全部 10 条重叠规则(`compiler: false` 回退到 `@eslint-react` 时使用)。 */
4265
+ function getReactCompilerDisabledRuleMap() {
4266
+ return Object.fromEntries(Object.keys(REACT_COMPILER_RULE_MAP).map((rule) => [`react-hooks/${rule}`, 0]));
4267
+ }
4130
4268
  // src/config.ts
4269
+ /** 层名字典:预设产出的全部唯一配置层名称。 */
4270
+ const LAYER_NAMES = {
4271
+ ignores: "pawover/ignores",
4272
+ javascript: {
4273
+ parser: "pawover/javascript/parser",
4274
+ rules: "pawover/javascript/rules"
4275
+ },
4276
+ typescript: {
4277
+ setup: "pawover/typescript/setup",
4278
+ parser: "pawover/typescript/parser",
4279
+ rules: "pawover/typescript/rules",
4280
+ typeAware: "pawover/typescript/type-aware"
4281
+ },
4282
+ stylistic: {
4283
+ setup: "pawover/stylistic/setup",
4284
+ rules: "pawover/stylistic/rules"
4285
+ },
4286
+ antfu: {
4287
+ setup: "pawover/antfu/setup",
4288
+ rules: "pawover/antfu/rules"
4289
+ },
4290
+ imports: {
4291
+ setup: "pawover/imports/setup",
4292
+ rules: "pawover/imports/rules"
4293
+ },
4294
+ importsSort: {
4295
+ setup: "pawover/imports-sort/setup",
4296
+ rules: "pawover/imports-sort/rules"
4297
+ },
4298
+ react: {
4299
+ setup: "pawover/react/setup",
4300
+ rules: "pawover/react/rules",
4301
+ typeAware: "pawover/react/type-aware"
4302
+ },
4303
+ vue: {
4304
+ setup: "pawover/vue/setup",
4305
+ parser: "pawover/vue/parser",
4306
+ rules: "pawover/vue/rules"
4307
+ }
4308
+ };
4131
4309
  const javascript = rules$5;
4132
4310
  const typescript = rules$1;
4133
4311
  const stylistic$1 = rules$2;
@@ -4139,81 +4317,137 @@ const reactHooks$1 = rules$3;
4139
4317
  const vue$1 = rules;
4140
4318
  /** react 映射中依赖类型信息的规则 id(`react/` 前缀) */
4141
4319
  const reactTypeCheckedRuleIds = new Set(reactTypeCheckedNames.map((name) => `react/${name}`));
4142
- /** react 映射中的 type-aware 规则子集(仅在 ts/tsx 层启用) */
4320
+ /** react 映射中的 type-aware 规则子集(仅在 ts/tsx/jsx 层启用) */
4143
4321
  const reactTypeCheckedRules = Object.fromEntries([...reactTypeCheckedRuleIds].flatMap((ruleId) => {
4144
4322
  const rule = react[ruleId];
4145
4323
  return rule === void 0 ? [] : [[ruleId, rule]];
4146
4324
  }));
4147
4325
  /** react 映射中剔除 type-aware 规则后的基础规则表(作用于全部源文件) */
4148
4326
  const reactBase = Object.fromEntries(Object.entries(react).filter(([ruleId]) => !reactTypeCheckedRuleIds.has(ruleId)));
4149
- /** 全部源文件(js / cjs / mjs / jsx / ts / tsx) */
4150
- const GLOB_SOURCE = ["**/*.{js,cjs,mjs,jsx,ts,tsx}"];
4151
- /** TypeScript 源文件(ts / tsx) */
4152
- const GLOB_TYPESCRIPT = ["**/*.{ts,tsx}"];
4153
- /** Vue 单文件组件 */
4154
- const GLOB_VUE = "**/*.vue";
4155
- /** JSX 组件文件(react type-aware 规则的检查对象;类型可用性由项目 tsconfig 的 include/allowJs 决定) */
4156
- const GLOB_JSX = "**/*.jsx";
4327
+ /** 追加「setup(注册插件)→ parser(可选)→ rules(规则与文件范围)」三层。 */
4328
+ function addGroup(configs, options) {
4329
+ configs.push({
4330
+ name: options.setup,
4331
+ plugins: { [options.plugin]: options.pluginImpl }
4332
+ });
4333
+ if (options.parserLayer) configs.push(options.parserLayer);
4334
+ configs.push({
4335
+ name: options.rules,
4336
+ files: [...options.files],
4337
+ rules: {
4338
+ ...options.ruleMap,
4339
+ ...options.overrides ?? {}
4340
+ }
4341
+ });
4342
+ }
4343
+ /** 将布尔 / 对象选项归一化为选项对象。 */
4344
+ function resolveOptions(value) {
4345
+ return typeof value === "object" && value !== null ? value : {};
4346
+ }
4347
+ /** 将 React Compiler 开关归一化为官方预设名;`false` 表示回退到 `@eslint-react` 实现。 */
4348
+ function resolveReactCompilerPreset(value) {
4349
+ if (value === false) return;
4350
+ return value === "latest" ? "latest" : "recommended";
4351
+ }
4157
4352
  /**
4158
4353
  * 创建 ESLint flat config 预设
4159
4354
  *
4160
- * 返回可直接用于 `eslint.config.js` 的 flat config 数组:首项为全局忽略(`GLOB_EXCLUDE`),随后为常开的 `javascript` 基础层与按选项启用的各规则组层。
4161
- * 全部上游插件已作为本包依赖精确锁定并自动注册,无需用户另行安装或注册插件。
4355
+ * 返回 composer(继承 Promise,可直接用于 `eslint.config.js` 的默认导出),也可链式调用
4356
+ * `append` / `prepend` / `override` / `renamePlugins` 等能力继续组合。
4357
+ *
4358
+ * 各规则组拆为 `setup` 层(只注册插件,不带 `files`)、`parser` 层(解析器)与
4359
+ * `rules` / `type-aware` 层(规则与文件范围),因此后置配置可以直接使用预设已注册的插件规则;
4360
+ * 若需注册自定义插件,在自己的配置层中挂载即可。
4162
4361
  *
4163
- * @param options 预设选项;缺省时仅返回忽略层与 `javascript` 基础层
4164
- * @returns flat config 配置数组
4362
+ * @param options 预设选项;缺省时返回忽略层与 `javascript` parser/rules 层
4363
+ * @param userConfigs 追加在预设之后的 flat config(单个对象、数组、composer 或 Promise)
4364
+ * @returns 可直接导出或继续链式组合的 flat config composer
4165
4365
  *
4166
4366
  * @example
4167
4367
  * ```ts
4168
- * // TypeScript + React 项目
4169
- * import pawover from "@pawover/kit-eslint-rules";
4368
+ * // TypeScript + React 项目,使用按组 overrides 覆盖规则
4369
+ * import pawover from "@pawover/kit/eslint-rules";
4170
4370
  *
4171
4371
  * export default pawover({
4172
- * typescript: true,
4372
+ * typescript: {
4373
+ * overrides: { "ts/strict-void-return": [1, { allowReturnAny: true }] },
4374
+ * },
4173
4375
  * react: true,
4174
- * parserOptions: { tsconfigRootDir: import.meta.dirname },
4175
4376
  * });
4176
4377
  * ```
4177
4378
  *
4178
4379
  * @example
4179
4380
  * ```ts
4180
- * // Vue 项目(Vue SFC + TypeScript)
4381
+ * // 追加自定义配置层
4181
4382
  * import pawover from "@pawover/kit-eslint-rules";
4182
4383
  *
4183
- * export default pawover({ typescript: true, vue: true });
4384
+ * export default pawover(
4385
+ * { typescript: true },
4386
+ * { rules: { "no-console": "error" } },
4387
+ * );
4184
4388
  * ```
4185
4389
  *
4186
4390
  * @example
4187
4391
  * ```ts
4188
- * // 后置覆盖:在预设之后追加自定义配置层
4392
+ * // 按层名称覆盖
4189
4393
  * import pawover from "@pawover/kit-eslint-rules";
4190
4394
  *
4191
- * export default [
4192
- * ...pawover({ typescript: true }),
4193
- * { rules: { "ts/no-explicit-any": 0 } },
4194
- * ];
4395
+ * export default pawover({ typescript: true })
4396
+ * .override("pawover/typescript/rules", {
4397
+ * rules: { "ts/consistent-type-definitions": ["error", "interface"] },
4398
+ * });
4195
4399
  * ```
4196
4400
  */
4197
- function pawover(options = {}) {
4198
- const { typescript: enableTypescript = false, stylistic: enableStylistic = false, antfu: enableAntfu = false, imports: enableImports = false, importsSort: enableImportsSort = false, react: enableReact = false, vue: enableVue = false, parserOptions } = options;
4401
+ function pawover(options = {}, ...userConfigs) {
4402
+ const { javascript: javascriptOptions, stylistic: enableStylistic = false, antfu: enableAntfu = false, imports: enableImports = false, importsSort: enableImportsSort = false, react: enableReact = false, vue: enableVue = false } = options;
4403
+ const enableTypescript = Boolean(options.typescript);
4404
+ const typescriptOptions = resolveOptions(options.typescript);
4405
+ const stylisticOptions = resolveOptions(options.stylistic);
4406
+ const antfuOptions = resolveOptions(options.antfu);
4407
+ const importsOptions = resolveOptions(options.imports);
4408
+ const importsSortOptions = resolveOptions(options.importsSort);
4409
+ const reactOptions = resolveOptions(options.react);
4410
+ const reactCompilerPreset = resolveReactCompilerPreset(reactOptions.compiler);
4411
+ const vueOptions = resolveOptions(options.vue);
4199
4412
  const sourceFiles = enableVue ? [...GLOB_SOURCE, GLOB_VUE] : [...GLOB_SOURCE];
4200
4413
  const typescriptParserOptions = {
4201
4414
  projectService: true,
4202
- ...parserOptions
4415
+ ...typescriptOptions.parserOptions ?? {}
4203
4416
  };
4204
- const configs = [{ ignores: [...GLOB_EXCLUDE] }, {
4205
- files: [...sourceFiles],
4206
- languageOptions: {
4207
- ecmaVersion: "latest",
4208
- sourceType: "module",
4209
- parserOptions: { ecmaFeatures: { jsx: true } }
4417
+ const filesTypeAware = typescriptOptions.filesTypeAware ?? [...GLOB_TYPESCRIPT];
4418
+ const reactTypeAwareFiles = [.../* @__PURE__ */ new Set([...filesTypeAware, GLOB_JSX])];
4419
+ const configs = [
4420
+ {
4421
+ name: LAYER_NAMES.ignores,
4422
+ ignores: [...GLOB_EXCLUDE]
4423
+ },
4424
+ {
4425
+ name: LAYER_NAMES.javascript.parser,
4426
+ files: [...sourceFiles],
4427
+ languageOptions: {
4428
+ ecmaVersion: "latest",
4429
+ sourceType: "module",
4430
+ parserOptions: { ecmaFeatures: { jsx: true } }
4431
+ }
4210
4432
  },
4211
- rules: javascript
4212
- }];
4433
+ {
4434
+ name: LAYER_NAMES.javascript.rules,
4435
+ files: [...sourceFiles],
4436
+ rules: {
4437
+ ...javascript,
4438
+ ...javascriptOptions?.overrides ?? {}
4439
+ }
4440
+ }
4441
+ ];
4213
4442
  if (enableTypescript) {
4214
4443
  const typescriptRuleFiles = enableVue ? [...GLOB_TYPESCRIPT, GLOB_VUE] : [...GLOB_TYPESCRIPT];
4215
4444
  const typescriptParserFiles = enableReact ? [...typescriptRuleFiles, GLOB_JSX] : [...typescriptRuleFiles];
4216
4445
  configs.push({
4446
+ name: LAYER_NAMES.typescript.setup,
4447
+ plugins: { ts: plugins.typescript }
4448
+ });
4449
+ configs.push({
4450
+ name: LAYER_NAMES.typescript.parser,
4217
4451
  files: [...typescriptParserFiles],
4218
4452
  languageOptions: {
4219
4453
  parser: plugins.typescriptParser,
@@ -4221,67 +4455,112 @@ function pawover(options = {}) {
4221
4455
  }
4222
4456
  });
4223
4457
  configs.push({
4458
+ name: LAYER_NAMES.typescript.rules,
4224
4459
  files: [...typescriptRuleFiles],
4225
- plugins: { ts: plugins.typescript },
4226
- rules: typescript
4460
+ rules: {
4461
+ ...typescript,
4462
+ ...typescriptOptions.overrides ?? {}
4463
+ }
4464
+ });
4465
+ if (typescriptOptions.overridesTypeAware) configs.push({
4466
+ name: LAYER_NAMES.typescript.typeAware,
4467
+ files: [...filesTypeAware],
4468
+ rules: { ...typescriptOptions.overridesTypeAware }
4227
4469
  });
4228
4470
  }
4229
- if (enableStylistic) configs.push({
4230
- files: [...sourceFiles],
4231
- plugins: { stylistic: plugins.stylistic },
4232
- rules: stylistic$1
4471
+ if (enableStylistic) addGroup(configs, {
4472
+ setup: LAYER_NAMES.stylistic.setup,
4473
+ rules: LAYER_NAMES.stylistic.rules,
4474
+ plugin: "stylistic",
4475
+ pluginImpl: plugins.stylistic,
4476
+ ruleMap: stylistic$1,
4477
+ overrides: stylisticOptions.overrides,
4478
+ files: sourceFiles
4233
4479
  });
4234
- if (enableAntfu) configs.push({
4235
- files: [...sourceFiles],
4236
- plugins: { antfu: plugins.antfu },
4237
- rules: antfu$1
4480
+ if (enableAntfu) addGroup(configs, {
4481
+ setup: LAYER_NAMES.antfu.setup,
4482
+ rules: LAYER_NAMES.antfu.rules,
4483
+ plugin: "antfu",
4484
+ pluginImpl: plugins.antfu,
4485
+ ruleMap: antfu$1,
4486
+ overrides: antfuOptions.overrides,
4487
+ files: sourceFiles
4238
4488
  });
4239
- if (enableImports) configs.push({
4240
- files: [...sourceFiles],
4241
- plugins: { imports: plugins.imports },
4242
- rules: imports
4489
+ if (enableImports) addGroup(configs, {
4490
+ setup: LAYER_NAMES.imports.setup,
4491
+ rules: LAYER_NAMES.imports.rules,
4492
+ plugin: "imports",
4493
+ pluginImpl: plugins.imports,
4494
+ ruleMap: imports,
4495
+ overrides: importsOptions.overrides,
4496
+ files: sourceFiles
4243
4497
  });
4244
- if (enableImportsSort) configs.push({
4245
- files: [...sourceFiles],
4246
- plugins: { importsSort: plugins.importsSort },
4247
- rules: importsSort
4498
+ if (enableImportsSort) addGroup(configs, {
4499
+ setup: LAYER_NAMES.importsSort.setup,
4500
+ rules: LAYER_NAMES.importsSort.rules,
4501
+ plugin: "importsSort",
4502
+ pluginImpl: plugins.importsSort,
4503
+ ruleMap: importsSort,
4504
+ overrides: importsSortOptions.overrides,
4505
+ files: sourceFiles
4248
4506
  });
4249
4507
  if (enableReact) {
4508
+ const reactCompilerRules = reactCompilerPreset ? getReactCompilerRuleMap(reactCompilerPreset) : {
4509
+ off: {},
4510
+ on: getReactCompilerDisabledRuleMap()
4511
+ };
4250
4512
  configs.push({
4251
- files: [...GLOB_SOURCE],
4513
+ name: LAYER_NAMES.react.setup,
4252
4514
  plugins: {
4253
4515
  "react": enableTypescript ? plugins.react : reactWithoutTypeChecked,
4254
4516
  "react-hooks": plugins.reactHooks
4255
- },
4256
- rules: enableTypescript ? {
4257
- ...reactBase,
4258
- ...reactHooks$1
4259
- } : {
4517
+ }
4518
+ });
4519
+ configs.push({
4520
+ name: LAYER_NAMES.react.rules,
4521
+ files: [...GLOB_SOURCE],
4522
+ rules: {
4260
4523
  ...reactBase,
4261
4524
  ...reactHooks$1,
4262
- ...reactTypeCheckedOff
4525
+ ...enableTypescript ? {} : reactTypeCheckedOff,
4526
+ ...reactCompilerRules.off,
4527
+ ...reactCompilerRules.on,
4528
+ ...reactOptions.overrides ?? {}
4263
4529
  }
4264
4530
  });
4265
4531
  if (enableTypescript) configs.push({
4266
- files: [...GLOB_TYPESCRIPT, GLOB_JSX],
4267
- plugins: { react: plugins.react },
4268
- rules: { ...reactTypeCheckedRules }
4532
+ name: LAYER_NAMES.react.typeAware,
4533
+ files: [...reactTypeAwareFiles],
4534
+ rules: {
4535
+ ...reactTypeCheckedRules,
4536
+ ...reactOptions.overridesTypeAware ?? {}
4537
+ }
4269
4538
  });
4270
4539
  }
4271
- if (enableVue) configs.push({
4272
- files: [GLOB_VUE],
4273
- languageOptions: {
4274
- parser: plugins.vueParser,
4275
- parserOptions: enableTypescript ? {
4276
- parser: plugins.typescriptParser,
4277
- ...typescriptParserOptions
4278
- } : {}
4540
+ if (enableVue) addGroup(configs, {
4541
+ setup: LAYER_NAMES.vue.setup,
4542
+ parserLayer: {
4543
+ name: LAYER_NAMES.vue.parser,
4544
+ files: [GLOB_VUE],
4545
+ languageOptions: {
4546
+ parser: plugins.vueParser,
4547
+ parserOptions: enableTypescript ? {
4548
+ parser: plugins.typescriptParser,
4549
+ ...typescriptParserOptions
4550
+ } : {}
4551
+ }
4279
4552
  },
4280
- plugins: { vue: plugins.vue },
4281
- rules: vue$1
4553
+ rules: LAYER_NAMES.vue.rules,
4554
+ plugin: "vue",
4555
+ pluginImpl: plugins.vue,
4556
+ ruleMap: vue$1,
4557
+ overrides: vueOptions.overrides,
4558
+ files: [GLOB_VUE]
4282
4559
  });
4283
- return configs;
4560
+ let result = composer(...configs);
4561
+ if (userConfigs.length > 0) result = result.append(...userConfigs);
4562
+ return result;
4284
4563
  }
4285
4564
  // src/index.ts
4286
4565
  var src_default = pawover;
4287
- export { GLOB_EXCLUDE, src_default as default };
4566
+ export { GLOB_EXCLUDE, GLOB_JSX, GLOB_SOURCE, GLOB_TYPESCRIPT, GLOB_VUE, src_default as default };
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "url": "https://github.com/pawover"
7
7
  },
8
8
  "description": "Self-contained ESLint flat config preset: javascript, typescript, react, vue, stylistic, antfu and import rules, with pinned bundled plugins",
9
- "version": "0.1.0-alpha.2",
9
+ "version": "0.2.0-alpha.0",
10
10
  "type": "module",
11
11
  "keywords": [
12
12
  "pawover",
@@ -46,6 +46,7 @@
46
46
  "dependencies": {
47
47
  "@eslint-react/eslint-plugin": "5.28.8",
48
48
  "@stylistic/eslint-plugin": "6.0.0-beta.6",
49
+ "eslint-flat-config-utils": "3.2.0",
49
50
  "eslint-plugin-antfu": "3.3.1",
50
51
  "eslint-plugin-import-lite": "0.6.0",
51
52
  "eslint-plugin-react-hooks": "7.1.1",