@fast-china/eslint-config 2.1.7 → 2.1.8

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 (88) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/CONTRIBUTING.md +2 -2
  3. package/README.md +66 -142
  4. package/README.zh.md +64 -142
  5. package/dist/configs/environment.mjs +2 -0
  6. package/dist/configs/environment.mjs.map +1 -1
  7. package/dist/configs/ignores.mjs +0 -1
  8. package/dist/configs/ignores.mjs.map +1 -1
  9. package/dist/configs/import.mjs +4 -1
  10. package/dist/configs/import.mjs.map +1 -1
  11. package/dist/configs/index.d.mts +2 -2
  12. package/dist/configs/index.mjs +2 -2
  13. package/dist/configs/javascript.mjs +3 -1
  14. package/dist/configs/javascript.mjs.map +1 -1
  15. package/dist/configs/json.mjs +3 -1
  16. package/dist/configs/json.mjs.map +1 -1
  17. package/dist/configs/markdown.d.mts +2 -1
  18. package/dist/configs/markdown.mjs +4 -2
  19. package/dist/configs/markdown.mjs.map +1 -1
  20. package/dist/configs/prettier.mjs +1 -1
  21. package/dist/configs/prettier.mjs.map +1 -1
  22. package/dist/configs/sort-package.d.mts +1 -1
  23. package/dist/configs/sort-package.mjs +1 -1
  24. package/dist/configs/sort-package.mjs.map +1 -1
  25. package/dist/configs/sort-tsconfig.d.mts +2 -2
  26. package/dist/configs/sort-tsconfig.mjs +2 -2
  27. package/dist/configs/sort-tsconfig.mjs.map +1 -1
  28. package/dist/configs/typescript.d.mts +11 -7
  29. package/dist/configs/typescript.mjs +25 -22
  30. package/dist/configs/typescript.mjs.map +1 -1
  31. package/dist/configs/uniapp.mjs +19 -13
  32. package/dist/configs/uniapp.mjs.map +1 -1
  33. package/dist/configs/vue.d.mts +18 -3
  34. package/dist/configs/vue.mjs +59 -9
  35. package/dist/configs/vue.mjs.map +1 -1
  36. package/dist/constants/index.d.mts +4 -0
  37. package/dist/constants/index.mjs +5 -1
  38. package/dist/constants/index.mjs.map +1 -1
  39. package/dist/index.d.mts +37 -58
  40. package/dist/index.mjs +43 -47
  41. package/dist/index.mjs.map +1 -1
  42. package/dist/rules/angular.d.mts +28 -0
  43. package/dist/rules/angular.mjs +28 -0
  44. package/dist/rules/angular.mjs.map +1 -1
  45. package/dist/rules/common.d.mts +16 -6
  46. package/dist/rules/common.mjs +16 -13
  47. package/dist/rules/common.mjs.map +1 -1
  48. package/dist/rules/import.d.mts +9 -0
  49. package/dist/rules/import.mjs +24 -0
  50. package/dist/rules/import.mjs.map +1 -1
  51. package/dist/rules/index.d.mts +2 -2
  52. package/dist/rules/index.mjs +2 -2
  53. package/dist/rules/javascript.d.mts +18 -0
  54. package/dist/rules/javascript.mjs +18 -0
  55. package/dist/rules/javascript.mjs.map +1 -1
  56. package/dist/rules/lodash.d.mts +2 -0
  57. package/dist/rules/lodash.mjs +6 -2
  58. package/dist/rules/lodash.mjs.map +1 -1
  59. package/dist/rules/react.d.mts +13 -0
  60. package/dist/rules/react.mjs +13 -0
  61. package/dist/rules/react.mjs.map +1 -1
  62. package/dist/rules/regexp.d.mts +25 -0
  63. package/dist/rules/regexp.mjs +25 -0
  64. package/dist/rules/regexp.mjs.map +1 -1
  65. package/dist/rules/sort-package.d.mts +3 -1
  66. package/dist/rules/sort-package.mjs +3 -1
  67. package/dist/rules/sort-package.mjs.map +1 -1
  68. package/dist/rules/sort-tsconfig.d.mts +3 -1
  69. package/dist/rules/sort-tsconfig.mjs +3 -1
  70. package/dist/rules/sort-tsconfig.mjs.map +1 -1
  71. package/dist/rules/typescript.d.mts +81 -13
  72. package/dist/rules/typescript.mjs +83 -15
  73. package/dist/rules/typescript.mjs.map +1 -1
  74. package/dist/rules/vue.d.mts +65 -10
  75. package/dist/rules/vue.mjs +41 -14
  76. package/dist/rules/vue.mjs.map +1 -1
  77. package/docs/engineering-audit.zh.md +45 -49
  78. package/docs/rules/angular.zh.md +721 -0
  79. package/docs/rules/core.zh.md +3031 -0
  80. package/docs/rules/index.zh.md +38 -0
  81. package/docs/rules/json.zh.md +784 -0
  82. package/docs/rules/markdown.zh.md +449 -0
  83. package/docs/rules/react.zh.md +2091 -0
  84. package/docs/rules/typescript.zh.md +2051 -0
  85. package/docs/rules/vue.zh.md +3016 -0
  86. package/docs/rules-risk.md +85 -91
  87. package/docs/rules-risk.zh.md +76 -82
  88. package/package.json +4 -2
@@ -1,119 +1,113 @@
1
1
  # Default Rules and Risk Guide
2
2
 
3
- This document describes the current 2.1.7 configuration model, major rules, and migration risks. Source comments explain current intent only; historical changes belong in `CHANGELOG.md`.
3
+ This guide describes the current configuration model, major rules, and auto-fix risks. Source comments explain current intent; historical differences belong in `CHANGELOG.md`.
4
+
5
+ ## Policy
6
+
7
+ Rules are selected in this order:
8
+
9
+ 1. Follow common JavaScript, TypeScript, and Vue conventions while keeping code concise and readable.
10
+ 2. Detect real bugs and type-safety problems.
11
+ 3. Preserve cross-project consistency.
12
+ 4. Apply Fast project style preferences last.
13
+
14
+ Rules that only satisfy ESLint by changing valid semantics, adding boilerplate, or forcing uncommon forms do not belong in the defaults.
4
15
 
5
16
  ## Configuration model
6
17
 
7
- SDKs, OA systems, administration apps, Vue web apps, and UniApp clients share one JavaScript, TypeScript, Import, and RegExp rule set. There are no strictness tiers.
18
+ Vue 3 and UniApp are separate named complete configurations from the root entry:
8
19
 
9
- The root entry is a fixed Vue 3 + TypeScript + UniApp preset:
20
+ - `vueConfig` handles plain Vue projects only.
21
+ - `uniAppConfig` adds `.nvue`, UniApp globals, manifest behavior, and the `unpackage` ignore.
10
22
 
11
- - `environment: "browser"`
12
- - `.gitignore`
13
- - JavaScript and type-aware TypeScript
14
- - Vue 3, `.nvue`, UniApp globals, and manifest handling
15
- - Import, RegExp, JSON, JSONC, and JSON5
16
- - `package.json` and `tsconfig*.json` sorting
17
- - Prettier conflict handling
18
- - Node.js tooling globals and trailing rule overrides
23
+ The root entry has no default export. It exposes `vueConfig`, `uniAppConfig`, `createVueProjectConfigs()`, `createUniAppProjectConfigs()`, `createBaseConfigs()`, `defineRules()`, and their types.
19
24
 
20
- `fastConfig()` retains only `environment`. React, Angular, Markdown, and Lodash compose from `./configs`; rules, globals, ignores, and special parser settings use trailing Flat Config objects.
25
+ SDKs, Node.js, and other framework-neutral projects use `createBaseConfigs()`. React, Angular, Markdown, and Lodash compose explicitly from `./configs`; manifest sorting is part of every complete configuration.
21
26
 
22
27
  ## Preset sources
23
28
 
24
- | Domain | Preset or implementation |
25
- | ---------- | ----------------------------------------------------------------------------- |
26
- | JavaScript | `@eslint/js` recommended plus local rules |
27
- | TypeScript | typescript-eslint strict and stylistic type-checked presets + Project Service |
28
- | Vue | `eslint-plugin-vue` `flat/recommended` plus type-aware TypeScript |
29
- | React | `@eslint-react` recommended/type-checked plus React Hooks Flat Recommended |
30
- | Angular | Angular ESLint TypeScript, template, and accessibility recommended presets |
31
- | JSON | `eslint-plugin-jsonc` recommended presets for three dialects |
32
- | Import | `eslint-plugin-import-x` recommended plus a fixed ordering policy |
33
- | RegExp | Explicit correctness, safety, and super-linear backtracking rules |
34
- | Prettier | `eslint-config-prettier` conflict disabling |
35
-
36
- ## Major rules
37
-
38
- ### JavaScript
39
-
40
- - `camelcase: ["error", { properties: "never" }]` requires camelCase variables and types while preserving external protocol property names.
41
- - `no-empty` allows intentionally empty catches and still reports other empty blocks.
42
- - `no-eval` and `no-void` are errors; `curly: ["error", "multi-line", "consistent"]` applies, and an existing `default` branch must come last.
43
- - `no-debugger: "error"`
44
- - `no-use-before-define` warns; classes and variables must be declared first while function declarations may be hoisted.
45
- - `prefer-arrow-callback`, `logical-assignment-operators`, and `prefer-object-spread` are errors.
46
- - `prefer-exponentiation-operator` and `prefer-object-has-own` are errors.
47
- - `sort-imports` warns and only sorts members within one import.
48
- - `import-x/order` is an error with `warnOnUnassignedImports: true` and `sortTypesGroup: true`; `@/**` belongs to `internal`, type imports follow every non-style import and retain source-category ordering inside the final type group, and stylesheet imports do not participate in this rule.
49
- - `import-x/style-imports-last` is an error; CSS, SCSS, LESS, and related styles must form the final contiguous import group, with their internal order preserved and no automatic fix.
50
-
51
- ### TypeScript
52
-
53
- - `strictTypeChecked`, `stylisticTypeChecked`, and `projectService: true` are always enabled; TypeScript, TSX, Vue, and NVue share `extraFileExtensions: [".vue", ".nvue"]` to prevent Project Service reloads during mixed-file linting.
54
- - `no-floating-promises` and `strict-void-return` are disabled so Promise waiting follows business semantics; `no-void` rejects `void promise` workarounds. `no-misused-promises`, `await-thenable`, `require-await`, and the unsafe-type rules remain strict, while Promise-returning handlers are allowed in Vue templates and TSX attributes.
55
- - Named TypeScript and TSX functions require explicit return types, except inline callbacks and already typed function expressions; exported module boundaries still require explicit types.
56
- - Vue/NVue SFCs disable function-return and module-boundary annotations and unused parameter checks, while unused variables and imports remain errors. Templates do not infer parameter types back into standalone script handlers, so those parameters still require explicit annotations.
57
- - `explicit-module-boundary-types` is an error and does not allow explicitly typed `any` arguments as an escape hatch.
58
- - `no-explicit-any` warns.
59
- - Regular TS/TSX parameters and caught errors can use an `_` prefix to mark intentional omissions; ordinary variables cannot use that prefix to evade the check.
60
- - `no-empty-function` only allows empty constructors and override methods.
61
- - `consistent-type-imports` fixes type-only dependencies as separate `import type` declarations, and `no-import-type-side-effects` rejects runtime imports containing only inline type specifiers.
62
- - `no-non-null-assertion` is an error.
63
- - `switch-exhaustiveness-check` is an error; `no-deprecated` and `no-unnecessary-condition` warn.
64
- - Numeric template interpolation and concise void arrow callbacks are allowed; dynamic object deletion and static-only utility classes are not forcibly rewritten.
65
- - `consistent-type-exports` and `prefer-readonly` are errors; primitive values are not forced from `||` to `??`.
66
- - `no-eval`, `no-implied-eval`, and `no-new-func` reject direct or indirect dynamic string execution; `no-promise-executor-return` rejects ignored executor returns, and Vue setup props or refs cannot be used in ways that lose reactivity.
67
- - `unified-signatures` preserves public overloads with different parameter names or standalone JSDoc; the stricter core `no-void` replaces `no-meaningless-void-operator`.
68
- - `consistent-type-definitions`, `consistent-indexed-object-style`, `class-literal-property-style`, and `prefer-regexp-exec` are disabled as syntax-only preferences.
69
-
70
- ### Vue
71
-
72
- - Uses `flat/recommended`.
73
- - `attribute-hyphenation: ["error", "always"]`.
74
- - `no-v-html` warns.
75
- - `no-v-text-v-html-on-component` is an error.
76
- - `require-explicit-emits`, `attributes-order`, and `no-mutating-props` are errors.
77
- - `.vue` and `.nvue` share the TypeScript parser and Project Service.
78
-
79
- ## Type-aware requirements
80
-
81
- TypeScript, Vue, and React TSX always require type information. Linted files must belong to a discoverable `tsconfig.json`, or Project Service reports a configuration error.
82
-
83
- The `typeChecked` and `tsconfigRootDir` wrapper options no longer exist. Complex monorepos can override `languageOptions.parserOptions` in trailing Flat Config, but should not disable type checking to hide project-boundary problems.
29
+ | Domain | Preset or implementation |
30
+ | ---------- | -------------------------------------------------------------------------- |
31
+ | JavaScript | `@eslint/js` recommended plus local high-confidence rules |
32
+ | TypeScript | typescript-eslint `recommendedTypeChecked` plus Project Service |
33
+ | Vue | `eslint-plugin-vue` `flat/recommended` plus type-aware TypeScript |
34
+ | React | `@eslint-react` recommended/type-checked plus React Hooks Flat Recommended |
35
+ | Angular | Angular ESLint TypeScript, template, and accessibility recommended presets |
36
+ | JSON | `eslint-plugin-jsonc` recommended presets for three dialects |
37
+ | Import | `eslint-plugin-import-x` recommended plus a general ordering warning |
38
+ | RegExp | Explicit correctness, safety, and super-linear backtracking rules |
39
+ | Prettier | `eslint-config-prettier` conflict disabling |
40
+
41
+ The complete `strictTypeChecked`, `stylisticTypeChecked`, and mechanical `all` presets are not enabled. Extra rules are limited to low-false-positive checks that directly improve correctness or readability.
42
+
43
+ ## JavaScript and Import
44
+
45
+ - `camelcase: ["error", { properties: "never" }]` applies to variables and types while external protocol properties retain their original names.
46
+ - `no-empty` permits a completely empty `catch` and reports other empty blocks.
47
+ - `no-void` is an error; `void` is not used to hide a Promise or express an ignored return value.
48
+ - Real-risk rules such as `no-eval`, `no-implied-eval`, `no-new-func`, `no-promise-executor-return`, and `no-debugger` are errors.
49
+ - `curly` requires consistent braces only for multiline branches and does not force braces on every single-line branch.
50
+ - `import-x/first` and `import-x/no-duplicates` are errors.
51
+ - `import-x/order` is an error and fixes declaration order; common framework, tool, and `@/**` path groups extend the general groups.
52
+ - `pathGroupsExcludedImportTypes: ["type"]` keeps type imports in the final type group.
53
+ - Non-style side-effect imports participate in ordering. `style-imports-last` requires a final contiguous stylesheet group without sorting within it, preserving CSS cascade order.
54
+ - `sort-imports` is an error and only normalizes members inside one import declaration; `prefer-object-has-own` is not enabled.
55
+
56
+ ## TypeScript
57
+
58
+ TypeScript always uses Project Service and `recommendedTypeChecked`. Linted files must belong to a discoverable `tsconfig.json`.
59
+
60
+ - `.ts`, `.mts`, and `.cts` files are SDK-like module boundaries: `explicit-module-boundary-types` is an error, so exported functions and public boundaries of exported classes require explicit types.
61
+ - `.tsx` files are UI component files: type-aware correctness rules remain enabled, but module-boundary annotations are not required for inferable JSX return types.
62
+ - `explicit-function-return-type` is disabled, leaving internal functions, local handlers, and callbacks to TypeScript inference.
63
+ - `no-inferrable-types` preserves explicit parameter and property types and only removes annotations without contract value, such as obvious local-variable types.
64
+ - Vue/NVue SFCs and standalone TSX components disable module-boundary and function-return requirements to preserve common concise component forms.
65
+ - `no-floating-promises` is disabled because waiting, returning, or handling a Promise depends on business semantics.
66
+ - `require-await` is an error; an `async` function without `await` changes return and exception semantics and should lose `async`.
67
+ - `no-misused-promises`, `await-thenable`, unsafe-type rules, exhaustive switches, getter/setter compatibility, and other high-confidence checks are errors.
68
+ - `return-await` requires `await` only when needed for correct error handling.
69
+ - Standard non-null assertions are permitted, while contradictory optional-chain or nullish-coalescing combinations remain errors.
70
+ - `no-explicit-any` and `no-deprecated` warn.
71
+ - `no-unnecessary-condition`, `unified-signatures`, and syntax-only preferences are disabled to preserve runtime guards and public overloads.
72
+ - Template strings allow numbers and booleans; dynamic property deletion and static utility classes are not forcibly rewritten.
73
+ - `prefer-nullish-coalescing` does not force primitive values from `||` to `??`; `prefer-optional-chain` applies only when the type explicitly contains null or undefined.
74
+
75
+ ## Vue
76
+
77
+ - The official `flat/recommended` preset is the general Vue baseline.
78
+ - `no-v-html` warns so XSS risk is visible while sanitized content remains possible.
79
+ - `require-explicit-emits`, `no-dupe-keys`, `no-mutating-props`, `no-setup-props-reactivity-loss`, `no-ref-object-reactivity-loss`, and `no-reserved-component-names` are errors for Vue SFC and Vue JSX/TSX component scripts.
80
+ - `attribute-hyphenation`, `no-v-text-v-html-on-component`, and `attributes-order` remain limited to `.vue/.nvue` templates; `attributes-order` sorts definition, list-rendering, conditional, render-modifier, unique, global, ordinary, event, and content attributes.
81
+ - JSX attributes retain JavaScript camelCase conventions and do not inherit the template kebab-case rule.
82
+ - Setup props/ref reactivity loss and invalid custom-event names remain errors.
84
83
 
85
84
  ## UniApp boundary
86
85
 
87
- The root entry declares `uni`, `uniCloud`, page APIs, and conditional-platform objects, and permits comments in `pages.json` and `manifest.json`.
86
+ Only the UniApp entry declares `uni`, `uniCloud`, page APIs, and conditional-platform objects such as `wx`, `plus`, `my`, and `tt`. The plain Vue entry neither receives these globals nor ignores `unpackage`.
88
87
 
89
- ESLint does not execute conditional compilation, so `wx`, `plus`, and similar objects are visible in every code file handled by the root entry. This prevents `no-undef` inside platform branches but cannot verify that objects occur under the correct `#ifdef`. Plain Vue projects that do not want these globals should compose focused fragments instead of using the root entry.
88
+ ESLint does not execute conditional compilation. It can avoid `no-undef` inside platform branches but cannot prove that an object occurs under the correct `#ifdef`.
90
89
 
91
- ## Auto-fix risks
90
+ ## Manifest sorting and auto-fix
92
91
 
93
- Review these fixes carefully:
92
+ `package.json` and `tsconfig*.json` sorting is enabled by default in `createBaseConfigs()`, `vueConfig`, and `uniAppConfig`. The individual factories remain available for custom composition.
94
93
 
95
- - Import groups, type-source categories, member order, and side-effect import placement.
96
- - Separate TypeScript `import type` declarations.
97
- - Vue attribute naming and ordering.
98
- - `package.json` and `tsconfig*.json` key order.
94
+ Review these fixes carefully:
99
95
 
100
- `package.json` sorting does not enter conditional `exports` objects whose order has runtime meaning.
96
+ - Import groups, path groups, type-import placement, and non-style side-effect import order.
97
+ - Stylesheet imports are only checked for final placement and are not moved or reordered automatically.
98
+ - Separate TypeScript `import type` and `export type` declarations.
99
+ - Default manifest key ordering.
101
100
 
102
- Run a check first:
101
+ Check before fixing:
103
102
 
104
103
  ```sh
105
104
  pnpm exec eslint .
106
- ```
107
-
108
- Then apply fixes after reviewing the scope:
109
-
110
- ```sh
111
105
  pnpm exec eslint . --fix
112
106
  ```
113
107
 
114
108
  ## Maintenance policy
115
109
 
116
- 1. Rule comments explain current behavior, risk, and exceptions without referring to historical versions.
117
- 2. After upgrading recommended presets, inspect the final effective rules to prevent silent severity changes.
118
- 3. New frameworks add parsers, file scopes, and framework semantics without creating another language-rule tier.
119
- 4. New public factories, parsers, or auto-fix behavior require type and runtime tests.
110
+ 1. Inspect effective rules after recommended preset upgrades so upstream changes do not silently alter severity.
111
+ 2. Every new rule must explain the real problem it catches, its false-positive boundary, and whether it auto-fixes.
112
+ 3. Frameworks add parsers, file scopes, and framework semantics without creating another language strictness tier.
113
+ 4. Public entries, parser scopes, or auto-fix changes require matching type, runtime, and package tests.
@@ -1,119 +1,113 @@
1
1
  # 默认规则与风险指南
2
2
 
3
- 本文说明 2.1.7 当前配置模型、主要规则和迁移风险。规则源码注释只解释现行意图;历史差异统一记录在 `CHANGELOG.md`。
3
+ 本文说明当前配置模型、主要规则和自动修复风险。源码注释只解释现行意图;历史差异记录在 `CHANGELOG.md`。
4
+
5
+ ## 规则原则
6
+
7
+ 规则按以下顺序取舍:
8
+
9
+ 1. 符合 JavaScript、TypeScript 与 Vue 社区的通用标准写法,并以简洁、可读为前提。
10
+ 2. 发现真实 Bug 和类型安全问题。
11
+ 3. 保持跨项目代码一致性。
12
+ 4. 最后才采用 Fast 系列项目的风格偏好。
13
+
14
+ 纯粹为了 ESLint 而改变正常语义、增加样板代码或强制冷门写法的规则,不进入默认配置。
4
15
 
5
16
  ## 配置模型
6
17
 
7
- SDK、OA、Admin、Vue Web UniApp 客户端共享同一套 JavaScript、TypeScript、Import 和 RegExp 规则,不提供严格度档位。
18
+ Vue 3 UniApp 是根入口的两个独立具名完整配置:
8
19
 
9
- 根入口固定面向 Vue 3 + TypeScript + UniApp:
20
+ - `vueConfig` 仅处理普通 Vue 项目。
21
+ - `uniAppConfig` 在 Vue 能力之外增加 `.nvue`、UniApp globals、清单适配和 `unpackage` 忽略。
10
22
 
11
- - `environment: "browser"`
12
- - `.gitignore`
13
- - JavaScript、类型感知 TypeScript
14
- - Vue 3、`.nvue`、UniApp globals 与清单适配
15
- - Import、RegExp、JSON/JSONC/JSON5
16
- - `package.json` 和 `tsconfig*.json` 排序
17
- - Prettier 冲突关闭层
18
- - Node.js 工程文件 globals 与末尾规则覆写
23
+ 根入口不提供默认导出,公开 `vueConfig`、`uniAppConfig`、`createVueProjectConfigs()`、`createUniAppProjectConfigs()`、`createBaseConfigs()`、`defineRules()` 及相关类型。
19
24
 
20
- `fastConfig()` 只保留 `environment`。React、Angular、Markdown 和 Lodash 通过 `./configs` 子路径组合;规则、globals、ignores 和解析器特殊设置使用后置 Flat Config。
25
+ SDK、Node.js 和其他无框架项目使用 `createBaseConfigs()`。React、Angular、Markdown 和 Lodash `./configs` 显式组合;清单排序属于所有完整配置的基础能力。
21
26
 
22
27
  ## 预置来源
23
28
 
24
29
  | 领域 | 预置或实现 |
25
30
  | ---------- | ----------------------------------------------------------------------- |
26
- | JavaScript | `@eslint/js` recommended + 本地规则 |
27
- | TypeScript | typescript-eslint 严格与风格类型感知预置 + Project Service |
31
+ | JavaScript | `@eslint/js` recommended + 本地高置信度规则 |
32
+ | TypeScript | typescript-eslint `recommendedTypeChecked` + Project Service |
28
33
  | Vue | `eslint-plugin-vue` `flat/recommended` + 类型感知 TypeScript |
29
34
  | React | `@eslint-react` recommended/type-checked + React Hooks Flat Recommended |
30
35
  | Angular | Angular ESLint TypeScript、模板及无障碍 recommended |
31
36
  | JSON | `eslint-plugin-jsonc` 三种方言 recommended |
32
- | Import | `eslint-plugin-import-x` recommended + 固定顺序策略 |
37
+ | Import | `eslint-plugin-import-x` recommended + 通用顺序警告 |
33
38
  | RegExp | 显式正确性、安全和超线性回溯规则 |
34
39
  | Prettier | `eslint-config-prettier` 冲突关闭层 |
35
40
 
36
- ## 主要规则
37
-
38
- ### JavaScript
39
-
40
- - `camelcase: ["error", { properties: "never" }]`:变量和类型使用 camelCase,对象属性允许沿用外部协议字段名。
41
- - `no-empty` 允许明确忽略失败的空 catch,其他空代码块仍报错。
42
- - `no-eval` `no-void` `error`;`curly: ["error", "multi-line", "consistent"]`,已有的 `default` 分支必须位于最后。
43
- - `no-debugger: "error"`
44
- - `no-use-before-define` 为 `warn`;类和变量必须先声明,函数声明允许提升。
45
- - `prefer-arrow-callback`、`logical-assignment-operators`、`prefer-object-spread` 为 `error`。
46
- - `prefer-exponentiation-operator`、`prefer-object-has-own` `error`。
47
- - `sort-imports` `warn`,只排序同一 import 的成员。
48
- - `import-x/order` `error`,并设置 `warnOnUnassignedImports: true` 和 `sortTypesGroup: true`;`@/**` 归入 `internal`,类型导入位于所有非样式导入之后并在 `type` 总分组内按来源层级排序,样式导入不参与该规则。
49
- - `import-x/style-imports-last` `error`;CSS、SCSS、LESS 等样式必须形成最后一个连续导入分组,组内顺序保持不变且不自动修复。
50
-
51
- ### TypeScript
52
-
53
- - 始终启用 `strictTypeChecked`、`stylisticTypeChecked` `projectService: true`;TypeScript、TSX、Vue 与 NVue 统一使用 `extraFileExtensions: [".vue", ".nvue"]`,避免混合检查时 Project Service 重载项目。
54
- - `no-floating-promises` 与 `strict-void-return` 关闭,Promise 是否等待由业务语义决定;`no-void` 禁止用 `void promise` 规避检查。`no-misused-promises`、`await-thenable`、`require-await` 和 unsafe 类型规则保持严格,但 Vue 模板与 TSX 属性允许 Promise 返回的事件处理函数。
55
- - TypeScript TSX 的命名函数要求显式返回类型,内联回调和已有函数类型约束的表达式除外;模块导出边界仍要求显式类型。
56
- - Vue/NVue SFC 关闭函数返回类型与模块边界类型要求,并关闭形参和 catch 形参的未使用检查;普通未使用变量和导入仍报错。模板不会反向推断独立处理函数的参数类型,这类参数仍需显式标注。
57
- - `explicit-module-boundary-types` `error`,模块导出边界必须显式声明类型,不允许用显式 `any` 参数规避。
58
- - `no-explicit-any` 为 `warn`。
59
- - 普通 TS/TSX 的未使用参数和异常可用 `_` 前缀明确忽略;未使用普通变量不能用该前缀规避。
60
- - `no-empty-function` 仅允许空构造函数和空覆写方法。
61
- - `consistent-type-imports` 使用独立 `import type` 修复形式,`no-import-type-side-effects` 禁止仅含内联类型说明符的运行时导入。
62
- - `no-non-null-assertion` `error`。
63
- - `switch-exhaustiveness-check` `error`;`no-deprecated` 与 `no-unnecessary-condition` 为 `warn`。
64
- - 模板字符串允许数字插值,箭头简写允许直接返回 void;动态对象字段删除和纯静态工具类不强制改写。
65
- - `consistent-type-exports` `prefer-readonly` 为 `error`;原始类型不强制将 `||` 改成 `??`。
66
- - `no-eval`、`no-implied-eval` 与 `no-new-func` 禁止直接或间接动态执行字符串代码;`no-promise-executor-return` 禁止误用 Promise executor 返回值;Vue setup props 和 ref 禁止以丢失响应性的方式使用。
67
- - `unified-signatures` 保留参数名不同或具有独立 JSDoc 的公共重载;`no-meaningless-void-operator` 由更严格的核心 `no-void` 替代。
68
- - `consistent-type-definitions`、`consistent-indexed-object-style`、`class-literal-property-style`、`prefer-regexp-exec` 关闭,避免纯语法偏好阻断构建。
69
-
70
- ### Vue
71
-
72
- - 使用 `flat/recommended`。
73
- - `attribute-hyphenation: ["error", "always"]`。
74
- - `no-v-html` `warn`。
75
- - `no-v-text-v-html-on-component` `error`。
76
- - `require-explicit-emits`、`attributes-order`、`no-mutating-props` 为 `error`。
77
- - `.vue` `.nvue` 统一使用 TypeScript parser 和 Project Service。
78
-
79
- ## 类型感知要求
80
-
81
- TypeScript、Vue 和 React TSX 始终依赖类型信息。被检查文件必须属于可发现的 `tsconfig.json`,否则 Project Service 会报告配置错误。
82
-
83
- 本包不再提供 `typeChecked` 或 `tsconfigRootDir` 包装选项。复杂 monorepo 可以通过后置 Flat Config 直接覆盖 `languageOptions.parserOptions`,但不应通过关闭类型检查绕过项目边界问题。
41
+ 没有启用完整 `strictTypeChecked`、`stylisticTypeChecked` 或机械式 `all` 预置。额外规则只选择误报低、能直接改善正确性或可读性的部分。
42
+
43
+ ## JavaScript 与 Import
44
+
45
+ - `camelcase: ["error", { properties: "never" }]`:变量和类型使用 camelCase,外部协议属性可以保留原名。
46
+ - `no-empty` 允许完全空的 `catch`;其他空块仍报错。
47
+ - `no-void` 为错误,不使用 `void` 隐藏 Promise 或表达“忽略返回值”。
48
+ - `no-eval`、`no-implied-eval`、`no-new-func`、`no-promise-executor-return`、`no-debugger` 等真实风险规则为错误。
49
+ - `curly` 只要求多行条件分支使用一致花括号;不会强制所有单行分支加括号。
50
+ - `import-x/first` 和 `import-x/no-duplicates` 为错误。
51
+ - `import-x/order` 为错误,支持修复声明顺序;在通用分组之上保留常用框架、工具和 `@/**` pathGroups。
52
+ - `pathGroupsExcludedImportTypes: ["type"]` 保证类型导入统一留在最后的 type 总分组。
53
+ - 非样式副作用导入参与顺序检查;样式导入由 `style-imports-last` 强制形成最后一个连续分组,但组内不排序,避免改变 CSS 层叠顺序。
54
+ - `sort-imports` 为错误,只统一同一 import 声明内的成员顺序;`prefer-object-has-own` 不启用。
55
+
56
+ ## TypeScript
57
+
58
+ TypeScript 始终启用 Project Service `recommendedTypeChecked`。被检查文件必须属于可发现的 `tsconfig.json`。
59
+
60
+ - `.ts`、`.mts`、`.cts` 视为 SDK 式模块封装:`explicit-module-boundary-types` 为错误,导出函数及导出类的公共边界必须显式声明类型。
61
+ - `.tsx` 视为 UI 组件文件:保留类型感知正确性规则,但关闭模块边界类型强制,不要求补写可推断的 JSX 返回类型。
62
+ - `explicit-function-return-type` 关闭,内部函数、局部处理函数和回调使用 TypeScript 推断。
63
+ - `no-inferrable-types` 保留参数和属性上的显式类型,只清理局部变量等没有契约价值的冗余标注。
64
+ - Vue/NVue SFC 与独立 TSX 组件关闭模块边界和函数返回类型要求,保留常见组件写法。
65
+ - `no-floating-promises` 关闭;是否等待、返回或处理 Promise 由业务语义决定。
66
+ - `require-await` 为错误;没有 `await` `async` 会改变返回值和异常语义,应删除 `async`。
67
+ - `no-misused-promises`、`await-thenable`、unsafe 类型规则、穷尽 switch、getter/setter 类型一致性等高置信度检查为错误。
68
+ - `return-await` 只在错误处理正确性需要时要求 `await`。
69
+ - 标准非空断言允许使用;可选链后非空断言、空值合并前非空断言等矛盾组合仍报错。
70
+ - `no-explicit-any` `no-deprecated` 为警告。
71
+ - `no-unnecessary-condition`、`unified-signatures` 及纯语法偏好关闭,避免删除运行时防御或改写公共重载。
72
+ - 模板字符串允许 number boolean;动态字段删除和纯静态工具类不强制改写。
73
+ - `prefer-nullish-coalescing` 不强制原始类型从 `||` 改为 `??`;`prefer-optional-chain` 只在类型明确包含 null/undefined 时要求改写。
74
+
75
+ ## Vue
76
+
77
+ - 使用官方 `flat/recommended` 作为通用 Vue 基线。
78
+ - `no-v-html` 为警告,提醒审查 XSS 风险但允许已净化内容。
79
+ - `require-explicit-emits`、`no-dupe-keys`、`no-mutating-props`、`no-setup-props-reactivity-loss`、`no-ref-object-reactivity-loss` `no-reserved-component-names` 为错误,并应用于 Vue SFC 与 Vue JSX/TSX 的组件脚本。
80
+ - `attribute-hyphenation`、`no-v-text-v-html-on-component` `attributes-order` 仅应用于 `.vue/.nvue` 模板;`attributes-order` 按定义、循环、条件、渲染修饰、唯一属性、全局属性、普通属性、事件和内容排序。
81
+ - JSX 属性继续使用 JavaScript 的 camelCase 约定,不受模板 kebab-case 规则影响。
82
+ - setup props/ref 响应性丢失和自定义事件命名错误仍为错误。
84
83
 
85
84
  ## UniApp 边界
86
85
 
87
- 根入口默认声明 `uni`、`uniCloud`、页面 API 及条件编译平台对象,并允许 `pages.json`、`manifest.json` 中的注释。
86
+ 只有 UniApp 入口声明 `uni`、`uniCloud`、页面 API `wx`、`plus`、`my`、`tt` 等条件编译平台对象。普通 Vue 入口不会获得这些 globals,也不会忽略 `unpackage`。
88
87
 
89
- ESLint 不执行条件编译,因此 `wx`、`plus` 等对象在根入口处理的全部代码文件中可见。这避免平台分支中的 `no-undef`,但不能验证对象是否位于正确的 `#ifdef` 分支。普通 Vue 项目如不希望获得这些 globals,应直接组合所需配置片段,而不是使用根入口。
88
+ ESLint 不执行条件编译,所以能避免平台分支中的 `no-undef`,但不能验证对象是否出现在正确的 `#ifdef` 分支。
90
89
 
91
- ## 自动修复风险
90
+ ## 清单排序与自动修复
92
91
 
93
- 重点审查以下自动修复:
92
+ `package.json` 和 `tsconfig*.json` 排序属于 `createBaseConfigs()`、`vueConfig` 和 `uniAppConfig` 的默认配置。独立片段工厂仍可用于自定义组合。
94
93
 
95
- - Import 分组、类型来源层级、成员顺序和副作用导入位置。
96
- - TypeScript 独立 `import type` 声明。
97
- - Vue 属性命名与排序。
98
- - `package.json` 和 `tsconfig*.json` 字段顺序。
94
+ 重点审查以下自动修复:
99
95
 
100
- `package.json` 排序不会进入顺序具有运行时语义的条件 `exports` 对象。
96
+ - Import 分组、pathGroups、类型导入位置与非样式副作用导入顺序。
97
+ - 样式导入只检查是否位于最后,不自动移动或重排。
98
+ - TypeScript 独立 `import type` / `export type`。
99
+ - 默认启用的清单字段排序。
101
100
 
102
- 建议先运行:
101
+ 先检查,再修复:
103
102
 
104
103
  ```sh
105
104
  pnpm exec eslint .
106
- ```
107
-
108
- 确认问题范围后再运行:
109
-
110
- ```sh
111
105
  pnpm exec eslint . --fix
112
106
  ```
113
107
 
114
108
  ## 维护约定
115
109
 
116
- 1. 修改规则时解释当前行为、风险和例外,不在源码注释中引用历史版本。
117
- 2. 升级 recommended 预置后检查最终生效规则,避免上游变化静默改变严重级别。
118
- 3. 新增框架只增加解析器、文件范围和框架语义,不建立第二套语言规则档位。
119
- 4. 新增公共配置工厂、解析器或自动修复行为时同步增加类型和运行时测试。
110
+ 1. 升级 recommended 预置后检查最终生效配置,避免上游变化静默改变严重级别。
111
+ 2. 新规则必须说明它解决的真实问题、误报边界和是否自动修复。
112
+ 3. 框架只增加解析器、文件范围和框架语义,不建立第二套语言严格度。
113
+ 4. 公共入口、解析器范围或自动修复行为变化时同步更新类型、运行时和发布测试。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fast-china/eslint-config",
3
- "version": "2.1.7",
3
+ "version": "2.1.8",
4
4
  "description": "Practical, typed ESLint Flat Config for Vue 3, UniApp, React, Angular, Vite, TypeScript, JavaScript, and Node.js.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -72,6 +72,8 @@
72
72
  "dev": "eslint-config-inspector --config src/eslint-inspector.config.ts",
73
73
  "build": "pnpm run typegen:check && tsdown",
74
74
  "build:inspector": "eslint-config-inspector build --config src/eslint-inspector.config.ts",
75
+ "docs:rules": "tsx scripts/rules-docs.ts",
76
+ "docs:rules:check": "tsx scripts/rules-docs.ts --check",
75
77
  "typegen": "tsx scripts/typegen.ts",
76
78
  "typegen:check": "tsx scripts/typegen.ts --check",
77
79
  "typecheck": "tsc --noEmit",
@@ -83,7 +85,7 @@
83
85
  "test:types": "tsc --noEmit --project tests/tsconfig.json",
84
86
  "test:runtime": "node --test tests/config.test.mjs",
85
87
  "test:package": "node --test tests/package.test.mjs",
86
- "check": "pnpm run typecheck && pnpm run test && pnpm run lint && pnpm run format:check",
88
+ "check": "pnpm run typecheck && pnpm run docs:rules:check && pnpm run test && pnpm run lint && pnpm run format:check",
87
89
  "prepack": "pnpm run check"
88
90
  },
89
91
  "peerDependencies": {