mm_eslint 1.2.4 → 1.2.6
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 +43 -28
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -897,23 +897,26 @@ Detector.prototype._checkFuncProp = function (name, config, errors) {
|
|
|
897
897
|
* @private
|
|
898
898
|
*/
|
|
899
899
|
Detector.prototype._checkClassProp = function (name, config, errors, val_node) {
|
|
900
|
-
//
|
|
901
|
-
|
|
902
|
-
|
|
900
|
+
// 根据值节点类型和属性名模式判断是类定义还是类实例
|
|
901
|
+
// 1. 如果值节点是require调用,则是类定义
|
|
902
|
+
// 2. 如果属性名符合PascalCase命名规范,优先认为是类定义
|
|
903
|
+
const is_class_definition = (val_node && val_node.type === "CallExpression" &&
|
|
904
|
+
val_node.callee && val_node.callee.name === "require") ||
|
|
905
|
+
(name && /^[A-Z][a-zA-Z0-9]*$/.test(name));
|
|
903
906
|
const is_class_instance = !is_class_definition;
|
|
904
907
|
|
|
905
|
-
if (
|
|
906
|
-
// 类实例允许使用小驼峰命名
|
|
907
|
-
const func_res = this._checkName("function-name", name);
|
|
908
|
-
if (!func_res.valid) {
|
|
909
|
-
errors.push(`${config.name}(类实例)${func_res.errors.join(", ")}`);
|
|
910
|
-
}
|
|
911
|
-
} else {
|
|
908
|
+
if (is_class_definition) {
|
|
912
909
|
// 类定义要求使用PascalCase
|
|
913
910
|
const cls_res = this._checkName("class-name", name);
|
|
914
911
|
if (!cls_res.valid) {
|
|
915
912
|
errors.push(`${config.name}(类)${cls_res.errors.join(", ")}`);
|
|
916
913
|
}
|
|
914
|
+
} else {
|
|
915
|
+
// 类实例允许使用小驼峰命名
|
|
916
|
+
const func_res = this._checkName("function-name", name);
|
|
917
|
+
if (!func_res.valid) {
|
|
918
|
+
errors.push(`${config.name}(类实例)${func_res.errors.join(", ")}`);
|
|
919
|
+
}
|
|
917
920
|
}
|
|
918
921
|
};
|
|
919
922
|
|
|
@@ -1793,27 +1796,39 @@ Detector.prototype._checkRecommen = function (name, rule_type, config, recommend
|
|
|
1793
1796
|
|
|
1794
1797
|
if (should_recommend) {
|
|
1795
1798
|
// 生成推荐建议:将目标词替换为推荐类别词
|
|
1796
|
-
const suggestions =
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1799
|
+
const suggestions = [];
|
|
1800
|
+
|
|
1801
|
+
// 为每个检测到的目标词生成一个推荐
|
|
1802
|
+
words.forEach((word) => {
|
|
1803
|
+
if (category_words.includes(word)) {
|
|
1804
|
+
// 只生成使用类别词替换的推荐,不包含原始词
|
|
1805
|
+
const regex = new RegExp(word, 'gi');
|
|
1806
|
+
const suggestion = name.replace(regex, (match) => {
|
|
1807
|
+
// 保持原始大小写格式
|
|
1808
|
+
if (match === match.toUpperCase()) {
|
|
1809
|
+
return category.toUpperCase();
|
|
1810
|
+
} else if (match[0] === match[0].toUpperCase()) {
|
|
1811
|
+
return category[0].toUpperCase() + category.slice(1);
|
|
1812
|
+
} else {
|
|
1813
|
+
return category;
|
|
1814
|
+
}
|
|
1815
|
+
});
|
|
1816
|
+
|
|
1817
|
+
// 只有当推荐结果与原始名称不同时才添加
|
|
1818
|
+
if (suggestion !== name) {
|
|
1819
|
+
suggestions.push(suggestion);
|
|
1807
1820
|
}
|
|
1808
|
-
}
|
|
1821
|
+
}
|
|
1809
1822
|
});
|
|
1810
1823
|
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1824
|
+
if (suggestions.length > 0) {
|
|
1825
|
+
recommendations.push({
|
|
1826
|
+
original: name,
|
|
1827
|
+
category: category,
|
|
1828
|
+
suggestions: suggestions,
|
|
1829
|
+
message: `检测到"${category_words.join('、')}",推荐使用: ${suggestions.join(', ')}`
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1817
1832
|
}
|
|
1818
1833
|
}
|
|
1819
1834
|
});
|