mm_eslint 1.4.6 → 1.4.8
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.
|
@@ -184,6 +184,22 @@ ConstName.prototype._isConstantValue = function (value_node) {
|
|
|
184
184
|
return true;
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
+
// 排除函数调用(CallExpression)
|
|
188
|
+
if (value_node.type === 'CallExpression') {
|
|
189
|
+
// 只允许特定的常量函数调用(如Object.freeze等)
|
|
190
|
+
// 普通的函数调用应该被视为变量
|
|
191
|
+
if (value_node.callee && value_node.callee.type === 'MemberExpression' &&
|
|
192
|
+
value_node.callee.object && value_node.callee.object.name === 'Object' &&
|
|
193
|
+
value_node.callee.property && (
|
|
194
|
+
value_node.callee.property.name === 'freeze' ||
|
|
195
|
+
value_node.callee.property.name === 'seal' ||
|
|
196
|
+
value_node.callee.property.name === 'preventExtensions'
|
|
197
|
+
)) {
|
|
198
|
+
return true; // Object.freeze等调用是常量
|
|
199
|
+
}
|
|
200
|
+
return false; // 其他函数调用不是常量
|
|
201
|
+
}
|
|
202
|
+
|
|
187
203
|
// 默认情况下,如果不是排除的类型,就认为是常量
|
|
188
204
|
return true;
|
|
189
205
|
}
|
|
@@ -431,6 +447,16 @@ ConstName.prototype.AssignmentExpression = function (node) {
|
|
|
431
447
|
return;
|
|
432
448
|
}
|
|
433
449
|
|
|
450
|
+
// 排除函数表达式和箭头函数表达式
|
|
451
|
+
if (node.right && (node.right.type === 'FunctionExpression' || node.right.type === 'ArrowFunctionExpression')) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// 排除类表达式
|
|
456
|
+
if (node.right && node.right.type === 'ClassExpression') {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
|
|
434
460
|
// 判断右边值是否为常量,同时排除类定义的情况
|
|
435
461
|
if (this._isConstantValue(node.right) && !this._isClassDefinition(node.right)) {
|
|
436
462
|
let name = node.left.property.name;
|
|
@@ -16,8 +16,8 @@ class VariableName extends Name {
|
|
|
16
16
|
* @returns {Object|undefined} - 检测结果对象或undefined
|
|
17
17
|
*/
|
|
18
18
|
VariableName.prototype.VariableDeclaration = function (node) {
|
|
19
|
-
// 检测变量声明(let
|
|
20
|
-
if (node.kind === 'let' || node.kind === 'var') {
|
|
19
|
+
// 检测变量声明(let、var 和需要修正的const)
|
|
20
|
+
if (node.kind === 'let' || node.kind === 'var' || node.kind === 'const') {
|
|
21
21
|
for (let i = 0; i < node.declarations.length; i++) {
|
|
22
22
|
let decl = node.declarations[i];
|
|
23
23
|
if (decl.id && decl.id.type === 'Identifier') {
|
|
@@ -57,6 +57,21 @@ VariableName.prototype.VariableDeclaration = function (node) {
|
|
|
57
57
|
// 使用精确的类型识别逻辑
|
|
58
58
|
let original_type = this._getOriginalType(decl, 'VariableDeclaration');
|
|
59
59
|
|
|
60
|
+
// 对于const声明,检查是否需要改为let
|
|
61
|
+
if (node.kind === 'const') {
|
|
62
|
+
// 如果右边是函数调用,应该使用let而不是const
|
|
63
|
+
if (decl.init && decl.init.type === 'CallExpression') {
|
|
64
|
+
let error = {
|
|
65
|
+
message: `变量'${name}'应该使用let标签声明而不是const标签声明`,
|
|
66
|
+
fix: function(fixer) {
|
|
67
|
+
return fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let');
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
this.report(node, error, 'variable');
|
|
71
|
+
return error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
60
75
|
// 只有当确实是变量时才进行变量命名规则检测
|
|
61
76
|
if (original_type === 'variable' || original_type === 'unknown') {
|
|
62
77
|
let error = this.check(node, name, original_type);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_eslint",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.8",
|
|
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": [
|