@polyv/eslint-config 0.7.0 → 1.0.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.
- package/README.md +101 -52
- package/configs/common/README.md +87 -0
- package/configs/common/index.mjs +86 -0
- package/configs/importx/README.md +51 -0
- package/configs/importx/index.mjs +52 -0
- package/configs/js/README.md +34 -0
- package/configs/js/index.mjs +104 -0
- package/configs/miniprogram/README.md +50 -0
- package/configs/miniprogram/index.mjs +46 -0
- package/configs/prettier/README.md +53 -0
- package/configs/prettier/index.mjs +17 -0
- package/configs/sonarjs/README.md +48 -0
- package/configs/sonarjs/index.mjs +92 -0
- package/configs/ts/README.md +43 -0
- package/configs/ts/index.mjs +148 -0
- package/configs/vue2/README.md +87 -0
- package/configs/vue2/index.mjs +96 -0
- package/index.mjs +29 -0
- package/package.json +58 -26
- package/plugin.mjs +17 -0
- package/prettier-config.mjs +15 -0
- package/rules/explicit-module-boundary-types/README.md +126 -0
- package/rules/explicit-module-boundary-types/index.mjs +171 -0
- package/rules/no-relative-directory-index-imports/README.md +87 -0
- package/rules/no-relative-directory-index-imports/index.mjs +142 -0
- package/rules/no-vue-component-variable-name-conflict/README.md +85 -0
- package/rules/no-vue-component-variable-name-conflict/index.mjs +262 -0
- package/utils.mjs +3 -0
- package/.editorconfig +0 -5
- package/.eslintrc.js +0 -3
- package/.gitattributes +0 -12
- package/.nvmrc +0 -1
- package/lib/for-js.js +0 -84
- package/lib/for-ts.js +0 -88
- package/lib/for-vue2-js.js +0 -45
- package/lib/util.js +0 -7
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const sourceFilePattern = '**/*.{js,cjs,mjs,jsx,ts,tsx,mts,cts}';
|
|
2
|
+
|
|
3
|
+
export const unitTestFilePatterns = [
|
|
4
|
+
'**/__tests__/**/*.{js,cjs,mjs,jsx,ts,tsx,mts,cts}',
|
|
5
|
+
'**/__test__/**/*.{js,cjs,mjs,jsx,ts,tsx,mts,cts}',
|
|
6
|
+
'**/tests/**/*.{js,cjs,mjs,jsx,ts,tsx,mts,cts}',
|
|
7
|
+
'**/test/**/*.{js,cjs,mjs,jsx,ts,tsx,mts,cts}',
|
|
8
|
+
'**/*.test.{js,cjs,mjs,jsx,ts,tsx,mts,cts}',
|
|
9
|
+
'**/*.spec.{js,cjs,mjs,jsx,ts,tsx,mts,cts}'
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export const miniprogramGlobals = {
|
|
13
|
+
App: 'readonly',
|
|
14
|
+
Component: 'readonly',
|
|
15
|
+
Page: 'readonly',
|
|
16
|
+
getApp: 'readonly',
|
|
17
|
+
wx: 'readonly'
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default [
|
|
21
|
+
{
|
|
22
|
+
ignores: [
|
|
23
|
+
'miniprogram_npm/**',
|
|
24
|
+
'**/miniprogram_npm/**'
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
files: [sourceFilePattern],
|
|
29
|
+
languageOptions: {
|
|
30
|
+
globals: miniprogramGlobals
|
|
31
|
+
},
|
|
32
|
+
rules: {
|
|
33
|
+
// 小程序 npm 构建需要显式 index,引入该配置时关闭会建议移除 index 的 import 规则。
|
|
34
|
+
'import-x/no-useless-path-segments': 'off',
|
|
35
|
+
// 禁止依赖相对目录的 index 兜底解析,避免小程序 npm 构建后路径解析失败。
|
|
36
|
+
'polyv/no-relative-directory-index-imports': 'error'
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
files: unitTestFilePatterns,
|
|
41
|
+
rules: {
|
|
42
|
+
// 单元测试文件不进入小程序 npm 产物,允许使用目录 index 简写。
|
|
43
|
+
'polyv/no-relative-directory-index-imports': 'off'
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Prettier 配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.prettier` 用于接入 `eslint-plugin-prettier/recommended`,并内置 Polyv 共享 Prettier 选项。
|
|
4
|
+
|
|
5
|
+
## 使用方式
|
|
6
|
+
|
|
7
|
+
Prettier 配置应放在配置数组最后,确保它能关闭前面配置中和 Prettier 冲突的格式类规则:
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
// eslint.config.mjs
|
|
11
|
+
import polyv from '@polyv/eslint-config';
|
|
12
|
+
|
|
13
|
+
export default [
|
|
14
|
+
...polyv.configs.common,
|
|
15
|
+
...polyv.configs.js,
|
|
16
|
+
...polyv.configs.prettier
|
|
17
|
+
];
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
也可以通过子路径单独引入:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
// eslint.config.mjs
|
|
24
|
+
import prettier from '@polyv/eslint-config/configs/prettier';
|
|
25
|
+
|
|
26
|
+
export default [
|
|
27
|
+
...prettier
|
|
28
|
+
];
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 配置内容
|
|
32
|
+
|
|
33
|
+
- 复用根级 [prettier-config.mjs](../../prettier-config.mjs) 中的共享 Prettier 配置。
|
|
34
|
+
- 引入 `eslint-plugin-prettier/recommended`。
|
|
35
|
+
- 注册 `prettier` plugin。
|
|
36
|
+
- 开启 `prettier/prettier` 规则,并设置 `usePrettierrc: false`,避免消费项目必须单独维护 `prettier.config.mjs`。
|
|
37
|
+
- 继承 `eslint-config-prettier`,关闭和 Prettier 冲突的格式类 ESLint 规则。
|
|
38
|
+
- 内置共享 Prettier 选项:`useTabs: false`、`printWidth: 120`、`tabWidth: 2`、`singleQuote: true`、`semi: true`、`trailingComma: 'all'`、`bracketSameLine: false`、`arrowParens: 'avoid'`、`quoteProps: 'as-needed'`、`singleAttributePerLine: true`。
|
|
39
|
+
|
|
40
|
+
## 注意事项
|
|
41
|
+
|
|
42
|
+
`configs.prettier` 不内联引入 `common`。接入完整预设时仍应显式组合 `polyv.configs.common`,并把 `polyv.configs.prettier` 放在最后。
|
|
43
|
+
|
|
44
|
+
如果消费项目需要覆盖共享格式选项,可以在 `polyv.configs.prettier` 后追加自己的 `prettier/prettier` 规则配置。
|
|
45
|
+
|
|
46
|
+
如果消费项目需要单独给 Prettier CLI 或编辑器使用共享配置,可以在项目里的 `prettier.config.mjs` 中写:
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
// prettier.config.mjs
|
|
50
|
+
import prettierConfig from '@polyv/eslint-config/prettier-config';
|
|
51
|
+
|
|
52
|
+
export default prettierConfig;
|
|
53
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
|
|
2
|
+
import prettierConfig from '../../prettier-config.mjs';
|
|
3
|
+
|
|
4
|
+
export const prettierOptions = prettierConfig;
|
|
5
|
+
|
|
6
|
+
export default [
|
|
7
|
+
// 启用 prettier 推荐配置,注册 prettier 插件并关闭与 prettier 冲突的格式类规则。
|
|
8
|
+
eslintPluginPrettierRecommended,
|
|
9
|
+
{
|
|
10
|
+
rules: {
|
|
11
|
+
// 复用共享 Prettier 选项,避免消费项目单独维护 prettier.config.mjs。
|
|
12
|
+
'prettier/prettier': ['error', prettierOptions, {
|
|
13
|
+
usePrettierrc: false
|
|
14
|
+
}]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# SonarJS 配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.sonarjs` 用于提供 `eslint-plugin-sonarjs` 的推荐规则和项目内覆盖规则。
|
|
4
|
+
|
|
5
|
+
## 使用方式
|
|
6
|
+
|
|
7
|
+
通常不需要单独引入该配置,`polyv.configs.common` 已经继承了 `polyv.configs.sonarjs`:
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
// eslint.config.mjs
|
|
11
|
+
import polyv from '@polyv/eslint-config';
|
|
12
|
+
|
|
13
|
+
export default [
|
|
14
|
+
...polyv.configs.common
|
|
15
|
+
];
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
如果只需要 SonarJS 能力,也可以单独组合:
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
// eslint.config.mjs
|
|
22
|
+
import polyv from '@polyv/eslint-config';
|
|
23
|
+
|
|
24
|
+
export default [
|
|
25
|
+
...polyv.configs.sonarjs
|
|
26
|
+
];
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
也可以通过子路径单独引入:
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
// eslint.config.mjs
|
|
33
|
+
import sonarjs from '@polyv/eslint-config/configs/sonarjs';
|
|
34
|
+
|
|
35
|
+
export default [
|
|
36
|
+
...sonarjs
|
|
37
|
+
];
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 配置内容
|
|
41
|
+
|
|
42
|
+
- 启用 `eslint-plugin-sonarjs` 的推荐配置。
|
|
43
|
+
- 集中维护复杂度、重复分支、重复字符串、无意义表达式、冗余跳转、嵌套 switch、未使用集合等 `sonarjs/*` 规则。
|
|
44
|
+
- 关闭部分容易和历史代码、示例配置、SDK 兼容代码产生误报或风格冲突的规则,例如 `sonarjs/elseif-without-else`、`sonarjs/no-clear-text-protocols`、`sonarjs/no-duplicate-string`、`sonarjs/no-hardcoded-ip`、`sonarjs/pseudo-random`、`sonarjs/redundant-type-aliases`、`sonarjs/slow-regex` 和 `sonarjs/todo-tag`。
|
|
45
|
+
|
|
46
|
+
## 注意事项
|
|
47
|
+
|
|
48
|
+
`configs.sonarjs` 不内联引入 `common`。接入完整预设时仍应显式组合 `polyv.configs.common`。
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import sonarjsPlugin from 'eslint-plugin-sonarjs';
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
// 启用 sonarjs 推荐规则,检查复杂度、重复代码和常见逻辑问题。
|
|
5
|
+
sonarjsPlugin.configs.recommended,
|
|
6
|
+
{
|
|
7
|
+
rules: {
|
|
8
|
+
// 限制函数认知复杂度,避免过多分支和嵌套。
|
|
9
|
+
'sonarjs/cognitive-complexity': ['error', 20],
|
|
10
|
+
// 关闭 elseif 必须带 else 的限制。
|
|
11
|
+
'sonarjs/elseif-without-else': 'off',
|
|
12
|
+
// 限制 switch 分支数量。
|
|
13
|
+
'sonarjs/max-switch-cases': 'error',
|
|
14
|
+
// 禁止所有条件分支代码完全相同。
|
|
15
|
+
'sonarjs/no-all-duplicated-branches': 'error',
|
|
16
|
+
// 禁止可以合并的嵌套 if。
|
|
17
|
+
'sonarjs/no-collapsible-if': 'error',
|
|
18
|
+
// 关闭注释代码检查,避免示例代码或兼容性注释误报。
|
|
19
|
+
'sonarjs/no-commented-code': 'off',
|
|
20
|
+
// 关闭明文协议检查,允许历史配置或示例中保留 http 等协议字符串。
|
|
21
|
+
'sonarjs/no-clear-text-protocols': 'off',
|
|
22
|
+
// 关闭集合长度误判检查,避免现有代码误报。
|
|
23
|
+
'sonarjs/no-collection-size-mischeck': 'off',
|
|
24
|
+
// 关闭重复字符串检查,避免业务常量、文案和接口路径产生大量误报。
|
|
25
|
+
'sonarjs/no-duplicate-string': 'off',
|
|
26
|
+
// 禁止不同分支出现重复代码块。
|
|
27
|
+
'sonarjs/no-duplicated-branches': 'error',
|
|
28
|
+
// 禁止刚赋值的元素被立即覆盖。
|
|
29
|
+
'sonarjs/no-element-overwrite': 'error',
|
|
30
|
+
// 禁止对明显为空的集合做无意义操作。
|
|
31
|
+
'sonarjs/no-empty-collection': 'error',
|
|
32
|
+
// 禁止调用函数时传入多余参数。
|
|
33
|
+
'sonarjs/no-extra-arguments': 'error',
|
|
34
|
+
// 禁止无意义或永远同值的表达式。
|
|
35
|
+
'sonarjs/no-gratuitous-expressions': 'error',
|
|
36
|
+
// 关闭硬编码 IP 检查,允许本地开发、内网地址或示例配置。
|
|
37
|
+
'sonarjs/no-hardcoded-ip': 'off',
|
|
38
|
+
// 禁止多个条件分支使用相同判断。
|
|
39
|
+
'sonarjs/no-identical-conditions': 'error',
|
|
40
|
+
// 禁止表达式两边完全相同。
|
|
41
|
+
'sonarjs/no-identical-expressions': 'error',
|
|
42
|
+
// 禁止多个函数实现完全相同。
|
|
43
|
+
'sonarjs/no-identical-functions': 'error',
|
|
44
|
+
// 禁止忽略应该使用的返回值。
|
|
45
|
+
'sonarjs/no-ignored-return': 'error',
|
|
46
|
+
// 关闭忽略异常检查,允许业务代码显式吞掉可忽略异常。
|
|
47
|
+
'sonarjs/no-ignored-exceptions': 'off',
|
|
48
|
+
// 关闭不变返回值检查,避免简单分支适配代码误报。
|
|
49
|
+
'sonarjs/no-invariant-returns': 'off',
|
|
50
|
+
// 关闭反向布尔判断检查。
|
|
51
|
+
'sonarjs/no-inverted-boolean-check': 'off',
|
|
52
|
+
// 禁止嵌套 switch。
|
|
53
|
+
'sonarjs/no-nested-switch': 'error',
|
|
54
|
+
// 禁止嵌套模板字符串。
|
|
55
|
+
'sonarjs/no-nested-template-literals': 'error',
|
|
56
|
+
// 禁止冗余布尔表达式。
|
|
57
|
+
'sonarjs/no-redundant-boolean': 'error',
|
|
58
|
+
// 禁止多余的 return、break 或 continue。
|
|
59
|
+
'sonarjs/no-redundant-jump': 'error',
|
|
60
|
+
// 关闭冗余类型别名检查,避免历史类型抽象或导出别名误报。
|
|
61
|
+
'sonarjs/redundant-type-aliases': 'off',
|
|
62
|
+
// 禁止同一行写复杂条件逻辑。
|
|
63
|
+
'sonarjs/no-same-line-conditional': 'error',
|
|
64
|
+
// 禁止过小的 switch。
|
|
65
|
+
'sonarjs/no-small-switch': 'error',
|
|
66
|
+
// 禁止创建或修改后没有实际使用的集合。
|
|
67
|
+
'sonarjs/no-unused-collection': 'error',
|
|
68
|
+
// 禁止使用没有返回值的函数调用结果。
|
|
69
|
+
'sonarjs/no-use-of-empty-return-value': 'error',
|
|
70
|
+
// 禁止只重新抛出异常的无意义 catch。
|
|
71
|
+
'sonarjs/no-useless-catch': 'error',
|
|
72
|
+
// 关闭从 PATH 查找系统命令的检查,避免 Node 工具脚本场景误报。
|
|
73
|
+
'sonarjs/no-os-command-from-path': 'off',
|
|
74
|
+
// 检查疑似写错的操作符组合。
|
|
75
|
+
'sonarjs/non-existent-operator': 'error',
|
|
76
|
+
// 建议直接返回表达式而不是先赋值再返回。
|
|
77
|
+
'sonarjs/prefer-immediate-return': 'error',
|
|
78
|
+
// 建议使用对象字面量。
|
|
79
|
+
'sonarjs/prefer-object-literal': 'error',
|
|
80
|
+
// 关闭单一布尔返回简化建议。
|
|
81
|
+
'sonarjs/prefer-single-boolean-return': 'off',
|
|
82
|
+
// 关闭伪随机数检查,允许非安全场景使用 Math.random 等实现。
|
|
83
|
+
'sonarjs/pseudo-random': 'off',
|
|
84
|
+
// 关闭慢正则检查,避免复杂兼容正则误报。
|
|
85
|
+
'sonarjs/slow-regex': 'off',
|
|
86
|
+
// 关闭待办标签检查,允许历史注释或临时事项保留。
|
|
87
|
+
'sonarjs/todo-tag': 'off',
|
|
88
|
+
// for 循环没有初始化或更新逻辑时建议改成 while。
|
|
89
|
+
'sonarjs/prefer-while': 'error'
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
];
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# TypeScript 配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.ts` 用于 TypeScript 项目或 TypeScript + JavaScript 混合项目。它已经集成 `polyv.configs.js`,因此混合项目不需要再额外引入 `polyv.configs.js`。
|
|
4
|
+
|
|
5
|
+
## 使用方式
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
// eslint.config.mjs
|
|
9
|
+
import polyv from '@polyv/eslint-config';
|
|
10
|
+
|
|
11
|
+
export default [
|
|
12
|
+
...polyv.configs.common,
|
|
13
|
+
...polyv.configs.ts
|
|
14
|
+
];
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 配置内容
|
|
18
|
+
|
|
19
|
+
- 集成 `polyv.configs.js`,覆盖 JavaScript 文件。
|
|
20
|
+
- 启用 `typescript-eslint` 的 `tseslint.configs.recommended`。
|
|
21
|
+
- 复用 `importX.flatConfigs.typescript` 的 TypeScript 处理,补充 TS 扩展名、`@typescript-eslint/parser` 映射和 TypeScript resolver,并关闭 TS 场景下容易误报的 `import-x/named`。
|
|
22
|
+
- 配置 TypeScript 文件范围:`**/*.{ts,tsx}`。
|
|
23
|
+
- 注入 browser 和 node 全局变量。
|
|
24
|
+
- 使用 `@stylistic` 承接 ESLint 10 生态中已经移除的 TypeScript 格式类规则,例如分号、缩进、逗号空格、尾逗号、函数括号前空格和类型注解空格。
|
|
25
|
+
- 迁移 ESLint 8 `for-ts.js` 中仍适用的 TypeScript 规则。
|
|
26
|
+
- 不使用 `STRICT_LINT` 分支,相关规则统一按当前配置写死。
|
|
27
|
+
|
|
28
|
+
## 关键规则
|
|
29
|
+
|
|
30
|
+
- `@typescript-eslint/no-unused-vars`:未使用变量给出 `warn`,检查全部变量,允许 `_` 前缀变量和参数,允许对象 rest 兄弟属性。
|
|
31
|
+
- `@typescript-eslint/no-explicit-any`:关闭 `any` 限制,兼容历史代码。
|
|
32
|
+
- `@typescript-eslint/no-empty-object-type`:关闭空对象类型限制,兼容历史 `{}` 类型写法。
|
|
33
|
+
- `@typescript-eslint/no-namespace`:关闭 namespace 限制,兼容历史声明合并或 SDK 类型组织方式。
|
|
34
|
+
- `@typescript-eslint/no-unused-expressions`:禁止无用表达式,但允许短路表达式和三元表达式承载调用逻辑。
|
|
35
|
+
- `polyv/explicit-module-boundary-types`:基于 `@typescript-eslint/explicit-module-boundary-types` 封装,按环境使用 `devWarnProdError`,生产环境为 `error`,其他环境为 `warn`;默认要求导出函数声明返回类型,具体场景可通过 settings 放开特定命名的返回类型推导。
|
|
36
|
+
- `no-empty`:禁止空代码块,但允许空 `catch`。
|
|
37
|
+
- `no-useless-escape`:关闭无意义转义检查,兼容历史字符串和正则写法。
|
|
38
|
+
|
|
39
|
+
## 注意事项
|
|
40
|
+
|
|
41
|
+
- `configs.ts` 不注册 `polyv`、`import-x`、`promise` 或 `sonarjs` 插件。
|
|
42
|
+
- 接入时必须显式组合 `polyv.configs.common`。
|
|
43
|
+
- Vue2 项目需要允许 `useXxx`、`xxxProp(s)` / `xxxEmit(s)` 省略返回类型时,在 `configs.ts` 后面继续组合 `polyv.configs.vue2`。
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import stylistic from '@stylistic/eslint-plugin';
|
|
3
|
+
import { importX } from 'eslint-plugin-import-x';
|
|
4
|
+
import tseslint from 'typescript-eslint';
|
|
5
|
+
import jsConfig from '../js/index.mjs';
|
|
6
|
+
import { devWarnProdError } from '../../utils.mjs';
|
|
7
|
+
|
|
8
|
+
const tsFiles = ['**/*.{ts,tsx}'];
|
|
9
|
+
const importXTypeScriptConfig = {
|
|
10
|
+
name: importX.flatConfigs.typescript.name,
|
|
11
|
+
settings: importX.flatConfigs.typescript.settings,
|
|
12
|
+
rules: importX.flatConfigs.typescript.rules
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default [
|
|
16
|
+
...jsConfig,
|
|
17
|
+
...tseslint.configs.recommended,
|
|
18
|
+
// 启用 import-x 的 TypeScript 配置,补充 TS parser、TS 扩展名和 TypeScript resolver 设置。
|
|
19
|
+
importXTypeScriptConfig,
|
|
20
|
+
{
|
|
21
|
+
files: tsFiles,
|
|
22
|
+
languageOptions: {
|
|
23
|
+
ecmaVersion: 'latest',
|
|
24
|
+
sourceType: 'module',
|
|
25
|
+
globals: {
|
|
26
|
+
...globals.browser,
|
|
27
|
+
...globals.node
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
plugins: {
|
|
31
|
+
'@stylistic': stylistic
|
|
32
|
+
},
|
|
33
|
+
rules: {
|
|
34
|
+
// 关闭核心分号规则,交给 @stylistic 版本处理。
|
|
35
|
+
semi: 'off',
|
|
36
|
+
// 要求语句结尾必须使用分号。
|
|
37
|
+
'@stylistic/semi': ['error', 'always'],
|
|
38
|
+
|
|
39
|
+
// 关闭核心缩进规则,交给 @stylistic 版本处理。
|
|
40
|
+
indent: 'off',
|
|
41
|
+
// 要求使用 2 个空格缩进,switch case 多缩进一级。
|
|
42
|
+
'@stylistic/indent': ['error', 2, {
|
|
43
|
+
SwitchCase: 1
|
|
44
|
+
}],
|
|
45
|
+
|
|
46
|
+
// 关闭核心逗号空格规则,交给 @stylistic 版本处理。
|
|
47
|
+
'comma-spacing': 'off',
|
|
48
|
+
// 要求逗号后有空格、逗号前没有空格。
|
|
49
|
+
'@stylistic/comma-spacing': ['error'],
|
|
50
|
+
|
|
51
|
+
// 关闭核心尾逗号规则,交给 @stylistic 版本处理。
|
|
52
|
+
'comma-dangle': 'off',
|
|
53
|
+
// 只允许多行结构使用尾逗号,单行结构不允许。
|
|
54
|
+
'@stylistic/comma-dangle': ['error', 'only-multiline'],
|
|
55
|
+
|
|
56
|
+
// 关闭核心使用前定义规则,交给 TypeScript 版本处理。
|
|
57
|
+
'no-use-before-define': 'off',
|
|
58
|
+
// 禁止变量在定义前使用,但允许函数和类提前使用。
|
|
59
|
+
'@typescript-eslint/no-use-before-define': ['error', {
|
|
60
|
+
functions: false,
|
|
61
|
+
classes: false,
|
|
62
|
+
variables: true
|
|
63
|
+
}],
|
|
64
|
+
|
|
65
|
+
// 关闭核心未使用变量规则,交给 TypeScript 版本处理。
|
|
66
|
+
'no-unused-vars': 'off',
|
|
67
|
+
// 未使用变量给出警告,检查全部变量,允许对象 rest 兄弟属性和 _ 前缀变量不触发告警。
|
|
68
|
+
'@typescript-eslint/no-unused-vars': ['warn', {
|
|
69
|
+
vars: 'all',
|
|
70
|
+
args: 'after-used',
|
|
71
|
+
argsIgnorePattern: '^_',
|
|
72
|
+
ignoreRestSiblings: true,
|
|
73
|
+
caughtErrors: 'none',
|
|
74
|
+
varsIgnorePattern: '^_'
|
|
75
|
+
}],
|
|
76
|
+
|
|
77
|
+
// 关闭核心循环内函数规则,交给 TypeScript 版本处理。
|
|
78
|
+
'no-loop-func': 'off',
|
|
79
|
+
// 禁止在循环中定义可能捕获循环变量的函数。
|
|
80
|
+
'@typescript-eslint/no-loop-func': 'error',
|
|
81
|
+
|
|
82
|
+
// 关闭核心函数括号前空格规则,交给 @stylistic 版本处理。
|
|
83
|
+
'space-before-function-paren': 'off',
|
|
84
|
+
// 约束函数括号前空格:普通函数不留空格,async 箭头函数留空格。
|
|
85
|
+
'@stylistic/space-before-function-paren': ['error', {
|
|
86
|
+
anonymous: 'never',
|
|
87
|
+
named: 'never',
|
|
88
|
+
asyncArrow: 'always'
|
|
89
|
+
}],
|
|
90
|
+
|
|
91
|
+
// 允许使用 any。
|
|
92
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
93
|
+
// 允许使用空对象类型,兼容历史类型声明中的 {} 写法。
|
|
94
|
+
'@typescript-eslint/no-empty-object-type': 'off',
|
|
95
|
+
// 允许使用 Function 类型,兼容历史函数类型声明。
|
|
96
|
+
'@typescript-eslint/no-unsafe-function-type': 'off',
|
|
97
|
+
// 允许使用 String、Number、Boolean、Object 等包装对象类型,兼容历史类型声明。
|
|
98
|
+
'@typescript-eslint/no-wrapper-object-types': 'off',
|
|
99
|
+
// 允许空函数。
|
|
100
|
+
'@typescript-eslint/no-empty-function': 'off',
|
|
101
|
+
// 允许空接口。
|
|
102
|
+
'@typescript-eslint/no-empty-interface': 'off',
|
|
103
|
+
// 允许 TypeScript namespace,兼容历史声明合并或 SDK 类型组织方式。
|
|
104
|
+
'@typescript-eslint/no-namespace': 'off',
|
|
105
|
+
// 禁止无用表达式,但允许短路和三元表达式承载调用逻辑。
|
|
106
|
+
'@typescript-eslint/no-unused-expressions': ['error', {
|
|
107
|
+
allowShortCircuit: true,
|
|
108
|
+
allowTernary: true
|
|
109
|
+
}],
|
|
110
|
+
// 要求类型注解冒号前无空格、冒号后有空格。
|
|
111
|
+
'@stylistic/type-annotation-spacing': ['error', {
|
|
112
|
+
after: true,
|
|
113
|
+
before: false
|
|
114
|
+
}],
|
|
115
|
+
// 要求箭头前后都有空格。
|
|
116
|
+
'@stylistic/arrow-spacing': ['error', {
|
|
117
|
+
before: true,
|
|
118
|
+
after: true
|
|
119
|
+
}],
|
|
120
|
+
// 要求仅作为类型使用的导入使用 import type,保持类型和值导入边界清晰。
|
|
121
|
+
'@typescript-eslint/consistent-type-imports': 'error',
|
|
122
|
+
// 关闭原模块边界类型规则,交给 polyv 包装版本读取 settings 并处理返回类型推导。
|
|
123
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
124
|
+
// 要求模块边界显式声明类型;具体场景可通过 settings 放开特定命名的返回类型推导。
|
|
125
|
+
'polyv/explicit-module-boundary-types': devWarnProdError,
|
|
126
|
+
// 要求 enum 名称和 enum 成员名称使用 PascalCase。
|
|
127
|
+
'@typescript-eslint/naming-convention': ['error', {
|
|
128
|
+
selector: 'enumMember',
|
|
129
|
+
format: ['PascalCase', 'UPPER_CASE']
|
|
130
|
+
}, {
|
|
131
|
+
selector: 'enum',
|
|
132
|
+
format: ['PascalCase']
|
|
133
|
+
}],
|
|
134
|
+
// 允许使用 CommonJS require。
|
|
135
|
+
'@typescript-eslint/no-var-requires': 'off',
|
|
136
|
+
// 禁止对数组使用 for-in,避免遍历到索引或继承属性。
|
|
137
|
+
'@typescript-eslint/no-for-in-array': 'off',
|
|
138
|
+
// 禁止空代码块,但允许空 catch 用于吞掉可忽略异常。
|
|
139
|
+
'no-empty': ['error', {
|
|
140
|
+
allowEmptyCatch: true
|
|
141
|
+
}],
|
|
142
|
+
// 允许历史字符串或正则里保留冗余转义。
|
|
143
|
+
'no-useless-escape': 'off',
|
|
144
|
+
// 允许变量赋值后未再被读取,避免历史代码中的中间赋值触发新规则。
|
|
145
|
+
'no-useless-assignment': 'off',
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
];
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Vue2 配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.vue2` 用于 Vue2 项目中的补充规则配置,包含 Vue2 SFC 解析、Vue2 essential 规则、从 ESLint 8 `for-vue2-js.js` 迁移的 Vue 规则,以及 Vue2 TypeScript 场景的返回类型推导 settings。
|
|
4
|
+
|
|
5
|
+
## 使用方式
|
|
6
|
+
|
|
7
|
+
Vue2 JavaScript 项目放在 `polyv.configs.js` 后面:
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
// eslint.config.mjs
|
|
11
|
+
import polyv from '@polyv/eslint-config';
|
|
12
|
+
|
|
13
|
+
export default [
|
|
14
|
+
...polyv.configs.common,
|
|
15
|
+
...polyv.configs.js,
|
|
16
|
+
...polyv.configs.vue2
|
|
17
|
+
];
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Vue2 TypeScript 项目放在 `polyv.configs.ts` 后面:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
// eslint.config.mjs
|
|
24
|
+
import polyv from '@polyv/eslint-config';
|
|
25
|
+
|
|
26
|
+
export default [
|
|
27
|
+
...polyv.configs.common,
|
|
28
|
+
...polyv.configs.ts,
|
|
29
|
+
...polyv.configs.vue2
|
|
30
|
+
];
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 配置内容
|
|
34
|
+
|
|
35
|
+
- 启用 `eslint-plugin-vue` 的 `flat/vue2-essential`,承接 ESLint 8 的 `plugin:vue/essential`。
|
|
36
|
+
- 为 `.vue` 文件配置 `vue-eslint-parser` 和 `typescript-eslint` parser,支持普通 `<script>`、`<script lang="ts">` 和 `<script setup lang="ts">`。
|
|
37
|
+
- 为 `.vue` 文件注入 browser、node 和 jest 全局变量。
|
|
38
|
+
- 从 ESLint 8 `for-vue2-js.js` 和 live-watch-sdk 本地 `.eslintrc.js` 迁移 Vue 规则,包括模板缩进、自闭合标签、属性换行、自定义事件命名、props 修改限制、组件名限制、attribute 连字符命名、v-on 事件连字符命名、模板组件名大小写和 style 属性限制。
|
|
39
|
+
- 提供 `settings['polyv/explicit-module-boundary-types']`,不覆盖 `polyv.configs.ts` 中的规则配置。
|
|
40
|
+
- 通过 settings 允许 `useXxx` hook 函数省略返回类型。
|
|
41
|
+
- 通过 settings 允许 `xxxProp`、`xxxProps`、`xxxEmit`、`xxxEmits` 这类 Vue2 props/emits 工厂方法省略返回类型。
|
|
42
|
+
- 仍然要求普通导出函数声明返回类型。
|
|
43
|
+
- 仍然检查参数类型,不会因为函数名匹配就放开参数类型。
|
|
44
|
+
- 启用 `polyv/no-vue-component-variable-name-conflict`,避免 `<script setup>` 中组件导入名和 lower camel 本地声明冲突。
|
|
45
|
+
|
|
46
|
+
## 关键规则
|
|
47
|
+
|
|
48
|
+
- `vue/html-indent`:要求 template 使用 2 个空格缩进。
|
|
49
|
+
- `vue/html-self-closing`:要求 void HTML 标签和组件使用自闭合,普通 HTML 标签不使用自闭合。
|
|
50
|
+
- `vue/no-v-html`:关闭 `v-html` 限制,兼容历史富文本渲染场景。
|
|
51
|
+
- `vue/max-attributes-per-line`:单行元素最多 3 个属性,多行元素每行 1 个属性。
|
|
52
|
+
- `vue/custom-event-name-casing`:要求自定义事件名称使用 `kebab-case`。
|
|
53
|
+
- `vue/no-mutating-props`:禁止直接修改 props。
|
|
54
|
+
- `vue/multi-word-component-names`:要求组件名使用多个单词,但允许 `index`、`Index`、`default`、`Default`。
|
|
55
|
+
- `vue/attribute-hyphenation`:要求模板中的 attribute 使用连字符命名。
|
|
56
|
+
- `vue/v-on-event-hyphenation`:要求 `v-on` 监听的自定义事件使用连字符命名。
|
|
57
|
+
- `vue/component-name-in-template-casing`:要求模板中已注册组件使用 `kebab-case` 标签名。
|
|
58
|
+
- `vue/enforce-style-attribute`:约束 `<style>` 标签属性,允许普通 `<style>`,违规时给出警告。
|
|
59
|
+
- `polyv/no-vue-component-variable-name-conflict`:禁止 Vue `<script setup>` 中的组件导入名和 lower camel 本地声明产生命名冲突。
|
|
60
|
+
|
|
61
|
+
## 示例
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// good.ts
|
|
65
|
+
export function dialogProps() {
|
|
66
|
+
return {
|
|
67
|
+
visible: Boolean
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function dialogEmits() {
|
|
72
|
+
return ['confirm', 'cancel'];
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// bad.ts
|
|
78
|
+
export function createDialog() {
|
|
79
|
+
return {
|
|
80
|
+
visible: Boolean
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## 注意事项
|
|
86
|
+
|
|
87
|
+
`configs.vue2` 不内联引入 `common`、`js` 或 `ts`。接入完整预设时仍应显式组合 `polyv.configs.common`,并按项目语言选择 `polyv.configs.js` 或 `polyv.configs.ts`。
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import vue from 'eslint-plugin-vue';
|
|
3
|
+
import tseslint from 'typescript-eslint';
|
|
4
|
+
|
|
5
|
+
const vueFiles = ['*.vue', '**/*.vue'];
|
|
6
|
+
const vue2ReturnTypeInferencePatterns = [
|
|
7
|
+
'^[A-Za-z_$][\\w$]*(?:Props?|Emits?)$'
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
export default [
|
|
11
|
+
// 启用 Vue2 essential 规则和 vue-eslint-parser,承接 ESLint 8 的 plugin:vue/essential。
|
|
12
|
+
...vue.configs['flat/vue2-essential'],
|
|
13
|
+
{
|
|
14
|
+
files: vueFiles,
|
|
15
|
+
languageOptions: {
|
|
16
|
+
globals: {
|
|
17
|
+
...globals.browser,
|
|
18
|
+
...globals.node,
|
|
19
|
+
...globals.jest
|
|
20
|
+
},
|
|
21
|
+
parserOptions: {
|
|
22
|
+
parser: {
|
|
23
|
+
js: tseslint.parser,
|
|
24
|
+
ts: tseslint.parser,
|
|
25
|
+
'<template>': tseslint.parser
|
|
26
|
+
},
|
|
27
|
+
ecmaVersion: 'latest',
|
|
28
|
+
sourceType: 'module',
|
|
29
|
+
ecmaFeatures: {
|
|
30
|
+
jsx: true
|
|
31
|
+
},
|
|
32
|
+
extraFileExtensions: ['.vue'],
|
|
33
|
+
vueFeatures: {
|
|
34
|
+
filter: true,
|
|
35
|
+
interpolationAsNonHTML: true,
|
|
36
|
+
styleCSSVariableInjection: true
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
settings: {
|
|
43
|
+
// 为 polyv/explicit-module-boundary-types 提供 Vue2 props/emits 工厂方法命名约定。
|
|
44
|
+
'polyv/explicit-module-boundary-types': {
|
|
45
|
+
allowHookReturnTypeInference: true,
|
|
46
|
+
allowedReturnTypeInferencePatterns: vue2ReturnTypeInferencePatterns
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
rules: {
|
|
50
|
+
// 要求 template 使用 2 个空格缩进。
|
|
51
|
+
'vue/html-indent': ['error', 2],
|
|
52
|
+
// 约束 HTML、SVG、MathML 和组件标签的自闭合写法。
|
|
53
|
+
'vue/html-self-closing': ['error', {
|
|
54
|
+
html: {
|
|
55
|
+
void: 'always',
|
|
56
|
+
normal: 'never',
|
|
57
|
+
component: 'always'
|
|
58
|
+
},
|
|
59
|
+
svg: 'always',
|
|
60
|
+
math: 'always'
|
|
61
|
+
}],
|
|
62
|
+
// 允许 v-html,兼容历史富文本渲染场景。
|
|
63
|
+
'vue/no-v-html': 'off',
|
|
64
|
+
// 限制单行元素最多 3 个属性,多行元素每行 1 个属性。
|
|
65
|
+
'vue/max-attributes-per-line': ['error', {
|
|
66
|
+
singleline: 3,
|
|
67
|
+
multiline: 1
|
|
68
|
+
}],
|
|
69
|
+
// 允许单行 HTML 元素内容和标签写在同一行。
|
|
70
|
+
'vue/singleline-html-element-content-newline': 'off',
|
|
71
|
+
// 要求自定义事件名称使用 kebab-case。
|
|
72
|
+
'vue/custom-event-name-casing': ['error', 'kebab-case'],
|
|
73
|
+
// 禁止直接修改 props。
|
|
74
|
+
'vue/no-mutating-props': 'error',
|
|
75
|
+
// 要求组件名使用多个单词,但允许入口组件常用命名。
|
|
76
|
+
'vue/multi-word-component-names': ['error', {
|
|
77
|
+
ignores: ['index', 'Index', 'default', 'Default']
|
|
78
|
+
}],
|
|
79
|
+
// 要求模板中的 attribute 使用连字符命名。
|
|
80
|
+
'vue/attribute-hyphenation': ['error', 'always'],
|
|
81
|
+
// 要求 v-on 监听的自定义事件使用连字符命名。
|
|
82
|
+
'vue/v-on-event-hyphenation': ['error', 'always'],
|
|
83
|
+
// 要求模板中已注册组件使用 kebab-case 标签名。
|
|
84
|
+
'vue/component-name-in-template-casing': ['error', 'kebab-case', {
|
|
85
|
+
registeredComponentsOnly: true,
|
|
86
|
+
ignores: []
|
|
87
|
+
}],
|
|
88
|
+
// 约束 style 标签属性,允许普通 style 写法,违规时给出警告。
|
|
89
|
+
'vue/enforce-style-attribute': ['warn', {
|
|
90
|
+
allow: ['plain']
|
|
91
|
+
}],
|
|
92
|
+
// 禁止 Vue 组件导入名和同名 lower camel 本地变量冲突,避免模板组件被误识别为变量。
|
|
93
|
+
'polyv/no-vue-component-variable-name-conflict': 'error'
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
];
|
package/index.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import common from './configs/common/index.mjs';
|
|
2
|
+
import importx from './configs/importx/index.mjs';
|
|
3
|
+
import js from './configs/js/index.mjs';
|
|
4
|
+
import miniprogram from './configs/miniprogram/index.mjs';
|
|
5
|
+
import prettier from './configs/prettier/index.mjs';
|
|
6
|
+
import sonarjs from './configs/sonarjs/index.mjs';
|
|
7
|
+
import ts from './configs/ts/index.mjs';
|
|
8
|
+
import vue2 from './configs/vue2/index.mjs';
|
|
9
|
+
import plugin, { rules } from './plugin.mjs';
|
|
10
|
+
|
|
11
|
+
const configs = {
|
|
12
|
+
common,
|
|
13
|
+
importx,
|
|
14
|
+
js,
|
|
15
|
+
miniprogram,
|
|
16
|
+
prettier,
|
|
17
|
+
sonarjs,
|
|
18
|
+
ts,
|
|
19
|
+
vue2
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { configs, plugin };
|
|
23
|
+
export { rules };
|
|
24
|
+
|
|
25
|
+
export default {
|
|
26
|
+
configs,
|
|
27
|
+
plugin,
|
|
28
|
+
rules
|
|
29
|
+
};
|