mm_eslint 1.5.3 → 1.5.5

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.
@@ -68,12 +68,24 @@ ConstName.prototype.VariableDeclaration = function (node) {
68
68
  continue;
69
69
  }
70
70
 
71
+ // 排除require()调用,因为导入的模块类型无法确定
72
+ if (this._isRequireCall(decl.init)) {
73
+ continue;
74
+ }
75
+
71
76
  // 判断是否为常量(包括 Object.freeze() 等情况)
72
77
  if (this._isConstantValue(decl.init)) {
73
78
  // 检测常量名
74
79
  let name = decl.id.name;
75
80
  let original_type = 'constant';
76
81
  let error = this.check(node, name, original_type);
82
+
83
+ // 如果常量名检测有错误,但符合变量命名规则,则排除此错误
84
+ if (error && this._isValidVariableName(name)) {
85
+ // 符合变量命名规则,跳过常量检测,由变量检测器处理
86
+ continue;
87
+ }
88
+
77
89
  this.report(node, error, original_type);
78
90
 
79
91
  if (error) {
@@ -590,6 +602,60 @@ ConstName.prototype.ExportNamedDeclaration = function (node) {
590
602
  }
591
603
  }
592
604
 
605
+ /**
606
+ * 判断节点是否为require()调用
607
+ * @param {Object} node - AST节点
608
+ * @returns {boolean} 是否为require()调用
609
+ */
610
+ ConstName.prototype._isRequireCall = function (node) {
611
+ if (!node) {
612
+ return false;
613
+ }
614
+
615
+ // 情况1:直接的require()调用,如 require('module')
616
+ if (node.type === 'CallExpression' &&
617
+ node.callee && node.callee.type === 'Identifier' &&
618
+ node.callee.name === 'require') {
619
+ return true;
620
+ }
621
+
622
+ // 情况2:require().property,如 require('module').property
623
+ if (node.type === 'MemberExpression' &&
624
+ node.object && node.object.type === 'CallExpression' &&
625
+ node.object.callee && node.object.callee.type === 'Identifier' &&
626
+ node.object.callee.name === 'require') {
627
+ return true;
628
+ }
629
+
630
+ // 情况3:require('module')[property],如 require('module')['property']
631
+ if (node.type === 'MemberExpression' &&
632
+ node.object && node.object.type === 'CallExpression' &&
633
+ node.object.callee && node.object.callee.type === 'Identifier' &&
634
+ node.object.callee.name === 'require') {
635
+ return true;
636
+ }
637
+
638
+ return false;
639
+ }
640
+
641
+ /**
642
+ * 判断名称是否符合变量命名规则
643
+ * @param {string} name - 名称
644
+ * @returns {boolean} 是否符合变量命名规则
645
+ */
646
+ ConstName.prototype._isValidVariableName = function (name) {
647
+ // 变量命名规则:小写蛇形命名法 (snake_case)
648
+ const variable_regex = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/;
649
+
650
+ // 检查名称长度(1-20字符)
651
+ if (name.length < 1 || name.length > 20) {
652
+ return false;
653
+ }
654
+
655
+ // 检查是否符合变量命名风格
656
+ return variable_regex.test(name);
657
+ }
658
+
593
659
  module.exports = {
594
660
  ConstName
595
661
  };
@@ -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",
3
+ "version": "1.5.5",
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": [