@polyv/eslint-config 0.6.0-beta.5 → 0.7.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/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 20
package/README.md CHANGED
@@ -1,41 +1,19 @@
1
1
  # 保利威前端工程 ESLint 通用配置
2
2
 
3
- ## 用法
3
+ ## 安装和使用
4
4
 
5
- ### 安装
5
+ 确保 Node.js 版本符合 **^20.10.0**(未测试其他版本的可用性),运行 npm 命令安装:
6
6
 
7
7
  ```bash
8
8
  npm install @polyv/eslint-config --save-dev
9
9
  ```
10
10
 
11
- 此外,根据工程使用的技术栈,还需要安装以下依赖:
12
-
13
- | 依赖 | 版本要求 | 纯 JS 工程 | 纯 TS 工程 | Vue.js 工程(JS) |
14
- | --- | --- | --- | --- | --- |
15
- | eslint | >=8.0.0 | √ | √ | √ |
16
- | eslint-config-standard | >=17.0.0 | √ | √ | √ |
17
- | eslint-plugin-n | >=15.7.0 | √ | √ | √ |
18
- | eslint-plugin-import | >= 2.0.0 | √ | √ | √ |
19
- | eslint-plugin-promise | >= 6.1.0 | √ | √ | √ |
20
- | eslint-plugin-sonarjs | >= 0.18.0 | √ | √ | √ |
21
- | @babel/core | >=7.19.0 | √ | | √ |
22
- | @babel/eslint-parser | >=7.19.0 | √ | | √ |
23
- | typescript | >=4.0.0 | | √ | |
24
- | @typescript-eslint/eslint-plugin | >=5.0.0 | | √ | |
25
- | @typescript-eslint/parser | >=5.0.0 | | √ |
26
- | eslint-import-resolver-typescript | >=3.5.0 | | √ | |
27
- | eslint-plugin-vue | >=9.0.0 | | | √ |
28
- | vue-eslint-parser | >=9.0.0 | | | √ |
29
- | @vue/eslint-config-standard | >=8.0.0 | | | √ |
30
-
31
- ### 调用
32
-
33
- 创建 `.eslintrc.js`(一般在工程根目录下创建),并配置 `extends` 字段:
11
+ 在项目中创建 `.eslintrc.js`(一般在根目录下创建),根据工程的开发语言和框架配置 `extends` 字段:
34
12
 
35
13
  ```javascript
36
14
  /* eslint-env node */
37
15
 
38
- // 纯 JS 工程的配置
16
+ // 纯 JavaScript 工程的配置
39
17
  module.exports = {
40
18
  root: true,
41
19
  extends: [
@@ -47,7 +25,7 @@ module.exports = {
47
25
  ```javascript
48
26
  /* eslint-env node */
49
27
 
50
- // 纯 TS 工程的配置
28
+ // 纯 TypeScript 工程的配置
51
29
  module.exports = {
52
30
  root: true,
53
31
  extends: [
@@ -60,24 +38,37 @@ module.exports = {
60
38
  ```javascript
61
39
  /* eslint-env node */
62
40
 
63
- // Vue.js 工程(JS)的配置
41
+ // Vue.js 2.x 工程(JavaScript)的配置
64
42
  module.exports = {
65
43
  root: true,
66
44
  extends: [
67
- './node_modules/@polyv/eslint-config/lib/for-vue'
45
+ './node_modules/@polyv/eslint-config/lib/for-vue2-js'
68
46
  ]
69
47
  };
70
48
  ```
71
49
 
72
50
  ## 注意点
73
51
 
52
+ ### 严格模式
53
+
54
+ 部分规则在严格模式下(环境变量 `STRICT_LINT` 不为 `false` 时),告警级别为错误,其他情况为警告。这些规则包括:
55
+
56
+ - sonarjs/no-nested-template-literals
57
+ - @typescript-eslint/naming-convention
58
+ - vue/custom-event-name-casing
59
+ - vue/no-mutating-props
60
+ - vue/multi-word-component-names
61
+ - vue/attribute-hyphenation
62
+ - vue/v-on-event-hyphenation
63
+
74
64
  ### 生产环境构建的差异
75
65
 
76
- 部分规则在 `NODE_ENV` 为 `production` 时,告警级别为错误,其他情况下为警告。这些规则包括:
66
+ 部分规则在环境变量 `NODE_ENV` 为 `production` 时,告警级别为错误,其他情况下为警告。这些规则包括:
77
67
 
78
68
  - no-debugger
79
69
  - no-unused-vars
80
70
  - no-constant-condition
81
71
  - no-empty
72
+ - sonarjs/no-duplicate-string
82
73
  - @typescript-eslint/no-unused-vars
83
74
  - @typescript-eslint/explicit-module-boundary-types
package/lib/for-js.js CHANGED
@@ -2,7 +2,10 @@
2
2
  * JavaScript 验证规则(基础规则)。
3
3
  */
4
4
 
5
- const { devWarnProdError } = require('./util');
5
+ const {
6
+ devWarnProdError,
7
+ strictErrorOtherwiseWarn
8
+ } = require('./util');
6
9
 
7
10
  module.exports = {
8
11
  parser: '@babel/eslint-parser',
@@ -25,9 +28,27 @@ module.exports = {
25
28
  'plugin:sonarjs/recommended'
26
29
  ],
27
30
  rules: {
31
+ semi: ['error', 'always'],
32
+ 'space-before-function-paren': ['error', {
33
+ anonymous: 'never',
34
+ named: 'never',
35
+ asyncArrow: 'always'
36
+ }],
37
+ 'operator-linebreak': ['error', 'after', {
38
+ overrides: {
39
+ '?': 'before',
40
+ ':': 'before'
41
+ }
42
+ }],
43
+ 'comma-dangle': ['error', 'only-multiline'],
44
+ 'no-trailing-spaces': ['error', { ignoreComments: true }],
45
+ 'no-multiple-empty-lines': ['error', { max: 2 }],
46
+ 'wrap-iife': ['error', 'inside'],
47
+ 'no-confusing-arrow': 'error',
48
+ 'padded-blocks': 'off',
49
+
50
+ camelcase: 'error',
28
51
  'no-debugger': devWarnProdError,
29
- 'semi': ['error', 'always'],
30
- 'camelcase': 'error',
31
52
  'no-unused-vars': [devWarnProdError, {
32
53
  vars: 'all',
33
54
  args: 'after-used',
@@ -39,32 +60,25 @@ module.exports = {
39
60
  classes: false,
40
61
  variables: true
41
62
  }],
42
- 'space-before-function-paren': ['error', {
43
- anonymous: 'never',
44
- named: 'never',
45
- asyncArrow: 'always'
46
- }],
47
- 'operator-linebreak': ['error', 'after'],
48
- 'padded-blocks': 'off',
49
- 'comma-dangle': ['error', 'only-multiline'],
50
63
  'no-loop-func': 'error',
51
64
  'no-script-url': 'error',
52
65
  'no-new': 'off',
53
66
  'no-constant-condition': devWarnProdError,
54
- 'no-empty': [devWarnProdError, { 'allowEmptyCatch': true }],
67
+ 'no-empty': [devWarnProdError, { allowEmptyCatch: true }],
55
68
  'no-lonely-if': 'off',
56
- 'wrap-iife': ['error', 'inside'],
57
69
  'no-var': 'error',
58
- 'prefer-promise-reject-errors': 'error',
59
70
  'no-template-curly-in-string': 'off',
60
- 'no-confusing-arrow': 'error',
71
+ 'prefer-promise-reject-errors': 'error',
72
+
61
73
  'import/no-unresolved': 'off',
62
74
  'import/no-duplicates': 'error',
63
- 'promise/prefer-await-to-then': 'error',
64
- 'sonarjs/cognitive-complexity': ['error', 18],
65
- 'sonarjs/no-duplicate-string': ['error', 5],
75
+
76
+ 'promise/prefer-await-to-then': 'warn',
77
+
78
+ 'sonarjs/cognitive-complexity': ['error', 20],
79
+ 'sonarjs/no-duplicate-string': [devWarnProdError, { threshold: 5 }],
66
80
  'sonarjs/prefer-single-boolean-return': 'off',
67
81
  'sonarjs/no-collection-size-mischeck': 'off',
68
- 'sonarjs/no-nested-template-literals': 'warn'
82
+ 'sonarjs/no-nested-template-literals': strictErrorOtherwiseWarn
69
83
  }
70
84
  };
package/lib/for-ts.js CHANGED
@@ -2,7 +2,10 @@
2
2
  * TypeScript 工程的附加验证规则。
3
3
  */
4
4
 
5
- const { devWarnProdError } = require('./util');
5
+ const {
6
+ devWarnProdError,
7
+ strictErrorOtherwiseWarn
8
+ } = require('./util');
6
9
 
7
10
  module.exports = {
8
11
  overrides: [{
@@ -12,12 +15,12 @@ module.exports = {
12
15
  'plugin:import/typescript'
13
16
  ],
14
17
  rules: {
15
- 'semi': 'off',
18
+ semi: 'off',
16
19
  '@typescript-eslint/semi': ['error', 'always'],
17
20
 
18
- 'indent': 'off',
21
+ indent: 'off',
19
22
  '@typescript-eslint/indent': ['error', 2, {
20
- 'SwitchCase': 1
23
+ SwitchCase: 1
21
24
  }],
22
25
 
23
26
  'comma-spacing': 'off',
@@ -28,9 +31,9 @@ module.exports = {
28
31
 
29
32
  'no-use-before-define': 'off',
30
33
  '@typescript-eslint/no-use-before-define': ['error', {
31
- 'functions': false,
32
- 'classes': false,
33
- 'variables': true
34
+ functions: false,
35
+ classes: false,
36
+ variables: true
34
37
  }],
35
38
 
36
39
  'no-unused-vars': 'off',
@@ -51,6 +54,7 @@ module.exports = {
51
54
  asyncArrow: 'always'
52
55
  }],
53
56
 
57
+ '@typescript-eslint/no-explicit-any': 'warn',
54
58
  '@typescript-eslint/no-empty-function': 'off',
55
59
  '@typescript-eslint/no-empty-interface': 'off',
56
60
  '@typescript-eslint/type-annotation-spacing': ['error', {
@@ -64,7 +68,7 @@ module.exports = {
64
68
  }
65
69
  }],
66
70
  '@typescript-eslint/explicit-module-boundary-types': devWarnProdError,
67
- '@typescript-eslint/naming-convention': ['warn', {
71
+ '@typescript-eslint/naming-convention': [strictErrorOtherwiseWarn, {
68
72
  selector: 'enumMember',
69
73
  format: ['UPPER_CASE']
70
74
  }, {
@@ -2,6 +2,7 @@
2
2
  * Vue.js 工程的验证规则。
3
3
  */
4
4
 
5
+ const { strictErrorOtherwiseWarn } = require('./util');
5
6
  const jsConfig = require('./for-js');
6
7
 
7
8
  module.exports = Object.assign({}, jsConfig, {
@@ -23,7 +24,9 @@ module.exports = Object.assign({}, jsConfig, {
23
24
  void: 'always',
24
25
  normal: 'never',
25
26
  component: 'always'
26
- }
27
+ },
28
+ svg: 'always',
29
+ math: 'always'
27
30
  }],
28
31
  'vue/no-v-html': 'off',
29
32
  'vue/max-attributes-per-line': ['error', {
@@ -31,14 +34,12 @@ module.exports = Object.assign({}, jsConfig, {
31
34
  multiline: 1
32
35
  }],
33
36
  'vue/singleline-html-element-content-newline': 'off',
34
-
35
- // 为兼容以前代码而设为 warn
36
- 'vue/custom-event-name-casing': ['warn', 'kebab-case'],
37
- 'vue/no-mutating-props': 'warn',
38
- 'vue/multi-word-component-names': ['warn', {
39
- ignores: ['index', 'Index']
37
+ 'vue/custom-event-name-casing': [strictErrorOtherwiseWarn, 'kebab-case'],
38
+ 'vue/no-mutating-props': strictErrorOtherwiseWarn,
39
+ 'vue/multi-word-component-names': [strictErrorOtherwiseWarn, {
40
+ ignores: ['index', 'Index', 'default', 'Default']
40
41
  }],
41
- 'vue/attribute-hyphenation': ['warn', 'always'],
42
- 'vue/v-on-event-hyphenation': ['warn', 'always']
42
+ 'vue/attribute-hyphenation': [strictErrorOtherwiseWarn, 'always'],
43
+ 'vue/v-on-event-hyphenation': [strictErrorOtherwiseWarn, 'always']
43
44
  }
44
45
  });
package/lib/util.js CHANGED
@@ -1 +1,7 @@
1
- exports.devWarnProdError = process.env.NODE_ENV === 'production' ? 'error' : 'warn';
1
+ exports.devWarnProdError = process.env.NODE_ENV === 'production'
2
+ ? 'error'
3
+ : 'warn';
4
+
5
+ exports.strictErrorOtherwiseWarn = process.env.STRICT_LINT !== 'false'
6
+ ? 'error'
7
+ : 'warn';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/eslint-config",
3
- "version": "0.6.0-beta.5",
3
+ "version": "0.7.0",
4
4
  "description": "Standard ESLint configuration for Polyv projects.",
5
5
  "keywords": [
6
6
  "polyv",
@@ -9,39 +9,25 @@
9
9
  ],
10
10
  "homepage": "https://www.polyv.net/",
11
11
  "license": "MIT",
12
- "peerDependencies": {
13
- "@babel/core": ">=7.19.0",
14
- "@babel/eslint-parser": ">=7.19.0",
15
- "@typescript-eslint/eslint-plugin": ">=5.0.0",
16
- "@typescript-eslint/parser": ">=5.0.0",
17
- "@vue/eslint-config-standard": ">=8.0.0",
18
- "eslint": ">=8.0.0",
19
- "eslint-config-standard": "17.0.0",
20
- "eslint-import-resolver-typescript": ">=3.5.0",
21
- "eslint-plugin-import": ">=2.0.0",
22
- "eslint-plugin-n": ">=15.7.0",
23
- "eslint-plugin-promise": ">=6.1.0",
24
- "eslint-plugin-sonarjs": ">=0.18.0",
25
- "eslint-plugin-vue": ">=9.0.0",
26
- "typescript": ">=4.0.0",
27
- "vue-eslint-parser": ">=9.0.0"
12
+ "engines": {
13
+ "node": "^20.10.0"
28
14
  },
29
- "devDependencies": {
30
- "@babel/core": "^7.21.4",
31
- "@babel/eslint-parser": "^7.21.3",
32
- "@typescript-eslint/eslint-plugin": "^5.59.2",
33
- "@typescript-eslint/parser": "^5.59.1",
15
+ "dependencies": {
16
+ "@babel/core": "^7.24.9",
17
+ "@babel/eslint-parser": "^7.24.8",
18
+ "@typescript-eslint/eslint-plugin": "^7.16.1",
19
+ "@typescript-eslint/parser": "^7.16.1",
34
20
  "@vue/eslint-config-standard": "^8.0.1",
35
- "eslint": "^8.39.0",
36
- "eslint-config-standard": "^17.0.0",
37
- "eslint-import-resolver-typescript": "^3.5.5",
38
- "eslint-plugin-import": "^2.27.5",
39
- "eslint-plugin-n": "^15.7.0",
40
- "eslint-plugin-promise": "^6.1.1",
41
- "eslint-plugin-sonarjs": "^0.19.0",
42
- "eslint-plugin-vue": "^9.11.0",
43
- "typescript": "^5.0.4",
44
- "vue-eslint-parser": "^9.1.1"
21
+ "eslint": "^8.57.0",
22
+ "eslint-config-standard": "^17.1.0",
23
+ "eslint-import-resolver-typescript": "^3.6.1",
24
+ "eslint-plugin-import": "^2.29.1",
25
+ "eslint-plugin-n": "^16.6.2",
26
+ "eslint-plugin-promise": "^6.6.0",
27
+ "eslint-plugin-sonarjs": "^0.25.1",
28
+ "eslint-plugin-vue": "^9.27.0",
29
+ "typescript": "^5.5.3",
30
+ "vue-eslint-parser": "^9.4.3"
45
31
  },
46
32
  "scripts": {
47
33
  "print-config": "eslint --print-config ./lib/for-vue.js > config.txt"