mm_eslint 1.5.3 → 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) {
|
|
@@ -590,6 +597,19 @@ ConstName.prototype.ExportNamedDeclaration = function (node) {
|
|
|
590
597
|
}
|
|
591
598
|
}
|
|
592
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
|
+
|
|
593
613
|
module.exports = {
|
|
594
614
|
ConstName
|
|
595
615
|
};
|
|
@@ -147,6 +147,20 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
147
147
|
return error;
|
|
148
148
|
}
|
|
149
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
|
+
|
|
150
164
|
// 对于const声明的其他情况,跳过变量规则检测(由常量规则处理)
|
|
151
165
|
return;
|
|
152
166
|
}
|
|
@@ -354,6 +368,42 @@ VariableName.prototype._isInForLoop = function (node) {
|
|
|
354
368
|
return false;
|
|
355
369
|
}
|
|
356
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
|
+
|
|
357
407
|
module.exports = {
|
|
358
408
|
VariableName
|
|
359
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": [
|