@foundbyte/eslint-plugin 1.0.8 → 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.
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
  要求函数体之间必须空行。
@@ -28,13 +28,32 @@ module.exports = {
28
28
  node.declarations.forEach(declaration => {
29
29
  // 检查是否有初始化表达式
30
30
  if (declaration.init) {
31
- // 检查初始化表达式是否是类型断言表达式
31
+ // 检查初始化表达式是否直接是类型断言
32
32
  if (declaration.init.type === 'TSAsExpression') {
33
+ // 对于直接的类型断言,应该限制
34
+ // 例如:const a = 1 as number, const a = {} as { s: string }
33
35
  context.report({
34
- node: declaration,
36
+ node: declaration.init,
35
37
  messageId: 'noInitialAsAssertion',
36
38
  });
37
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
+ }
38
57
  }
39
58
  });
40
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foundbyte/eslint-plugin",
3
- "version": "1.0.8",
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",