@stdex/presets-eslint-typescript 1.0.0 → 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 安坚实
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # 通用 ESLint 配置(针对 TypeScript 项目)
2
+
3
+ General ESLint Configurations(For TypeScript Projects)
4
+
5
+ 详细介绍和使用方法见:[presets-eslint/README.md](../README.md)
6
+
7
+ For detailed documentation and usage instructions, see [presets-eslint/README_EN.md](../README_EN.md).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stdex/presets-eslint-typescript",
3
- "version": "1.0.0",
4
- "description": "公共 ESLint 配置",
3
+ "version": "1.0.1",
4
+ "description": "通用 ESLint 配置(针对 TypeScript 项目) - General ESLint Configurations(For TypeScript Projects) ",
5
5
  "homepage": "https://github.com/anjianshi/stdex-packages/presets-eslint/",
6
6
  "bugs": {
7
7
  "url": "https://github.com/anjianshi/stdex-packages/issues",
@@ -9,19 +9,23 @@
9
9
  },
10
10
  "license": "MIT",
11
11
  "author": "anjianshi <anjianshi@gmail.com>",
12
- "repository": "github:anjianshi/stdex-packages",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "github:anjianshi/stdex-packages"
15
+ },
13
16
  "publishConfig": {
14
17
  "registry": "https://registry.npmjs.org/",
15
18
  "access": "public"
16
19
  },
17
20
  "main": "index.mjs",
18
21
  "dependencies": {
19
- "@stdex/presets-eslint-base": "workspace:*",
22
+ "@stdex/presets-eslint-base": "1.0.1",
20
23
  "eslint-config-prettier": "^10.1.8",
21
24
  "eslint-import-resolver-typescript": "^4.4.5",
25
+ "eslint-plugin-import-x": "^4.17.1",
22
26
  "typescript-eslint": "^8.64.0"
23
27
  },
24
28
  "peerDependencies": {
25
29
  "eslint": "^10.6.0"
26
30
  }
27
- }
31
+ }
package/parts/import.mjs CHANGED
@@ -1,7 +1,12 @@
1
1
  /**
2
2
  * import 检验配置
3
3
  * 参考自 eslint-plugin-import-x 的 flatConfigs.typescript 内容
4
+ *
5
+ * Import validation configuration.
6
+ * Based on the `flatConfigs.typescript` configuration from `eslint-plugin-import-x`.
4
7
  */
8
+ import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript'
9
+ import { createNodeResolver } from 'eslint-plugin-import-x'
5
10
  import { tsExtensions, tsFiles } from './common.mjs'
6
11
 
7
12
  export default {
@@ -13,24 +18,23 @@ export default {
13
18
  'import-x/external-module-folders': ['node_modules', 'node_modules/@types'],
14
19
 
15
20
  // 指定哪些扩展名的文件需要进一步解析(以看其导出了哪些内容)
21
+ // Specify which file extensions require further parsing to determine their exports.
16
22
  'import-x/extensions': [...tsExtensions, 'js', 'mjs', 'cjs', 'jsx', 'mjsx', 'cjsx'],
17
23
 
18
24
  // 指定各扩展名要使用什么 parser 来解析(以看其导出了哪些内容)
19
25
  // 未指定的当做普通 JavaScript 文件处理
26
+ // Specify which parser to use for each file extension to determine its exports.
27
+ // Unspecified extensions are treated as regular JavaScript files.
20
28
  'import-x/parsers': {
21
29
  '@typescript-eslint/parser': [...tsExtensions],
22
30
  },
23
31
 
24
- 'import-x/resolver': {
25
- node: true,
26
- typescript: {
27
- project: './',
28
- },
29
- },
32
+ 'import-x/resolver-next': [createTypeScriptImportResolver(), createNodeResolver()],
30
33
  },
31
34
 
32
35
  rules: {
33
- // TypeScript 会自行检查以下项目,禁用它们以避免冲突并提升性能.
36
+ // TypeScript 会自行检查以下项目,禁用它们以避免冲突并提升性能。
37
+ // TypeScript checks the following items itself, so disable these rules to avoid conflicts and improve performance.
34
38
  // <https://typescript-eslint.io/troubleshooting/typed-linting/performance#eslint-plugin-import>
35
39
  'import-x/default': 'off',
36
40
  'import-x/named': 'off',
package/parts/main.mjs CHANGED
@@ -1,16 +1,22 @@
1
1
  /*
2
2
  【一些规则不启用的理由】
3
+ 【Reasons for disabling certain rules】
4
+
3
5
  @typescript-eslint/consistent-type-definitions
4
6
  未能覆盖所有情况,有些时候必须用 type,不能用 interface,但它识别不出来。
7
+ Does not cover all cases. In some situations, type must be used instead of interface, but the rule cannot detect them.
5
8
 
6
9
  @typescript-eslint/no-invalid-void-type
7
10
  不能很好地覆盖所有情况,例如一个类型有个 generic type,是最终用于定义函数返回值的,本应允许是 void,但这条规则不允许。
11
+ Does not handle all cases well. For example, if a generic type parameter is ultimately used as a function's return type, void should be allowed, but this rule rejects it.
8
12
 
9
13
  @typescript-eslint/no-meaningless-void-operator
10
14
  和 no-confusing-void-expression 的规则冲突。
15
+ Conflicts with the no-confusing-void-expression rule.
11
16
 
12
17
  @typescript-eslint/prefer-optional-chain
13
18
  和其他规则起冲突。
19
+ Conflicts with other rules.
14
20
  */
15
21
  import { tsFiles } from './common.mjs'
16
22
 
@@ -26,10 +32,14 @@ export default {
26
32
  },
27
33
 
28
34
  rules: {
29
- 'default-case': 'off', // 交给 @typescript-eslint/switch-exhaustiveness-check 规则来检查 switch
30
- 'no-void': 'off', // Typescript 里 void 符号另有用途
35
+ // 交给 @typescript-eslint/switch-exhaustiveness-check 规则来检查 switch
36
+ // Leave `switch` statement checks to the `@typescript-eslint/switch-exhaustiveness-check` rule.
37
+ 'default-case': 'off',
38
+
39
+ // Typescript 里 void 符号另有用途
40
+ // `void` has other uses in TypeScript.
41
+ 'no-void': 'off',
31
42
 
32
- // Supported Rules
33
43
  '@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow-with-description' }],
34
44
  '@typescript-eslint/consistent-type-exports': [
35
45
  'error',
@@ -37,7 +47,10 @@ export default {
37
47
  ],
38
48
  '@typescript-eslint/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports' }],
39
49
  '@typescript-eslint/explicit-member-accessibility': ['error', { accessibility: 'no-public' }],
40
- '@typescript-eslint/no-confusing-void-expression': ['off', { ignoreVoidOperator: true }], // 在 https://github.com/typescript-eslint/typescript-eslint/issues/8538 修复后再考虑启用
50
+ '@typescript-eslint/no-confusing-void-expression': [
51
+ 'error',
52
+ { ignoreArrowShorthand: true, ignoreVoidOperator: true, ignoreVoidReturningFunctions: true },
53
+ ],
41
54
  '@typescript-eslint/no-explicit-any': ['error', { fixToUnknown: false, ignoreRestArgs: true }],
42
55
  '@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error',
43
56
  '@typescript-eslint/no-misused-promises': ['error', { checksVoidReturn: false }],
@@ -46,21 +59,33 @@ export default {
46
59
  '@typescript-eslint/no-unnecessary-condition': ['error', { allowConstantLoopConditions: true }],
47
60
  '@typescript-eslint/no-unnecessary-qualifier': 'error',
48
61
  '@typescript-eslint/prefer-enum-initializers': 'error',
62
+
49
63
  // 一个很常见的情况是在 Promise 中,我们得到了一个错误对象,知道它是 Error 但标注出的类型确是 unknown。
50
64
  // 如果我们选择用 reject(error as Error) 来抛出错误,@typescript-eslint/no-unnecessary-type-assertion 规则会报错,
51
65
  // 因为 reject 能接收任意类型,这个 `as Error` 被认为不必要。
52
66
  // 但不加 `as Error`,又会提示只能抛出 Error。
53
67
  // 所以这里干脆允许抛出 unknown。
68
+ // A common case occurs with Promises: we receive an error object that we know is an `Error`, but its declared type is `unknown`.
69
+ // If we use `reject(error as Error)`, the `@typescript-eslint/no-unnecessary-type-assertion` rule reports an error because `reject` accepts values of any type, making the `as Error` assertion unnecessary.
70
+ // However, without `as Error`, another rule complains that only `Error` objects may be thrown.
71
+ // Therefore, we simply allow `unknown` values to be thrown here.
54
72
  '@typescript-eslint/prefer-promise-reject-errors': ['error', { allowThrowingUnknown: true }],
73
+
55
74
  '@typescript-eslint/prefer-readonly': 'error',
56
75
  '@typescript-eslint/promise-function-async': 'error',
57
76
  '@typescript-eslint/require-array-sort-compare': ['error', { ignoreStringArrays: true }],
58
77
  '@typescript-eslint/restrict-template-expressions': ['error', { allowNumber: true }],
59
- '@typescript-eslint/strict-boolean-expressions': 'off', // 这个规则不能正确判断 object | null | boolean 的情况
78
+
79
+ // 这个规则不能正确判断 object | null | boolean 的情况
80
+ // This rule does not correctly handle the `object | null | boolean` case.
81
+ '@typescript-eslint/strict-boolean-expressions': 'off',
82
+
60
83
  '@typescript-eslint/switch-exhaustiveness-check': 'error',
61
- '@typescript-eslint/unbound-method': 'off', // 此规则对有些情况适配的不好
62
84
 
63
- // Extension Rules
85
+ // 此规则对有些情况适配的不好
86
+ // This rule does not handle certain cases well.
87
+ '@typescript-eslint/unbound-method': 'off',
88
+
64
89
  'default-param-last': 'off',
65
90
  '@typescript-eslint/default-param-last': 'error',
66
91
  '@typescript-eslint/no-empty-function': 'off',
@@ -83,7 +108,6 @@ export default {
83
108
  'no-useless-constructor': 'off',
84
109
  '@typescript-eslint/no-useless-constructor': 'error',
85
110
  '@typescript-eslint/return-await': 'error',
86
-
87
111
  'import-x/no-duplicates': ['error', { 'prefer-inline': true, considerQueryString: true }],
88
112
  },
89
113
  }