mm_eslint 1.5.1 → 1.5.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/lib/detector/const_name.js +22 -0
- package/lib/detector/variable_name.js +30 -26
- package/package.json +1 -1
|
@@ -10,6 +10,23 @@ class ConstName extends Name {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* 判断节点是否在for...of或for...in循环中
|
|
15
|
+
* @param {Object} node - AST节点
|
|
16
|
+
* @returns {boolean} 是否在for...of或for...in循环中
|
|
17
|
+
*/
|
|
18
|
+
ConstName.prototype._isInForOfLoop = function (node) {
|
|
19
|
+
let current_node = node;
|
|
20
|
+
while (current_node.parent) {
|
|
21
|
+
// 检查父节点是否为ForOfStatement或ForInStatement
|
|
22
|
+
if (current_node.parent.type === 'ForOfStatement' || current_node.parent.type === 'ForInStatement') {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
current_node = current_node.parent;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
13
30
|
/**
|
|
14
31
|
* 变量声明节点检测函数(常量声明)
|
|
15
32
|
* @param {Object} node - 变量声明节点
|
|
@@ -46,6 +63,11 @@ ConstName.prototype.VariableDeclaration = function (node) {
|
|
|
46
63
|
continue;
|
|
47
64
|
}
|
|
48
65
|
|
|
66
|
+
// 排除for...of和for...in循环中的const声明
|
|
67
|
+
if (this._isInForOfLoop(node)) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
49
71
|
// 判断是否为常量(包括 Object.freeze() 等情况)
|
|
50
72
|
if (this._isConstantValue(decl.init)) {
|
|
51
73
|
// 检测常量名
|
|
@@ -79,6 +79,19 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
79
79
|
// 检测变量名
|
|
80
80
|
let name = decl.id.name;
|
|
81
81
|
|
|
82
|
+
// 检查是否在for...of循环中
|
|
83
|
+
if (this._isInForLoop(node)) {
|
|
84
|
+
// 在for...of循环中的const声明应该使用let
|
|
85
|
+
let error = {
|
|
86
|
+
message: `变量'${name}'应该使用let标签声明而不是const标签声明`,
|
|
87
|
+
fix: function(fixer) {
|
|
88
|
+
return fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let');
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
this.report(node, error, 'variable');
|
|
92
|
+
return error;
|
|
93
|
+
}
|
|
94
|
+
|
|
82
95
|
// 对于const声明,处理右边是CallExpression或AwaitExpression的情况
|
|
83
96
|
if (decl.init) {
|
|
84
97
|
// 对于AwaitExpression,直接处理
|
|
@@ -129,32 +142,6 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
129
142
|
}
|
|
130
143
|
}
|
|
131
144
|
|
|
132
|
-
// /**
|
|
133
|
-
// * 标识符引用节点检测函数(变量引用)
|
|
134
|
-
// * @param {Object} node - 标识符节点
|
|
135
|
-
// * @returns {Object|undefined} - 检测结果对象或undefined
|
|
136
|
-
// */
|
|
137
|
-
// VariableName.prototype.Identifier = function (node) {
|
|
138
|
-
// // 检测标识符引用(如表达式中的 Name、Age)
|
|
139
|
-
// if (node.type === 'Identifier') {
|
|
140
|
-
// let name = node.name;
|
|
141
|
-
|
|
142
|
-
// // 使用精确的类型识别逻辑
|
|
143
|
-
// let original_type = this._getOriginalType(node, 'Identifier');
|
|
144
|
-
|
|
145
|
-
// // 只有当确实是变量引用时才进行变量命名规则检测
|
|
146
|
-
// if (original_type === 'variable' || original_type === 'unknown') {
|
|
147
|
-
// let error = this.check(node, name, original_type);
|
|
148
|
-
|
|
149
|
-
// // 如果检测到错误,报告错误
|
|
150
|
-
// if (error) {
|
|
151
|
-
// this.report(node, error, original_type);
|
|
152
|
-
// return error;
|
|
153
|
-
// }
|
|
154
|
-
// }
|
|
155
|
-
// }
|
|
156
|
-
// };
|
|
157
|
-
|
|
158
145
|
/**
|
|
159
146
|
* 属性节点检测函数(变量属性)
|
|
160
147
|
* @param {Object} node - 属性节点
|
|
@@ -338,6 +325,23 @@ VariableName.prototype.ExportNamedDeclaration = function (node) {
|
|
|
338
325
|
}
|
|
339
326
|
}
|
|
340
327
|
|
|
328
|
+
/**
|
|
329
|
+
* 判断节点是否在for...of或for...in循环中
|
|
330
|
+
* @param {Object} node - AST节点
|
|
331
|
+
* @returns {boolean} 是否在for...of或for...in循环中
|
|
332
|
+
*/
|
|
333
|
+
VariableName.prototype._isInForLoop = function (node) {
|
|
334
|
+
let current_node = node;
|
|
335
|
+
while (current_node.parent) {
|
|
336
|
+
// 检查父节点是否为ForOfStatement或ForInStatement
|
|
337
|
+
if (current_node.parent.type === 'ForOfStatement' || current_node.parent.type === 'ForInStatement') {
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
current_node = current_node.parent;
|
|
341
|
+
}
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
|
|
341
345
|
module.exports = {
|
|
342
346
|
VariableName
|
|
343
347
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_eslint",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "ESLint plugin for personal naming conventions - supports PascalCase, camelCase, snake_case, and UPPER_SNAKE_CASE naming rules with intelligent recommendations",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"keywords": [
|