mm_eslint 1.2.3 → 1.2.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.
- package/index.js +50 -27
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -790,7 +790,7 @@ Detector.prototype._checkPropName = function (name, val_node, parent_node) {
|
|
|
790
790
|
const val_type = this._getPropType(val_node, name, parent_node);
|
|
791
791
|
|
|
792
792
|
if (use_strong_val) {
|
|
793
|
-
this._checkPropStrong(name, val_type, config, errors);
|
|
793
|
+
this._checkPropStrong(name, val_type, config, errors, val_node);
|
|
794
794
|
} else {
|
|
795
795
|
this._checkPropWeak(name, val_type, config, errors);
|
|
796
796
|
}
|
|
@@ -820,6 +820,11 @@ Detector.prototype._checkPropLength = function (name, config, errors) {
|
|
|
820
820
|
|
|
821
821
|
/**
|
|
822
822
|
* 强验证模式检查
|
|
823
|
+
* @param {string} name - 属性名
|
|
824
|
+
* @param {string} val_type - 值类型
|
|
825
|
+
* @param {Object} config - 配置对象
|
|
826
|
+
* @param {Array} errors - 错误数组
|
|
827
|
+
* @param {Object} val_node - 属性值AST节点(可选)
|
|
823
828
|
* @private
|
|
824
829
|
*/
|
|
825
830
|
Detector.prototype._checkPropStrong = function (
|
|
@@ -827,6 +832,7 @@ Detector.prototype._checkPropStrong = function (
|
|
|
827
832
|
val_type,
|
|
828
833
|
config,
|
|
829
834
|
errors,
|
|
835
|
+
val_node,
|
|
830
836
|
) {
|
|
831
837
|
switch (val_type) {
|
|
832
838
|
case "function":
|
|
@@ -836,7 +842,7 @@ Detector.prototype._checkPropStrong = function (
|
|
|
836
842
|
this._checkMethodProp(name, config, errors);
|
|
837
843
|
break;
|
|
838
844
|
case "class":
|
|
839
|
-
this._checkClassProp(name, config, errors);
|
|
845
|
+
this._checkClassProp(name, config, errors, val_node);
|
|
840
846
|
break;
|
|
841
847
|
case "constant":
|
|
842
848
|
this._checkConstProp(name, config, errors);
|
|
@@ -884,12 +890,17 @@ Detector.prototype._checkFuncProp = function (name, config, errors) {
|
|
|
884
890
|
|
|
885
891
|
/**
|
|
886
892
|
* 检查类属性
|
|
893
|
+
* @param {string} name - 属性名
|
|
894
|
+
* @param {Object} config - 配置对象
|
|
895
|
+
* @param {Array} errors - 错误数组
|
|
896
|
+
* @param {Object} val_node - 属性值AST节点(可选)
|
|
887
897
|
* @private
|
|
888
898
|
*/
|
|
889
|
-
Detector.prototype._checkClassProp = function (name, config, errors) {
|
|
890
|
-
//
|
|
891
|
-
|
|
892
|
-
|
|
899
|
+
Detector.prototype._checkClassProp = function (name, config, errors, val_node) {
|
|
900
|
+
// 根据值节点类型判断是类定义还是类实例
|
|
901
|
+
const is_class_definition = val_node && val_node.type === "CallExpression" &&
|
|
902
|
+
val_node.callee && val_node.callee.name === "require";
|
|
903
|
+
const is_class_instance = !is_class_definition;
|
|
893
904
|
|
|
894
905
|
if (is_class_instance) {
|
|
895
906
|
// 类实例允许使用小驼峰命名
|
|
@@ -1782,27 +1793,39 @@ Detector.prototype._checkRecommen = function (name, rule_type, config, recommend
|
|
|
1782
1793
|
|
|
1783
1794
|
if (should_recommend) {
|
|
1784
1795
|
// 生成推荐建议:将目标词替换为推荐类别词
|
|
1785
|
-
const suggestions =
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
+
const suggestions = [];
|
|
1797
|
+
|
|
1798
|
+
// 为每个检测到的目标词生成一个推荐
|
|
1799
|
+
words.forEach((word) => {
|
|
1800
|
+
if (category_words.includes(word)) {
|
|
1801
|
+
// 只生成使用类别词替换的推荐,不包含原始词
|
|
1802
|
+
const regex = new RegExp(word, 'gi');
|
|
1803
|
+
const suggestion = name.replace(regex, (match) => {
|
|
1804
|
+
// 保持原始大小写格式
|
|
1805
|
+
if (match === match.toUpperCase()) {
|
|
1806
|
+
return category.toUpperCase();
|
|
1807
|
+
} else if (match[0] === match[0].toUpperCase()) {
|
|
1808
|
+
return category[0].toUpperCase() + category.slice(1);
|
|
1809
|
+
} else {
|
|
1810
|
+
return category;
|
|
1811
|
+
}
|
|
1812
|
+
});
|
|
1813
|
+
|
|
1814
|
+
// 只有当推荐结果与原始名称不同时才添加
|
|
1815
|
+
if (suggestion !== name) {
|
|
1816
|
+
suggestions.push(suggestion);
|
|
1796
1817
|
}
|
|
1797
|
-
}
|
|
1818
|
+
}
|
|
1798
1819
|
});
|
|
1799
1820
|
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1821
|
+
if (suggestions.length > 0) {
|
|
1822
|
+
recommendations.push({
|
|
1823
|
+
original: name,
|
|
1824
|
+
category: category,
|
|
1825
|
+
suggestions: suggestions,
|
|
1826
|
+
message: `检测到"${category_words.join('、')}",推荐使用: ${suggestions.join(', ')}`
|
|
1827
|
+
});
|
|
1828
|
+
}
|
|
1806
1829
|
}
|
|
1807
1830
|
}
|
|
1808
1831
|
});
|
|
@@ -2189,10 +2212,10 @@ const variable_name_rule = {
|
|
|
2189
2212
|
node.init.callee &&
|
|
2190
2213
|
node.init.callee.type === "Identifier" &&
|
|
2191
2214
|
/^[A-Z][a-zA-Z]*$/.test(node.init.callee.name)) {
|
|
2192
|
-
//
|
|
2215
|
+
// 这是类实例,应该使用函数名规则(camelCase)
|
|
2193
2216
|
const class_instance_name = node.id.name;
|
|
2194
|
-
const detector = new Detector({ "
|
|
2195
|
-
const result = detector.checkName(class_instance_name, "
|
|
2217
|
+
const detector = new Detector({ "function-name": options });
|
|
2218
|
+
const result = detector.checkName(class_instance_name, "function-name");
|
|
2196
2219
|
|
|
2197
2220
|
if (!result.valid) {
|
|
2198
2221
|
result.errors.forEach((error) => {
|