@foundbyte/eslint-plugin 1.1.1 → 1.1.2
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
|
@@ -24,7 +24,9 @@ module.exports = {
|
|
|
24
24
|
// 启用所有推荐的规则
|
|
25
25
|
'@foundbyte/arrow-function-padding': 'error',
|
|
26
26
|
'@foundbyte/no-initial-as-assertion': ['error', {
|
|
27
|
-
|
|
27
|
+
excludeVariableAssertions: true,
|
|
28
|
+
excludeTypePatterns: ['^const$'],
|
|
29
|
+
excludeMethodReturns: ['parse', 'querySelector']
|
|
28
30
|
}],
|
|
29
31
|
'@foundbyte/hooks-arrow-function-only': 'error',
|
|
30
32
|
'@foundbyte/type-comment-requirement': ['error', {
|
|
@@ -106,6 +108,11 @@ const parsed = JSON.parse(jsonString) as { name: string } // 不会报错
|
|
|
106
108
|
- 支持全局函数:`JSON.parse` 等
|
|
107
109
|
- 支持链式调用:`arr.map().filter() as Type`
|
|
108
110
|
|
|
111
|
+
- `excludeTypePatterns` (array, 默认 []): 排除匹配正则表达式模式的类型断言
|
|
112
|
+
- 支持正则表达式字符串数组
|
|
113
|
+
- 用于排除特定的类型断言模式,如 `as const`
|
|
114
|
+
- 示例:`['^const$', '^Readonly<.*>$']` 可以排除 `as const` 和 `as Readonly<...>`
|
|
115
|
+
|
|
109
116
|
**注意:**
|
|
110
117
|
- 该规则检查变量声明时的所有类型断言,包括嵌套在函数调用参数中的类型断言
|
|
111
118
|
- 推荐使用显式类型注解,因为类型断言会绕过 TypeScript 的类型检查
|
|
@@ -23,6 +23,14 @@ module.exports = {
|
|
|
23
23
|
},
|
|
24
24
|
default: [],
|
|
25
25
|
description: '排除特定方法返回类型的 as 断言'
|
|
26
|
+
},
|
|
27
|
+
excludeTypePatterns: {
|
|
28
|
+
type: 'array',
|
|
29
|
+
items: {
|
|
30
|
+
type: 'string'
|
|
31
|
+
},
|
|
32
|
+
default: [],
|
|
33
|
+
description: '排除匹配正则表达式模式的类型断言'
|
|
26
34
|
}
|
|
27
35
|
},
|
|
28
36
|
additionalProperties: false
|
|
@@ -37,6 +45,7 @@ module.exports = {
|
|
|
37
45
|
const options = context.options[0] || {};
|
|
38
46
|
const excludeVariableAssertions = options.excludeVariableAssertions || false;
|
|
39
47
|
const excludeMethodReturns = options.excludeMethodReturns || [];
|
|
48
|
+
const excludeTypePatterns = options.excludeTypePatterns || [];
|
|
40
49
|
|
|
41
50
|
/**
|
|
42
51
|
* 检查是否是变量断言的情况
|
|
@@ -45,9 +54,10 @@ module.exports = {
|
|
|
45
54
|
*/
|
|
46
55
|
function isVariableAssertion(node) {
|
|
47
56
|
// 检查是否是变量断言:const a = variable as Type
|
|
57
|
+
// 当 excludeVariableAssertions 为 true 时,返回 true 表示要排除
|
|
48
58
|
return node.expression &&
|
|
49
59
|
node.expression.type === 'Identifier' &&
|
|
50
|
-
|
|
60
|
+
excludeVariableAssertions;
|
|
51
61
|
}
|
|
52
62
|
|
|
53
63
|
/**
|
|
@@ -78,6 +88,45 @@ module.exports = {
|
|
|
78
88
|
return false;
|
|
79
89
|
}
|
|
80
90
|
|
|
91
|
+
/**
|
|
92
|
+
* 检查是否是 as const 断言
|
|
93
|
+
* @param {Object} node TSAsExpression 节点
|
|
94
|
+
* @returns {boolean}
|
|
95
|
+
*/
|
|
96
|
+
function isConstAssertion(node) {
|
|
97
|
+
// 检查是否是 as const 断言
|
|
98
|
+
return node.typeAnnotation &&
|
|
99
|
+
node.typeAnnotation.type === 'TSTypeReference' &&
|
|
100
|
+
node.typeAnnotation.typeName &&
|
|
101
|
+
node.typeAnnotation.typeName.name === 'const';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 检查是否匹配排除的类型模式
|
|
106
|
+
* @param {Object} node TSAsExpression 节点
|
|
107
|
+
* @returns {boolean}
|
|
108
|
+
*/
|
|
109
|
+
function matchesExcludedTypePattern(node) {
|
|
110
|
+
if (excludeTypePatterns.length === 0) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 获取类型注解的源代码
|
|
115
|
+
const sourceCode = context.getSourceCode();
|
|
116
|
+
const typeAnnotationText = sourceCode.getText(node.typeAnnotation);
|
|
117
|
+
|
|
118
|
+
// 检查是否匹配任何排除模式
|
|
119
|
+
return excludeTypePatterns.some(pattern => {
|
|
120
|
+
try {
|
|
121
|
+
const regex = new RegExp(pattern);
|
|
122
|
+
return regex.test(typeAnnotationText);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
// 如果正则表达式无效,忽略该模式
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
81
130
|
/**
|
|
82
131
|
* 检查变量声明是否使用了 as 类型断言
|
|
83
132
|
* @param {Object} node AST节点
|
|
@@ -104,6 +153,11 @@ module.exports = {
|
|
|
104
153
|
return;
|
|
105
154
|
}
|
|
106
155
|
|
|
156
|
+
// 排除匹配正则表达式模式的类型断言
|
|
157
|
+
if (matchesExcludedTypePattern(declaration.init)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
107
161
|
// 对于直接的类型断言,应该限制
|
|
108
162
|
// 例如:const a = 1 as number, const a = {} as { s: string }
|
|
109
163
|
context.report({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foundbyte/eslint-plugin",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
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",
|