mm_eslint 1.5.2 → 1.5.4
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.
|
@@ -74,6 +74,13 @@ ConstName.prototype.VariableDeclaration = function (node) {
|
|
|
74
74
|
let name = decl.id.name;
|
|
75
75
|
let original_type = 'constant';
|
|
76
76
|
let error = this.check(node, name, original_type);
|
|
77
|
+
|
|
78
|
+
// 如果常量名检测有错误,但符合变量命名规则,则排除此错误
|
|
79
|
+
if (error && this._isValidVariableName(name)) {
|
|
80
|
+
// 符合变量命名规则,跳过常量检测,由变量检测器处理
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
77
84
|
this.report(node, error, original_type);
|
|
78
85
|
|
|
79
86
|
if (error) {
|
|
@@ -227,6 +234,11 @@ ConstName.prototype._isConstantValue = function (value_node) {
|
|
|
227
234
|
return false; // await表达式的结果通常是可变的,不是常量
|
|
228
235
|
}
|
|
229
236
|
|
|
237
|
+
// 排除条件表达式(三元运算符)
|
|
238
|
+
if (value_node.type === 'ConditionalExpression') {
|
|
239
|
+
return false; // 条件表达式的结果在运行时确定,不是常量
|
|
240
|
+
}
|
|
241
|
+
|
|
230
242
|
// 默认情况下,如果不是排除的类型,就认为是常量
|
|
231
243
|
return true;
|
|
232
244
|
}
|
|
@@ -585,6 +597,19 @@ ConstName.prototype.ExportNamedDeclaration = function (node) {
|
|
|
585
597
|
}
|
|
586
598
|
}
|
|
587
599
|
|
|
600
|
+
/**
|
|
601
|
+
* 判断名称是否符合变量命名规则
|
|
602
|
+
* @param {string} name - 名称
|
|
603
|
+
* @returns {boolean} 是否符合变量命名规则
|
|
604
|
+
*/
|
|
605
|
+
ConstName.prototype._isValidVariableName = function (name) {
|
|
606
|
+
// 变量命名规则:小写蛇形命名法 (snake_case)
|
|
607
|
+
const variable_regex = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/;
|
|
608
|
+
|
|
609
|
+
// 检查是否符合变量命名风格
|
|
610
|
+
return variable_regex.test(name);
|
|
611
|
+
}
|
|
612
|
+
|
|
588
613
|
module.exports = {
|
|
589
614
|
ConstName
|
|
590
615
|
};
|
|
@@ -134,7 +134,33 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
134
134
|
this.report(node, error, 'variable');
|
|
135
135
|
return error;
|
|
136
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
|
+
}
|
|
137
149
|
}
|
|
150
|
+
|
|
151
|
+
// 新增:检测const声明但符合变量命名规则的情况
|
|
152
|
+
// 如果名称符合变量命名规则(小写蛇形命名法),则提示使用let
|
|
153
|
+
if (this._isValidVariableName(name) && !this._isValidConstantName(name)) {
|
|
154
|
+
let error = {
|
|
155
|
+
message: `变量'${name}'应该使用let标签声明而不是const标签声明`,
|
|
156
|
+
fix: function(fixer) {
|
|
157
|
+
return fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let');
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
this.report(node, error, 'variable');
|
|
161
|
+
return error;
|
|
162
|
+
}
|
|
163
|
+
|
|
138
164
|
// 对于const声明的其他情况,跳过变量规则检测(由常量规则处理)
|
|
139
165
|
return;
|
|
140
166
|
}
|
|
@@ -342,6 +368,42 @@ VariableName.prototype._isInForLoop = function (node) {
|
|
|
342
368
|
return false;
|
|
343
369
|
}
|
|
344
370
|
|
|
371
|
+
/**
|
|
372
|
+
* 判断名称是否符合变量命名规则
|
|
373
|
+
* @param {string} name - 名称
|
|
374
|
+
* @returns {boolean} 是否符合变量命名规则
|
|
375
|
+
*/
|
|
376
|
+
VariableName.prototype._isValidVariableName = function (name) {
|
|
377
|
+
// 变量命名规则:小写蛇形命名法 (snake_case)
|
|
378
|
+
const variable_regex = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/;
|
|
379
|
+
|
|
380
|
+
// 检查名称长度(1-20字符)
|
|
381
|
+
if (name.length < 1 || name.length > 20) {
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// 检查是否符合变量命名风格
|
|
386
|
+
return variable_regex.test(name);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* 判断名称是否符合常量命名规则
|
|
391
|
+
* @param {string} name - 名称
|
|
392
|
+
* @returns {boolean} 是否符合常量命名规则
|
|
393
|
+
*/
|
|
394
|
+
VariableName.prototype._isValidConstantName = function (name) {
|
|
395
|
+
// 常量命名规则:大写蛇形命名法 (UPPER_SNAKE_CASE)
|
|
396
|
+
const constant_regex = /^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$/;
|
|
397
|
+
|
|
398
|
+
// 检查名称长度(1-20字符)
|
|
399
|
+
if (name.length < 1 || name.length > 20) {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 检查是否符合常量命名风格
|
|
404
|
+
return constant_regex.test(name);
|
|
405
|
+
}
|
|
406
|
+
|
|
345
407
|
module.exports = {
|
|
346
408
|
VariableName
|
|
347
409
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_eslint",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.4",
|
|
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": [
|