@foundbyte/eslint-plugin 1.1.0 → 1.1.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/README.md CHANGED
@@ -23,7 +23,9 @@ module.exports = {
23
23
  rules: {
24
24
  // 启用所有推荐的规则
25
25
  '@foundbyte/arrow-function-padding': 'error',
26
- '@foundbyte/no-initial-as-assertion': 'error',
26
+ '@foundbyte/no-initial-as-assertion': ['error', {
27
+ 'excludeMethodReturns': ['map', 'parse', 'filter', 'reduce','querySelector']
28
+ }],
27
29
  '@foundbyte/hooks-arrow-function-only': 'error',
28
30
  '@foundbyte/type-comment-requirement': ['error', {
29
31
  minCommentsPerGroup: 1,
@@ -55,7 +57,14 @@ module.exports = {
55
57
 
56
58
  **配置示例:**
57
59
  ```javascript
58
- '@foundbyte/no-initial-as-assertion': 'error' // 禁止在变量声明时使用 as 类型断言
60
+ // 基本用法
61
+ '@foundbyte/no-initial-as-assertion': 'error'
62
+
63
+ // 高级配置 - 排除特定情况
64
+ '@foundbyte/no-initial-as-assertion': ['error', {
65
+ excludeVariableAssertions: true, // 排除对变量的 as 断言
66
+ excludeMethodReturns: ['map', 'parse'] // 排除特定方法返回类型的 as 断言
67
+ }]
59
68
  ```
60
69
 
61
70
  **示例:**
@@ -71,12 +80,36 @@ const a = ref() as Ref<string>
71
80
  const b = computed(() => {}) as ComputedRef<number>
72
81
  const c = reactive({}) as Reactive<object>
73
82
  const d = ref({} as { a: string }) // 嵌套的类型断言也会被检测到
83
+
84
+ // ✅ 当 excludeVariableAssertions 为 true 时 - 变量断言被排除
85
+ interface ID { a?: string, b?: string }
86
+ const d: ID = {}
87
+ const e = d as { a?: string } // 不会报错
88
+
89
+ // ✅ 当 excludeMethodReturns 包含 'map' 时 - 数组方法返回类型断言被排除
90
+ const arr = [1, 2, 3]
91
+ const mapped = arr.map(x => x * 2) as number[] // 不会报错
92
+
93
+ // ✅ 当 excludeMethodReturns 包含 'parse' 时 - JSON.parse 返回类型断言被排除
94
+ const jsonString = '{"name": "test"}'
95
+ const parsed = JSON.parse(jsonString) as { name: string } // 不会报错
74
96
  ```
75
97
 
98
+ **配置选项:**
99
+
100
+ - `excludeVariableAssertions` (boolean, 默认 false): 是否排除对变量的 as 断言
101
+ - 当为 true 时,类似 `const a = variable as Type` 的情况不会被报错
102
+ - 适用于变量到变量的类型断言场景
103
+
104
+ - `excludeMethodReturns` (array, 默认 []): 排除特定方法返回类型的 as 断言
105
+ - 支持数组方法:`map`, `filter`, `reduce` 等
106
+ - 支持全局函数:`JSON.parse` 等
107
+ - 支持链式调用:`arr.map().filter() as Type`
108
+
76
109
  **注意:**
77
110
  - 该规则检查变量声明时的所有类型断言,包括嵌套在函数调用参数中的类型断言
78
111
  - 推荐使用显式类型注解,因为类型断言会绕过 TypeScript 的类型检查
79
- - 对于函数调用返回值的类型断言,该规则不会报错
112
+ - 对于函数调用返回值的类型断言,可以通过配置选项排除特定情况
80
113
 
81
114
  ### arrow-function-padding
82
115
 
@@ -1,6 +1,18 @@
1
1
  import { Rule } from 'eslint'
2
2
 
3
- export interface NoInitialAsAssertionOptions {}
3
+ export interface NoInitialAsAssertionOptions {
4
+ /**
5
+ * 是否排除对变量的 as 断言
6
+ * @default false
7
+ */
8
+ excludeVariableAssertions?: boolean
9
+
10
+ /**
11
+ * 排除特定方法返回类型的 as 断言
12
+ * @default []
13
+ */
14
+ excludeMethodReturns?: string[]
15
+ }
4
16
 
5
17
  /**
6
18
  * 禁止在初始变量定义时使用 as 类型断言
@@ -7,13 +7,77 @@ module.exports = {
7
7
  recommended: false,
8
8
  },
9
9
  fixable: null,
10
- schema: [],
10
+ schema: [
11
+ {
12
+ type: 'object',
13
+ properties: {
14
+ excludeVariableAssertions: {
15
+ type: 'boolean',
16
+ default: false,
17
+ description: '是否排除对变量的 as 断言'
18
+ },
19
+ excludeMethodReturns: {
20
+ type: 'array',
21
+ items: {
22
+ type: 'string'
23
+ },
24
+ default: [],
25
+ description: '排除特定方法返回类型的 as 断言'
26
+ }
27
+ },
28
+ additionalProperties: false
29
+ }
30
+ ],
11
31
  messages: {
12
32
  noInitialAsAssertion: '禁止在初始变量定义时使用 as 类型断言,请使用显式类型注解',
13
33
  },
14
34
  },
15
35
 
16
36
  create(context) {
37
+ const options = context.options[0] || {};
38
+ const excludeVariableAssertions = options.excludeVariableAssertions || false;
39
+ const excludeMethodReturns = options.excludeMethodReturns || [];
40
+
41
+ /**
42
+ * 检查是否是变量断言的情况
43
+ * @param {Object} node TSAsExpression 节点
44
+ * @returns {boolean}
45
+ */
46
+ function isVariableAssertion(node) {
47
+ // 检查是否是变量断言:const a = variable as Type
48
+ return node.expression &&
49
+ node.expression.type === 'Identifier' &&
50
+ !excludeVariableAssertions;
51
+ }
52
+
53
+ /**
54
+ * 检查是否是排除的方法返回类型断言
55
+ * @param {Object} node TSAsExpression 节点
56
+ * @returns {boolean}
57
+ */
58
+ function isExcludedMethodReturn(node) {
59
+ if (!node.expression || node.expression.type !== 'CallExpression') {
60
+ return false;
61
+ }
62
+
63
+ const callExpression = node.expression;
64
+ if (callExpression.callee && callExpression.callee.type === 'MemberExpression') {
65
+ // 处理数组方法如 arr.map().filter() as Type
66
+ const methodName = callExpression.callee.property && callExpression.callee.property.name;
67
+ if (methodName && excludeMethodReturns.includes(methodName)) {
68
+ return true;
69
+ }
70
+ } else if (callExpression.callee && callExpression.callee.type === 'Identifier') {
71
+ // 处理全局函数如 JSON.parse() as Type
72
+ const functionName = callExpression.callee.name;
73
+ if (functionName && excludeMethodReturns.includes(functionName)) {
74
+ return true;
75
+ }
76
+ }
77
+
78
+ return false;
79
+ }
80
+
17
81
  /**
18
82
  * 检查变量声明是否使用了 as 类型断言
19
83
  * @param {Object} node AST节点
@@ -30,6 +94,16 @@ module.exports = {
30
94
  if (declaration.init) {
31
95
  // 检查初始化表达式是否直接是类型断言
32
96
  if (declaration.init.type === 'TSAsExpression') {
97
+ // 排除变量断言的情况
98
+ if (isVariableAssertion(declaration.init)) {
99
+ return;
100
+ }
101
+
102
+ // 排除特定方法返回类型的断言
103
+ if (isExcludedMethodReturn(declaration.init)) {
104
+ return;
105
+ }
106
+
33
107
  // 对于直接的类型断言,应该限制
34
108
  // 例如:const a = 1 as number, const a = {} as { s: string }
35
109
  context.report({
@@ -43,6 +117,16 @@ module.exports = {
43
117
  if (declaration.init.arguments && declaration.init.arguments.length > 0) {
44
118
  for (const arg of declaration.init.arguments) {
45
119
  if (arg.type === 'TSAsExpression') {
120
+ // 排除变量断言的情况
121
+ if (isVariableAssertion(arg)) {
122
+ continue;
123
+ }
124
+
125
+ // 排除特定方法返回类型的断言
126
+ if (isExcludedMethodReturn(arg)) {
127
+ continue;
128
+ }
129
+
46
130
  // 对于函数参数中的类型断言,应该限制
47
131
  // 例如:ref({} as string) 中的 {} as string
48
132
  context.report({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foundbyte/eslint-plugin",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "ESLint plugin for enforcing code style rules including arrow function padding, Vue script lines limit, and TypeScript type comment requirements",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",