@foundbyte/eslint-plugin 1.0.8 → 1.0.9

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,6 +23,7 @@ module.exports = {
23
23
  rules: {
24
24
  // 启用所有推荐的规则
25
25
  '@foundbyte/arrow-function-padding': 'error',
26
+ '@foundbyte/no-initial-as-assertion': 'error',
26
27
  '@foundbyte/hooks-arrow-function-only': 'error',
27
28
  '@foundbyte/type-comment-requirement': ['error', {
28
29
  minCommentsPerGroup: 1,
@@ -48,6 +49,35 @@ module.exports = {
48
49
 
49
50
  ## 规则列表与配置示例
50
51
 
52
+ ### no-initial-as-assertion
53
+
54
+ 禁止在初始变量定义时使用 `as` 类型断言,强制使用显式类型注解。
55
+
56
+ **配置示例:**
57
+ ```javascript
58
+ '@foundbyte/no-initial-as-assertion': 'error' // 禁止在变量声明时使用 as 类型断言
59
+ ```
60
+
61
+ **示例:**
62
+ ```typescript
63
+ // ✅ 正确 - 使用显式类型注解
64
+ const a: Ref<string> = ref()
65
+ const b: ComputedRef<number> = computed(() => {})
66
+ const c = ref() // 没有类型断言
67
+ const d: Ref<{ a: string }> = ref({}) // 使用显式类型注解
68
+
69
+ // ❌ 错误 - 在变量声明时使用 as 类型断言
70
+ const a = ref() as Ref<string>
71
+ const b = computed(() => {}) as ComputedRef<number>
72
+ const c = reactive({}) as Reactive<object>
73
+ const d = ref({} as { a: string }) // 嵌套的类型断言也会被检测到
74
+ ```
75
+
76
+ **注意:**
77
+ - 该规则检查变量声明时的所有类型断言,包括嵌套在函数调用参数中的类型断言
78
+ - 推荐使用显式类型注解,因为类型断言会绕过 TypeScript 的类型检查
79
+ - 对于函数调用返回值的类型断言,该规则不会报错
80
+
51
81
  ### arrow-function-padding
52
82
 
53
83
  要求函数体之间必须空行。
@@ -14,6 +14,44 @@ 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
+
17
55
  /**
18
56
  * 检查变量声明是否使用了 as 类型断言
19
57
  * @param {Object} node AST节点
@@ -28,8 +66,8 @@ module.exports = {
28
66
  node.declarations.forEach(declaration => {
29
67
  // 检查是否有初始化表达式
30
68
  if (declaration.init) {
31
- // 检查初始化表达式是否是类型断言表达式
32
- if (declaration.init.type === 'TSAsExpression') {
69
+ // 检查初始化表达式是否包含类型断言
70
+ if (containsTypeAssertion(declaration.init)) {
33
71
  context.report({
34
72
  node: declaration,
35
73
  messageId: 'noInitialAsAssertion',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foundbyte/eslint-plugin",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
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",