mm_eslint 1.4.9 → 1.5.1

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.
@@ -200,6 +200,11 @@ ConstName.prototype._isConstantValue = function (value_node) {
200
200
  return false; // 其他函数调用不是常量
201
201
  }
202
202
 
203
+ // 排除await表达式
204
+ if (value_node.type === 'AwaitExpression') {
205
+ return false; // await表达式的结果通常是可变的,不是常量
206
+ }
207
+
203
208
  // 默认情况下,如果不是排除的类型,就认为是常量
204
209
  return true;
205
210
  }
@@ -500,10 +505,28 @@ ConstName.prototype.ExportNamedDeclaration = function (node) {
500
505
  for (let i = 0; i < node.declaration.declarations.length; i++) {
501
506
  let declaration = node.declaration.declarations[i];
502
507
  if (declaration.id && declaration.id.type === 'Identifier') {
503
- let name = declaration.id.name;
504
- let error = this.check(node, name, 'export-constant');
505
- this.report(node, error, 'export-constant');
506
- return error;
508
+ // 排除函数:如果右边是函数表达式或箭头函数,则不是常量
509
+ if (declaration.init && (declaration.init.type === 'FunctionExpression' || declaration.init.type === 'ArrowFunctionExpression')) {
510
+ return;
511
+ }
512
+
513
+ // 排除类:如果右边是类表达式,则不是常量
514
+ if (declaration.init && declaration.init.type === 'ClassExpression') {
515
+ return;
516
+ }
517
+
518
+ // 排除类实例:如果右边是 new 表达式,则不是常量
519
+ if (declaration.init && declaration.init.type === 'NewExpression') {
520
+ return;
521
+ }
522
+
523
+ // 判断是否为常量(包括 Object.freeze() 等情况)
524
+ if (this._isConstantValue(declaration.init)) {
525
+ let name = declaration.id.name;
526
+ let error = this.check(node, name, 'export-constant');
527
+ this.report(node, error, 'export-constant');
528
+ return error;
529
+ }
507
530
  }
508
531
  }
509
532
  }
@@ -16,8 +16,9 @@ class VariableName extends Name {
16
16
  * @returns {Object|undefined} - 检测结果对象或undefined
17
17
  */
18
18
  VariableName.prototype.VariableDeclaration = function (node) {
19
- // 检测变量声明(let、var 和需要修正的const)
20
- if (node.kind === 'let' || node.kind === 'var' || node.kind === 'const') {
19
+ // 检测变量声明
20
+ if (node.kind === 'let' || node.kind === 'var') {
21
+ // 处理let和var声明
21
22
  for (let i = 0; i < node.declarations.length; i++) {
22
23
  let decl = node.declarations[i];
23
24
  if (decl.id && decl.id.type === 'Identifier') {
@@ -54,13 +55,34 @@ VariableName.prototype.VariableDeclaration = function (node) {
54
55
  // 检测变量名
55
56
  let name = decl.id.name;
56
57
 
58
+ // 对于let和var声明,正常进行变量命名检测
57
59
  // 使用精确的类型识别逻辑
58
60
  let original_type = this._getOriginalType(decl, 'VariableDeclaration');
59
61
 
60
- // 对于const声明,检查是否需要改为let
61
- if (node.kind === 'const') {
62
- // 如果右边是函数调用,应该使用let而不是const
63
- if (decl.init && decl.init.type === 'CallExpression') {
62
+ // 只有当确实是变量时才进行变量命名规则检测
63
+ if (original_type === 'variable' || original_type === 'unknown') {
64
+ let error = this.check(node, name, original_type);
65
+
66
+ // 如果检测到错误,报告错误
67
+ if (error) {
68
+ this.report(node, error, original_type);
69
+ return error;
70
+ }
71
+ }
72
+ }
73
+ }
74
+ } else if (node.kind === 'const') {
75
+ // 单独处理const声明
76
+ for (let i = 0; i < node.declarations.length; i++) {
77
+ let decl = node.declarations[i];
78
+ if (decl.id && decl.id.type === 'Identifier') {
79
+ // 检测变量名
80
+ let name = decl.id.name;
81
+
82
+ // 对于const声明,处理右边是CallExpression或AwaitExpression的情况
83
+ if (decl.init) {
84
+ // 对于AwaitExpression,直接处理
85
+ if (decl.init.type === 'AwaitExpression') {
64
86
  let error = {
65
87
  message: `变量'${name}'应该使用let标签声明而不是const标签声明`,
66
88
  fix: function(fixer) {
@@ -70,18 +92,38 @@ VariableName.prototype.VariableDeclaration = function (node) {
70
92
  this.report(node, error, 'variable');
71
93
  return error;
72
94
  }
73
- }
74
-
75
- // 只有当确实是变量时才进行变量命名规则检测
76
- if (original_type === 'variable' || original_type === 'unknown') {
77
- let error = this.check(node, name, original_type);
78
95
 
79
- // 如果检测到错误,报告错误
80
- if (error) {
81
- this.report(node, error, original_type);
96
+ // 对于CallExpression,进行详细排除检查
97
+ else if (decl.init.type === 'CallExpression') {
98
+ // 排除require()调用,因为导入的模块类型无法确定
99
+ if (decl.init.callee && decl.init.callee.type === 'Identifier' && decl.init.callee.name === 'require') {
100
+ return; // 跳过require()调用
101
+ }
102
+
103
+ // 排除Object.freeze()等常量函数调用
104
+ if (decl.init.callee && decl.init.callee.type === 'MemberExpression' &&
105
+ decl.init.callee.object && decl.init.callee.object.type === 'Identifier' &&
106
+ decl.init.callee.object.name === 'Object' && decl.init.callee.property &&
107
+ decl.init.callee.property.type === 'Identifier' && (
108
+ decl.init.callee.property.name === 'freeze' ||
109
+ decl.init.callee.property.name === 'seal' ||
110
+ decl.init.callee.property.name === 'preventExtensions'
111
+ )) {
112
+ return; // 跳过常量函数调用
113
+ }
114
+
115
+ let error = {
116
+ message: `变量'${name}'应该使用let标签声明而不是const标签声明`,
117
+ fix: function(fixer) {
118
+ return fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let');
119
+ }
120
+ };
121
+ this.report(node, error, 'variable');
82
122
  return error;
83
123
  }
84
124
  }
125
+ // 对于const声明的其他情况,跳过变量规则检测(由常量规则处理)
126
+ return;
85
127
  }
86
128
  }
87
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_eslint",
3
- "version": "1.4.9",
3
+ "version": "1.5.1",
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": [