@foundbyte/eslint-plugin 1.0.9 → 1.1.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.
@@ -14,44 +14,6 @@ module.exports = {
14
14
  },
15
15
 
16
16
  create(context) {
17
- /**
18
- * 递归检查 AST 节点中是否包含类型断言
19
- * @param {Object} node AST节点
20
- * @param {Set} visited 已访问的节点集合,用于避免循环引用
21
- * @returns {boolean} 是否包含类型断言
22
- */
23
- function containsTypeAssertion(node, visited = new Set()) {
24
- if (!node || typeof node !== 'object') return false;
25
-
26
- // 避免循环引用
27
- if (visited.has(node)) return false;
28
- visited.add(node);
29
-
30
- // 如果当前节点是类型断言,返回 true
31
- if (node.type === 'TSAsExpression') {
32
- return true;
33
- }
34
-
35
- // 递归检查子节点,只检查 AST 相关的属性
36
- const astKeys = ['init', 'expression', 'body', 'declarations', 'arguments', 'callee', 'left', 'right'];
37
-
38
- for (const key of astKeys) {
39
- if (node[key]) {
40
- if (Array.isArray(node[key])) {
41
- for (const child of node[key]) {
42
- if (containsTypeAssertion(child, visited)) {
43
- return true;
44
- }
45
- }
46
- } else if (containsTypeAssertion(node[key], visited)) {
47
- return true;
48
- }
49
- }
50
- }
51
-
52
- return false;
53
- }
54
-
55
17
  /**
56
18
  * 检查变量声明是否使用了 as 类型断言
57
19
  * @param {Object} node AST节点
@@ -66,13 +28,32 @@ module.exports = {
66
28
  node.declarations.forEach(declaration => {
67
29
  // 检查是否有初始化表达式
68
30
  if (declaration.init) {
69
- // 检查初始化表达式是否包含类型断言
70
- if (containsTypeAssertion(declaration.init)) {
31
+ // 检查初始化表达式是否直接是类型断言
32
+ if (declaration.init.type === 'TSAsExpression') {
33
+ // 对于直接的类型断言,应该限制
34
+ // 例如:const a = 1 as number, const a = {} as { s: string }
71
35
  context.report({
72
- node: declaration,
36
+ node: declaration.init,
73
37
  messageId: 'noInitialAsAssertion',
74
38
  });
75
39
  }
40
+ // 检查函数调用的参数中是否包含类型断言
41
+ else if (declaration.init.type === 'CallExpression') {
42
+ // 检查函数调用的参数
43
+ if (declaration.init.arguments && declaration.init.arguments.length > 0) {
44
+ for (const arg of declaration.init.arguments) {
45
+ if (arg.type === 'TSAsExpression') {
46
+ // 对于函数参数中的类型断言,应该限制
47
+ // 例如:ref({} as string) 中的 {} as string
48
+ context.report({
49
+ node: arg,
50
+ messageId: 'noInitialAsAssertion',
51
+ });
52
+ break; // 只报告第一个找到的类型断言
53
+ }
54
+ }
55
+ }
56
+ }
76
57
  }
77
58
  });
78
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foundbyte/eslint-plugin",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
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",