mm_eslint 1.5.1 → 1.5.3
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 +27 -0
- package/lib/detector/variable_name.js +42 -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
|
// 检测常量名
|
|
@@ -205,6 +227,11 @@ ConstName.prototype._isConstantValue = function (value_node) {
|
|
|
205
227
|
return false; // await表达式的结果通常是可变的,不是常量
|
|
206
228
|
}
|
|
207
229
|
|
|
230
|
+
// 排除条件表达式(三元运算符)
|
|
231
|
+
if (value_node.type === 'ConditionalExpression') {
|
|
232
|
+
return false; // 条件表达式的结果在运行时确定,不是常量
|
|
233
|
+
}
|
|
234
|
+
|
|
208
235
|
// 默认情况下,如果不是排除的类型,就认为是常量
|
|
209
236
|
return true;
|
|
210
237
|
}
|
|
@@ -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,直接处理
|
|
@@ -121,6 +134,18 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
121
134
|
this.report(node, error, 'variable');
|
|
122
135
|
return error;
|
|
123
136
|
}
|
|
137
|
+
|
|
138
|
+
// 对于ConditionalExpression(条件表达式/三元运算符),直接处理
|
|
139
|
+
else if (decl.init.type === 'ConditionalExpression') {
|
|
140
|
+
let error = {
|
|
141
|
+
message: `变量'${name}'应该使用let标签声明而不是const标签声明`,
|
|
142
|
+
fix: function(fixer) {
|
|
143
|
+
return fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let');
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
this.report(node, error, 'variable');
|
|
147
|
+
return error;
|
|
148
|
+
}
|
|
124
149
|
}
|
|
125
150
|
// 对于const声明的其他情况,跳过变量规则检测(由常量规则处理)
|
|
126
151
|
return;
|
|
@@ -129,32 +154,6 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
129
154
|
}
|
|
130
155
|
}
|
|
131
156
|
|
|
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
157
|
/**
|
|
159
158
|
* 属性节点检测函数(变量属性)
|
|
160
159
|
* @param {Object} node - 属性节点
|
|
@@ -338,6 +337,23 @@ VariableName.prototype.ExportNamedDeclaration = function (node) {
|
|
|
338
337
|
}
|
|
339
338
|
}
|
|
340
339
|
|
|
340
|
+
/**
|
|
341
|
+
* 判断节点是否在for...of或for...in循环中
|
|
342
|
+
* @param {Object} node - AST节点
|
|
343
|
+
* @returns {boolean} 是否在for...of或for...in循环中
|
|
344
|
+
*/
|
|
345
|
+
VariableName.prototype._isInForLoop = function (node) {
|
|
346
|
+
let current_node = node;
|
|
347
|
+
while (current_node.parent) {
|
|
348
|
+
// 检查父节点是否为ForOfStatement或ForInStatement
|
|
349
|
+
if (current_node.parent.type === 'ForOfStatement' || current_node.parent.type === 'ForInStatement') {
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
current_node = current_node.parent;
|
|
353
|
+
}
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
|
|
341
357
|
module.exports = {
|
|
342
358
|
VariableName
|
|
343
359
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_eslint",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
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": [
|