mm_eslint 1.5.0 → 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.
- package/lib/detector/const_name.js +27 -4
- package/lib/detector/variable_name.js +39 -24
- package/package.json +1 -1
|
@@ -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
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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
|
}
|
|
@@ -79,33 +79,48 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
79
79
|
// 检测变量名
|
|
80
80
|
let name = decl.id.name;
|
|
81
81
|
|
|
82
|
-
// 对于const
|
|
83
|
-
if (decl.init
|
|
84
|
-
//
|
|
85
|
-
if (decl.init.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
decl.init.callee.property.name === 'freeze' ||
|
|
95
|
-
decl.init.callee.property.name === 'seal' ||
|
|
96
|
-
decl.init.callee.property.name === 'preventExtensions'
|
|
97
|
-
)) {
|
|
98
|
-
return; // 跳过常量函数调用
|
|
82
|
+
// 对于const声明,处理右边是CallExpression或AwaitExpression的情况
|
|
83
|
+
if (decl.init) {
|
|
84
|
+
// 对于AwaitExpression,直接处理
|
|
85
|
+
if (decl.init.type === 'AwaitExpression') {
|
|
86
|
+
let error = {
|
|
87
|
+
message: `变量'${name}'应该使用let标签声明而不是const标签声明`,
|
|
88
|
+
fix: function(fixer) {
|
|
89
|
+
return fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let');
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
this.report(node, error, 'variable');
|
|
93
|
+
return error;
|
|
99
94
|
}
|
|
100
95
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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()调用
|
|
105
101
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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');
|
|
122
|
+
return error;
|
|
123
|
+
}
|
|
109
124
|
}
|
|
110
125
|
// 对于const声明的其他情况,跳过变量规则检测(由常量规则处理)
|
|
111
126
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_eslint",
|
|
3
|
-
"version": "1.5.
|
|
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": [
|