mm_eslint 1.3.0 → 1.3.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/detector.js +49 -8
- package/package.json +1 -1
package/detector.js
CHANGED
|
@@ -399,6 +399,17 @@ Detector.prototype._checkConstantName = function (constant_name, node, val_node)
|
|
|
399
399
|
return null;
|
|
400
400
|
}
|
|
401
401
|
|
|
402
|
+
// 检查是否应该使用let而不是const
|
|
403
|
+
var should_use_let = this._shouldUseLetInsteadOfConst(node, val_node);
|
|
404
|
+
|
|
405
|
+
// 如果应该使用let,则提示开发者
|
|
406
|
+
if (should_use_let) {
|
|
407
|
+
return {
|
|
408
|
+
node: node,
|
|
409
|
+
message: `变量'${constant_name}'应该使用let而不是const声明,因为它不是真正的常量`
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
402
413
|
// 检查是否为复杂类型(方法、函数、实例等)
|
|
403
414
|
var is_complex_type = this._isComplexType(val_node);
|
|
404
415
|
|
|
@@ -885,6 +896,38 @@ Detector.prototype._isBaseValue = function (val_node) {
|
|
|
885
896
|
}
|
|
886
897
|
};
|
|
887
898
|
|
|
899
|
+
/**
|
|
900
|
+
* 检查是否应该使用let而不是const声明变量
|
|
901
|
+
* @param {object} node 变量声明节点
|
|
902
|
+
* @param {object} val_node 值节点
|
|
903
|
+
* @returns {boolean} 是否应该使用let
|
|
904
|
+
*/
|
|
905
|
+
Detector.prototype._shouldUseLetInsteadOfConst = function (node, val_node) {
|
|
906
|
+
try {
|
|
907
|
+
if (!node || !val_node) {
|
|
908
|
+
return false;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// 检查是否为解构赋值
|
|
912
|
+
var is_destructuring = false;
|
|
913
|
+
if (node.parent && node.parent.type === 'VariableDeclarator') {
|
|
914
|
+
var declarator = node.parent;
|
|
915
|
+
if (declarator.id && (declarator.id.type === 'ObjectPattern' || declarator.id.type === 'ArrayPattern')) {
|
|
916
|
+
is_destructuring = true;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// 检查右侧是否为复杂类型(类实例、函数、对象等)
|
|
921
|
+
var is_complex_type = this._isComplexType(val_node);
|
|
922
|
+
|
|
923
|
+
// 如果是解构赋值且右侧是复杂类型,建议使用let
|
|
924
|
+
return is_destructuring && is_complex_type;
|
|
925
|
+
} catch (error) {
|
|
926
|
+
console.error('检查是否应该使用let时出错:', error);
|
|
927
|
+
return false;
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
|
|
888
931
|
/**
|
|
889
932
|
* 检查值是否为复杂类型(方法、函数、实例等)
|
|
890
933
|
* @param {object} val_node 值节点
|
|
@@ -1159,14 +1202,12 @@ Detector.prototype._checkConstantNameForDestructuring = function (constant_name,
|
|
|
1159
1202
|
return null;
|
|
1160
1203
|
}
|
|
1161
1204
|
|
|
1162
|
-
//
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
};
|
|
1169
|
-
}
|
|
1205
|
+
// 对于解构赋值,直接提示使用let而不是const
|
|
1206
|
+
// 因为解构赋值通常不是真正的常量
|
|
1207
|
+
return {
|
|
1208
|
+
node: node,
|
|
1209
|
+
message: `变量'${constant_name}'应该使用let而不是const声明,因为它不是真正的常量`
|
|
1210
|
+
};
|
|
1170
1211
|
|
|
1171
1212
|
// 检查并推荐更合适的词汇
|
|
1172
1213
|
var rec_err = this._checkAndRecommend(constant_name, 'constant-name');
|