@polyv/eslint-config 0.8.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
package/README.md
CHANGED
|
@@ -1,74 +1,123 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @polyv/eslint-config
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
这是面向 ESLint 10.x 的 flat config 配置包,包含通用插件配置、JavaScript 配置、TypeScript 配置、Vue2 补充配置和微信小程序专项配置。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## 安装
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @polyv/eslint-config --save-dev
|
|
8
|
+
npm install @polyv/eslint-config eslint@^10 --save-dev
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## 配置导航
|
|
12
|
+
|
|
13
|
+
- [通用配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/common/README.md):必须显式组合的基础配置,包含通用忽略项、运行 ESLint 时消费项目目录内的 `.gitignore`、`polyv` plugin 注册,以及 `promise`、`sonarjs`、`import-x` 推荐规则。
|
|
14
|
+
- [Import-X 配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/importx/README.md):提供 `eslint-plugin-import-x` 推荐规则和 node resolver 设置,已被通用配置继承。
|
|
15
|
+
- [SonarJS 配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/sonarjs/README.md):提供 `eslint-plugin-sonarjs` 推荐规则和项目内覆盖规则,已被通用配置继承。
|
|
16
|
+
- [JavaScript 配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/js/README.md):纯 JavaScript 项目使用,需和 `polyv.configs.common` 一起组合。
|
|
17
|
+
- [TypeScript 配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/ts/README.md):TypeScript 或 TypeScript + JavaScript 混合项目使用,已集成 JavaScript 配置,需和 `polyv.configs.common` 一起组合。
|
|
18
|
+
- [Vue2 配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/vue2/README.md):Vue2 项目使用,提供 Vue2 SFC 解析、Vue2 essential 规则、ESLint 8 `for-vue2-js.js` 迁移规则和 Vue2 TypeScript 返回类型推导 settings。
|
|
19
|
+
- [小程序配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/miniprogram/README.md):微信小程序项目使用,开启 `polyv/no-relative-directory-index-imports` 并处理小程序专项忽略。
|
|
20
|
+
- [Prettier 配置](https://github.com/polyv/eslint-config/blob/main/eslint10/configs/prettier/README.md):接入 `eslint-plugin-prettier/recommended` 和共享 Prettier 选项,需要放在配置数组最后。
|
|
21
|
+
|
|
22
|
+
## 规则导航
|
|
23
|
+
|
|
24
|
+
- [explicit-module-boundary-types](https://github.com/polyv/eslint-config/blob/main/eslint10/rules/explicit-module-boundary-types/README.md):要求导出函数显式声明模块边界类型,同时允许通过 options 或 settings 放开特定命名的返回类型推导。
|
|
25
|
+
- [no-relative-directory-index-imports](https://github.com/polyv/eslint-config/blob/main/eslint10/rules/no-relative-directory-index-imports/README.md):禁止依赖相对目录的 `index` 文件兜底解析,避免微信小程序 npm 构建后的运行时解析失败。
|
|
26
|
+
- [no-vue-component-variable-name-conflict](https://github.com/polyv/eslint-config/blob/main/eslint10/rules/no-vue-component-variable-name-conflict/README.md):禁止 Vue `<script setup>` 中的组件导入名和 lower camel 本地声明产生命名冲突。
|
|
27
|
+
|
|
28
|
+
## 快速选择
|
|
29
|
+
|
|
30
|
+
纯 JavaScript 项目:
|
|
12
31
|
|
|
13
32
|
```javascript
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
]
|
|
22
|
-
};
|
|
33
|
+
// eslint.config.mjs
|
|
34
|
+
import polyv from '@polyv/eslint-config';
|
|
35
|
+
|
|
36
|
+
export default [
|
|
37
|
+
...polyv.configs.common,
|
|
38
|
+
...polyv.configs.js
|
|
39
|
+
];
|
|
23
40
|
```
|
|
24
41
|
|
|
42
|
+
TypeScript 或 TypeScript + JavaScript 混合项目:
|
|
43
|
+
|
|
25
44
|
```javascript
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
'./node_modules/@polyv/eslint-config/lib/for-ts'
|
|
34
|
-
]
|
|
35
|
-
};
|
|
45
|
+
// eslint.config.mjs
|
|
46
|
+
import polyv from '@polyv/eslint-config';
|
|
47
|
+
|
|
48
|
+
export default [
|
|
49
|
+
...polyv.configs.common,
|
|
50
|
+
...polyv.configs.ts
|
|
51
|
+
];
|
|
36
52
|
```
|
|
37
53
|
|
|
54
|
+
Vue2 JavaScript 项目:
|
|
55
|
+
|
|
38
56
|
```javascript
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
};
|
|
57
|
+
// eslint.config.mjs
|
|
58
|
+
import polyv from '@polyv/eslint-config';
|
|
59
|
+
|
|
60
|
+
export default [
|
|
61
|
+
...polyv.configs.common,
|
|
62
|
+
...polyv.configs.js,
|
|
63
|
+
...polyv.configs.vue2
|
|
64
|
+
];
|
|
48
65
|
```
|
|
49
66
|
|
|
50
|
-
|
|
67
|
+
Vue2 TypeScript 项目:
|
|
51
68
|
|
|
52
|
-
|
|
69
|
+
```javascript
|
|
70
|
+
// eslint.config.mjs
|
|
71
|
+
import polyv from '@polyv/eslint-config';
|
|
72
|
+
|
|
73
|
+
export default [
|
|
74
|
+
...polyv.configs.common,
|
|
75
|
+
...polyv.configs.ts,
|
|
76
|
+
...polyv.configs.vue2
|
|
77
|
+
];
|
|
78
|
+
```
|
|
53
79
|
|
|
54
|
-
|
|
80
|
+
微信小程序项目:
|
|
55
81
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
- vue/no-mutating-props
|
|
60
|
-
- vue/multi-word-component-names
|
|
61
|
-
- vue/attribute-hyphenation
|
|
62
|
-
- vue/v-on-event-hyphenation
|
|
82
|
+
```javascript
|
|
83
|
+
// eslint.config.mjs
|
|
84
|
+
import polyv from '@polyv/eslint-config';
|
|
63
85
|
|
|
64
|
-
|
|
86
|
+
export default [
|
|
87
|
+
...polyv.configs.common,
|
|
88
|
+
...polyv.configs.miniprogram
|
|
89
|
+
];
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
需要接入 Prettier 时,把 `polyv.configs.prettier` 放在最后:
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
// eslint.config.mjs
|
|
96
|
+
import polyv from '@polyv/eslint-config';
|
|
97
|
+
|
|
98
|
+
export default [
|
|
99
|
+
...polyv.configs.common,
|
|
100
|
+
...polyv.configs.js,
|
|
101
|
+
...polyv.configs.prettier
|
|
102
|
+
];
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## 插件和规则
|
|
65
106
|
|
|
66
|
-
|
|
107
|
+
- `polyv.configs.common` 会默认注册 `polyv` plugin。如果已经引用了 `polyv.configs.common`,则无需额外注册该插件。
|
|
108
|
+
- 只需要单独使用自定义规则时,可以引入 `plugin` 或 `@polyv/eslint-config/plugin` 后手动注册。
|
|
109
|
+
- 当前自定义规则:
|
|
110
|
+
- [polyv/explicit-module-boundary-types](https://github.com/polyv/eslint-config/blob/main/eslint10/rules/explicit-module-boundary-types/README.md):要求导出函数显式声明模块边界类型,同时允许指定函数或方法省略返回类型。
|
|
111
|
+
- [polyv/no-relative-directory-index-imports](https://github.com/polyv/eslint-config/blob/main/eslint10/rules/no-relative-directory-index-imports/README.md):用于禁止依赖相对目录的 `index` 文件兜底解析。
|
|
112
|
+
- [polyv/no-vue-component-variable-name-conflict](https://github.com/polyv/eslint-config/blob/main/eslint10/rules/no-vue-component-variable-name-conflict/README.md):用于禁止 Vue `<script setup>` 中的组件导入名和 lower camel 本地声明产生命名冲突。
|
|
67
113
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
114
|
+
## Prettier 配置
|
|
115
|
+
|
|
116
|
+
消费项目如果需要单独给 Prettier 使用共享配置,可以引入:
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
// prettier.config.mjs
|
|
120
|
+
import prettierConfig from '@polyv/eslint-config/prettier-config';
|
|
121
|
+
|
|
122
|
+
export default prettierConfig;
|
|
123
|
+
```
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# 通用配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.common` 是接入本包预设配置时必须显式组合的基础配置。
|
|
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
|
+
];
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
也可以通过子路径单独引入:
|
|
17
|
+
|
|
18
|
+
```javascript
|
|
19
|
+
// eslint.config.mjs
|
|
20
|
+
import common from '@polyv/eslint-config/configs/common';
|
|
21
|
+
|
|
22
|
+
export default [
|
|
23
|
+
...common
|
|
24
|
+
];
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 配置内容
|
|
28
|
+
|
|
29
|
+
- 配置通用忽略项:`node_modules/`、`dest/`、`dist/`、`.dist/`、`coverage/`、`pnpm-lock.yaml`、`package-lock.json`。
|
|
30
|
+
- 会递归读取运行 ESLint 时消费项目目录内的 `.gitignore`,让不提交到 git 的产物文件也进入 ESLint 忽略范围;扫描时会跳过 `.git`、`node_modules`、`dest`、`dist`、`.dist`、`coverage`。
|
|
31
|
+
- 默认注册 `polyv` plugin;如果已经引用 `polyv.configs.common`,消费项目无需额外注册该插件。
|
|
32
|
+
- 继承 [Import-X 配置](../importx/README.md),启用 `eslint-plugin-import-x` 推荐规则和 node resolver。
|
|
33
|
+
- 启用 `eslint-plugin-promise` 的 `flat/recommended` 配置。
|
|
34
|
+
- 继承 [SonarJS 配置](../sonarjs/README.md),启用 `eslint-plugin-sonarjs` 推荐规则和项目内覆盖规则。
|
|
35
|
+
|
|
36
|
+
## 规则命名
|
|
37
|
+
|
|
38
|
+
导入相关规则使用 `import-x/...` 命名空间,例如:
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// eslint.config.mjs
|
|
42
|
+
{
|
|
43
|
+
rules: {
|
|
44
|
+
'import-x/no-duplicates': 'error'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 插件用法
|
|
50
|
+
|
|
51
|
+
如果没有使用 `polyv.configs.common`,但需要单独启用 Polyv 自定义规则,可以手动注册插件:
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
// eslint.config.mjs
|
|
55
|
+
import { plugin as polyvPlugin } from '@polyv/eslint-config';
|
|
56
|
+
|
|
57
|
+
export default [
|
|
58
|
+
{
|
|
59
|
+
plugins: {
|
|
60
|
+
polyv: polyvPlugin
|
|
61
|
+
},
|
|
62
|
+
rules: {
|
|
63
|
+
'polyv/explicit-module-boundary-types': 'error',
|
|
64
|
+
'polyv/no-relative-directory-index-imports': 'error'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
也可以从子路径单独引入 plugin:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
// eslint.config.mjs
|
|
74
|
+
import polyvPlugin from '@polyv/eslint-config/plugin';
|
|
75
|
+
|
|
76
|
+
export default [
|
|
77
|
+
{
|
|
78
|
+
plugins: {
|
|
79
|
+
polyv: polyvPlugin
|
|
80
|
+
},
|
|
81
|
+
rules: {
|
|
82
|
+
'polyv/explicit-module-boundary-types': 'error',
|
|
83
|
+
'polyv/no-relative-directory-index-imports': 'error'
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
];
|
|
87
|
+
```
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { includeIgnoreFile } from 'eslint/config';
|
|
4
|
+
import promisePlugin from 'eslint-plugin-promise';
|
|
5
|
+
import importx from '../importx/index.mjs';
|
|
6
|
+
import sonarjs from '../sonarjs/index.mjs';
|
|
7
|
+
import polyvPlugin from '../../plugin.mjs';
|
|
8
|
+
|
|
9
|
+
const skippedGitignoreSearchDirectories = new Set([
|
|
10
|
+
'.git',
|
|
11
|
+
'node_modules',
|
|
12
|
+
'dest',
|
|
13
|
+
'dist',
|
|
14
|
+
'.dist',
|
|
15
|
+
'coverage'
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
function findGitignoreFiles(root) {
|
|
19
|
+
const gitignoreFiles = [];
|
|
20
|
+
const pendingDirectories = [root];
|
|
21
|
+
|
|
22
|
+
while (pendingDirectories.length > 0) {
|
|
23
|
+
const directory = pendingDirectories.pop();
|
|
24
|
+
|
|
25
|
+
let entries;
|
|
26
|
+
try {
|
|
27
|
+
entries = fs.readdirSync(directory, { withFileTypes: true });
|
|
28
|
+
} catch {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
const entryPath = path.join(directory, entry.name);
|
|
34
|
+
|
|
35
|
+
if (entry.isFile() && entry.name === '.gitignore') {
|
|
36
|
+
gitignoreFiles.push(entryPath);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (entry.isDirectory() && !skippedGitignoreSearchDirectories.has(entry.name)) {
|
|
41
|
+
pendingDirectories.push(entryPath);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return gitignoreFiles;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const consumerRoot = fs.realpathSync(process.cwd());
|
|
50
|
+
const consumerGitignorePaths = findGitignoreFiles(consumerRoot);
|
|
51
|
+
const consumerGitignoreConfig = consumerGitignorePaths.length > 0
|
|
52
|
+
? includeIgnoreFile(consumerGitignorePaths, {
|
|
53
|
+
// 读取消费项目内的 .gitignore,让未提交到 git 的产物文件也进入 ESLint 忽略范围。
|
|
54
|
+
gitignoreResolution: true,
|
|
55
|
+
name: 'polyv/consumer-gitignore'
|
|
56
|
+
})
|
|
57
|
+
: [];
|
|
58
|
+
|
|
59
|
+
export default [
|
|
60
|
+
...consumerGitignoreConfig,
|
|
61
|
+
{
|
|
62
|
+
ignores: [
|
|
63
|
+
'node_modules/**',
|
|
64
|
+
'**/node_modules/**',
|
|
65
|
+
'dest/**',
|
|
66
|
+
'**/dest/**',
|
|
67
|
+
'dist/**',
|
|
68
|
+
'**/dist/**',
|
|
69
|
+
'.dist/**',
|
|
70
|
+
'**/.dist/**',
|
|
71
|
+
'coverage/**',
|
|
72
|
+
'pnpm-lock.yaml',
|
|
73
|
+
'package-lock.json'
|
|
74
|
+
]
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
plugins: {
|
|
78
|
+
// 注册 Polyv 自定义规则插件,供各场景配置按需启用 polyv/* 规则。
|
|
79
|
+
polyv: polyvPlugin
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
...importx,
|
|
83
|
+
// 启用 promise 推荐规则,检查 Promise 常见错误写法。
|
|
84
|
+
promisePlugin.configs['flat/recommended'],
|
|
85
|
+
...sonarjs
|
|
86
|
+
];
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Import-X 配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.importx` 用于提供 `eslint-plugin-import-x` 的推荐规则和 Node resolver 配置。
|
|
4
|
+
|
|
5
|
+
## 使用方式
|
|
6
|
+
|
|
7
|
+
通常不需要单独引入该配置,`polyv.configs.common` 已经继承了 `polyv.configs.importx`:
|
|
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
|
+
如果只需要 import-x 能力,也可以单独组合:
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
// eslint.config.mjs
|
|
22
|
+
import polyv from '@polyv/eslint-config';
|
|
23
|
+
|
|
24
|
+
export default [
|
|
25
|
+
...polyv.configs.importx
|
|
26
|
+
];
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
也可以通过子路径单独引入:
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
// eslint.config.mjs
|
|
33
|
+
import importx from '@polyv/eslint-config/configs/importx';
|
|
34
|
+
|
|
35
|
+
export default [
|
|
36
|
+
...importx
|
|
37
|
+
];
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 配置内容
|
|
41
|
+
|
|
42
|
+
- 启用 `eslint-plugin-import-x` 的 `flatConfigs.recommended`。
|
|
43
|
+
- 通过 `import-x/ignore` 忽略 `vite-plugin-dts`,避免该包 ESM/CJS 导出形态差异触发 import-x 误报。
|
|
44
|
+
- 配置 `import-x/resolver` 使用 node resolver。
|
|
45
|
+
- resolver 扩展名包含 `.js`、`.cjs`、`.mjs`、`.jsx`、`.ts`、`.tsx`、`.mts`、`.cts`、`.d.ts`。
|
|
46
|
+
- 集中维护导入导出规则,例如 `import-x/default`、`import-x/export`、`import-x/first`、`import-x/named`、`import-x/namespace`、`import-x/no-absolute-path`、`import-x/no-duplicates`、`import-x/no-named-as-default`、`import-x/no-named-default`、`import-x/no-unresolved` 和 `import-x/no-webpack-loader-syntax`。
|
|
47
|
+
- 针对本包自身的 `eslint.config.mjs` 关闭 `import-x/no-named-as-default-member`,避免默认导出访问 `configs` 时误报。
|
|
48
|
+
|
|
49
|
+
## 注意事项
|
|
50
|
+
|
|
51
|
+
ESLint 10 下的 import 能力按 `eslint-plugin-import-x` README 的写法接入,使用 `import { importX } from 'eslint-plugin-import-x'` 和 `import-x/...` 规则命名空间。
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { importX } from 'eslint-plugin-import-x';
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
// 启用 import-x 推荐规则,检查导入导出正确性和常见导入问题。
|
|
5
|
+
importX.flatConfigs.recommended,
|
|
6
|
+
{
|
|
7
|
+
settings: {
|
|
8
|
+
// import-x/ignore 匹配的是解析后的路径;vite-plugin-dts 已覆盖该包的 ESM/CJS 导出形态差异,避免 import-x 产生误报。
|
|
9
|
+
'import-x/ignore': ['vite-plugin-dts'],
|
|
10
|
+
'import-x/resolver': {
|
|
11
|
+
node: {
|
|
12
|
+
extensions: ['.js', '.cjs', '.mjs', '.jsx', '.ts', '.tsx', '.mts', '.cts', '.d.ts']
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
rules: {
|
|
17
|
+
// 检查默认导入是否真实存在。
|
|
18
|
+
'import-x/default': 'error',
|
|
19
|
+
// 检查导出语句是否有效。
|
|
20
|
+
'import-x/export': 'error',
|
|
21
|
+
// 要求 import 语句放在文件顶部。
|
|
22
|
+
'import-x/first': 'error',
|
|
23
|
+
// 检查具名导入是否真实存在。
|
|
24
|
+
'import-x/named': 'error',
|
|
25
|
+
// 检查命名空间导入后的成员访问是否真实存在。
|
|
26
|
+
'import-x/namespace': 'error',
|
|
27
|
+
// 禁止使用绝对路径导入。
|
|
28
|
+
'import-x/no-absolute-path': ['error', {
|
|
29
|
+
esmodule: true,
|
|
30
|
+
commonjs: true,
|
|
31
|
+
amd: false
|
|
32
|
+
}],
|
|
33
|
+
// 禁止重复导入同一个模块。
|
|
34
|
+
'import-x/no-duplicates': 'error',
|
|
35
|
+
// 警告容易和默认导入混淆的具名导出命名。
|
|
36
|
+
'import-x/no-named-as-default': 'warn',
|
|
37
|
+
// 禁止用具名方式导入 default。
|
|
38
|
+
'import-x/no-named-default': 'error',
|
|
39
|
+
// 当前项目不检查路径解析,避免别名和构建工具路径误报。
|
|
40
|
+
'import-x/no-unresolved': 'off',
|
|
41
|
+
// 禁止在导入路径中使用 webpack loader 语法。
|
|
42
|
+
'import-x/no-webpack-loader-syntax': 'error'
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
files: ['eslint.config.mjs'],
|
|
47
|
+
rules: {
|
|
48
|
+
// 本包自检配置通过默认导出访问 configs,关闭 import-x 的默认成员误报。
|
|
49
|
+
'import-x/no-named-as-default-member': 'off'
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# JavaScript 配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.js` 用于纯 JavaScript 项目。它包含 `@eslint/js` 的推荐配置,并迁移 ESLint 8 `for-js.js` 中仍适用于 ESLint 10 的规则语义。
|
|
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.js
|
|
14
|
+
];
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
纯 JavaScript 项目使用 `configs.js`;TypeScript 或 TypeScript + JavaScript 混合项目应使用 `configs.ts`,因为 `configs.ts` 已经集成 `configs.js`。
|
|
18
|
+
|
|
19
|
+
## 配置内容
|
|
20
|
+
|
|
21
|
+
- 启用 `@eslint/js` 的 `js.configs.recommended`。
|
|
22
|
+
- 配置 JavaScript 文件范围:`**/*.{js,cjs,mjs,jsx}`。
|
|
23
|
+
- 注入 browser 和 node 全局变量。
|
|
24
|
+
- 迁移基础代码风格规则,例如分号、函数括号前空格、操作符换行、尾逗号、空行和尾随空格。
|
|
25
|
+
- 迁移基础质量规则,例如 `no-debugger`、`no-unused-vars`、`no-use-before-define`、`no-var`、`no-empty`。
|
|
26
|
+
- 迁移 `promise` 相关规则;相关插件由 `polyv.configs.common` 统一提供。
|
|
27
|
+
- `import-x` 相关规则统一放在 `polyv.configs.importx` 中,并由 `polyv.configs.common` 继承。
|
|
28
|
+
- `sonarjs` 相关规则统一放在 `polyv.configs.sonarjs` 中,并由 `polyv.configs.common` 继承。
|
|
29
|
+
|
|
30
|
+
## 注意事项
|
|
31
|
+
|
|
32
|
+
- `configs.js` 不注册 `polyv`、`import-x`、`promise` 或 `sonarjs` 插件,也不维护 `import-x/*` 或 `sonarjs/*` 规则。
|
|
33
|
+
- 接入时必须显式组合 `polyv.configs.common`,否则 `import-x/*`、`promise/*`、`sonarjs/*` 规则无法加载。
|
|
34
|
+
- `.cjs` 文件会被设置为 `sourceType: 'commonjs'`。
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import js from '@eslint/js';
|
|
2
|
+
import globals from 'globals';
|
|
3
|
+
import { devWarnProdError } from '../../utils.mjs';
|
|
4
|
+
|
|
5
|
+
const jsFiles = ['**/*.{js,cjs,mjs,jsx}'];
|
|
6
|
+
|
|
7
|
+
export default [
|
|
8
|
+
js.configs.recommended,
|
|
9
|
+
{
|
|
10
|
+
files: jsFiles,
|
|
11
|
+
languageOptions: {
|
|
12
|
+
ecmaVersion: 'latest',
|
|
13
|
+
sourceType: 'module',
|
|
14
|
+
globals: {
|
|
15
|
+
...globals.browser,
|
|
16
|
+
...globals.node
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
rules: {
|
|
20
|
+
// 要求语句结尾必须使用分号。
|
|
21
|
+
semi: ['error', 'always'],
|
|
22
|
+
// 约束函数括号前空格:普通函数不留空格,async 箭头函数留空格。
|
|
23
|
+
'space-before-function-paren': ['error', {
|
|
24
|
+
anonymous: 'never',
|
|
25
|
+
named: 'never',
|
|
26
|
+
asyncArrow: 'always'
|
|
27
|
+
}],
|
|
28
|
+
// 要求操作符换行时放在行尾,但三元表达式的问号和冒号放在行首。
|
|
29
|
+
'operator-linebreak': ['error', 'after', {
|
|
30
|
+
overrides: {
|
|
31
|
+
'?': 'before',
|
|
32
|
+
':': 'before'
|
|
33
|
+
}
|
|
34
|
+
}],
|
|
35
|
+
// 只允许多行结构使用尾逗号,单行结构不允许。
|
|
36
|
+
'comma-dangle': ['error', 'only-multiline'],
|
|
37
|
+
// 禁止行尾空格,但允许注释中保留尾随空格。
|
|
38
|
+
'no-trailing-spaces': ['error', {
|
|
39
|
+
ignoreComments: true
|
|
40
|
+
}],
|
|
41
|
+
// 限制连续空行最多 2 行。
|
|
42
|
+
'no-multiple-empty-lines': ['error', {
|
|
43
|
+
max: 2
|
|
44
|
+
}],
|
|
45
|
+
// 要求立即执行函数的调用括号包在函数表达式内部。
|
|
46
|
+
'wrap-iife': ['error', 'inside'],
|
|
47
|
+
// 禁止可能与比较运算混淆的箭头函数写法。
|
|
48
|
+
'no-confusing-arrow': 'error',
|
|
49
|
+
// 允许代码块内出现首尾空行。
|
|
50
|
+
'padded-blocks': 'off',
|
|
51
|
+
|
|
52
|
+
// 要求标识符使用驼峰命名。
|
|
53
|
+
camelcase: 'error',
|
|
54
|
+
// 禁止 debugger;生产环境报错,其他环境警告。
|
|
55
|
+
'no-debugger': devWarnProdError,
|
|
56
|
+
// 检查未使用变量;生产环境报错,其他环境警告。
|
|
57
|
+
'no-unused-vars': [devWarnProdError, {
|
|
58
|
+
vars: 'all',
|
|
59
|
+
args: 'after-used',
|
|
60
|
+
ignoreRestSiblings: true,
|
|
61
|
+
caughtErrors: 'none'
|
|
62
|
+
}],
|
|
63
|
+
// 允许变量赋值后未再被读取,避免历史代码中的中间赋值触发新规则。
|
|
64
|
+
'no-useless-assignment': 'off',
|
|
65
|
+
// 禁止变量在定义前使用,但允许函数和类提前使用。
|
|
66
|
+
'no-use-before-define': ['error', {
|
|
67
|
+
functions: false,
|
|
68
|
+
classes: false,
|
|
69
|
+
variables: true
|
|
70
|
+
}],
|
|
71
|
+
// 禁止在循环中定义可能捕获循环变量的函数。
|
|
72
|
+
'no-loop-func': 'error',
|
|
73
|
+
// 禁止使用 javascript: URL。
|
|
74
|
+
'no-script-url': 'error',
|
|
75
|
+
// 允许只实例化对象而不保存返回值。
|
|
76
|
+
'no-new': 'off',
|
|
77
|
+
// 禁止常量条件;生产环境报错,其他环境警告。
|
|
78
|
+
'no-constant-condition': devWarnProdError,
|
|
79
|
+
// 禁止空代码块,但允许空 catch。
|
|
80
|
+
'no-empty': [devWarnProdError, {
|
|
81
|
+
allowEmptyCatch: true
|
|
82
|
+
}],
|
|
83
|
+
// 允许 else 中只有一个 if。
|
|
84
|
+
'no-lonely-if': 'off',
|
|
85
|
+
// 禁止 var,统一使用 let 或 const。
|
|
86
|
+
'no-var': 'error',
|
|
87
|
+
// 允许普通字符串里出现类似模板字符串占位符的文本。
|
|
88
|
+
'no-template-curly-in-string': 'off',
|
|
89
|
+
// 要求 Promise reject 使用 Error 对象。
|
|
90
|
+
'prefer-promise-reject-errors': 'error',
|
|
91
|
+
|
|
92
|
+
// 检查 Promise 构造函数参数命名。
|
|
93
|
+
'promise/param-names': 'error',
|
|
94
|
+
// 建议使用 async/await 替代 then 链。
|
|
95
|
+
'promise/prefer-await-to-then': 'warn',
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
files: ['**/*.{cjs}'],
|
|
100
|
+
languageOptions: {
|
|
101
|
+
sourceType: 'commonjs'
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# 小程序配置
|
|
2
|
+
|
|
3
|
+
`polyv.configs.miniprogram` 用于微信小程序项目,重点处理小程序 npm 构建后的相对目录 `index` 解析问题。
|
|
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.miniprogram
|
|
14
|
+
];
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
如果项目使用 TypeScript,请在消费项目自己的 ESLint 配置中先配置 TypeScript parser,再叠加 `polyv.configs.common` 和 `polyv.configs.miniprogram`。
|
|
18
|
+
|
|
19
|
+
## 配置内容
|
|
20
|
+
|
|
21
|
+
- 忽略微信小程序构建 npm 后生成的 `miniprogram_npm/` 目录。
|
|
22
|
+
- 注入小程序全局变量:`App`、`Component`、`Page`、`getApp`、`wx`。
|
|
23
|
+
- 关闭 `import-x/no-useless-path-segments`,避免它建议移除显式 `/index`,从而和小程序路径要求冲突。
|
|
24
|
+
- 开启 `polyv/no-relative-directory-index-imports`,禁止依赖相对目录的 `index` 文件兜底解析。
|
|
25
|
+
- 单元测试相关文件会关闭 `polyv/no-relative-directory-index-imports`,包括 `test/`、`tests/`、`__test__/`、`__tests__/` 目录,以及 `*.test.*`、`*.spec.*` 文件。
|
|
26
|
+
|
|
27
|
+
## 自定义规则
|
|
28
|
+
|
|
29
|
+
`polyv/no-relative-directory-index-imports` 用于禁止这类写法:
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
// bad.js
|
|
33
|
+
import helper from './helper';
|
|
34
|
+
const utils = require('./utils');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
应显式写成:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
// good.js
|
|
41
|
+
import helper from './helper/index';
|
|
42
|
+
const utils = require('./utils/index');
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
该规则同时处理 `./`、`.`、`../`、`..` 等目录入口写法。
|
|
46
|
+
|
|
47
|
+
## 注意事项
|
|
48
|
+
|
|
49
|
+
- `polyv` plugin 由 `polyv.configs.common` 统一注册。
|
|
50
|
+
- `configs.miniprogram` 不内联引入 `common`,消费项目必须显式组合 `polyv.configs.common`。
|