mm_eslint 1.0.6 → 1.0.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.
- package/index.js +74 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1879,6 +1879,79 @@ const instance_property_rule = {
|
|
|
1879
1879
|
},
|
|
1880
1880
|
};
|
|
1881
1881
|
|
|
1882
|
+
/**
|
|
1883
|
+
* 原型方法命名规则(检测 ClassName.prototype.methodName = function() {} 模式)
|
|
1884
|
+
*/
|
|
1885
|
+
const prototype_method_rule = {
|
|
1886
|
+
meta: {
|
|
1887
|
+
type: "suggestion",
|
|
1888
|
+
docs: {
|
|
1889
|
+
description: "原型方法名必须使用小驼峰命名法(camelCase)且优先使用单个单词",
|
|
1890
|
+
category: "Stylistic Issues",
|
|
1891
|
+
recommended: true,
|
|
1892
|
+
},
|
|
1893
|
+
schema: [
|
|
1894
|
+
{
|
|
1895
|
+
type: "object",
|
|
1896
|
+
properties: {
|
|
1897
|
+
regex: { type: "string" },
|
|
1898
|
+
min: { type: "number" },
|
|
1899
|
+
max: { type: "number" },
|
|
1900
|
+
message: { type: "string" },
|
|
1901
|
+
single_word: { type: "boolean" },
|
|
1902
|
+
single_word_len: { type: "number" },
|
|
1903
|
+
styles: { type: "array", items: { type: "string" } },
|
|
1904
|
+
},
|
|
1905
|
+
additionalProperties: false,
|
|
1906
|
+
},
|
|
1907
|
+
],
|
|
1908
|
+
},
|
|
1909
|
+
create(context) {
|
|
1910
|
+
const options = context.options[0] || {};
|
|
1911
|
+
return {
|
|
1912
|
+
AssignmentExpression(node) {
|
|
1913
|
+
// 检测 ClassName.prototype.methodName = function() {} 模式
|
|
1914
|
+
if (
|
|
1915
|
+
node.left.type === "MemberExpression" &&
|
|
1916
|
+
node.left.object.type === "MemberExpression" &&
|
|
1917
|
+
node.left.object.property.type === "Identifier" &&
|
|
1918
|
+
node.left.object.property.name === "prototype" &&
|
|
1919
|
+
node.left.property.type === "Identifier" &&
|
|
1920
|
+
(node.right.type === "FunctionExpression" ||
|
|
1921
|
+
node.right.type === "ArrowFunctionExpression")
|
|
1922
|
+
) {
|
|
1923
|
+
const method_name = node.left.property.name;
|
|
1924
|
+
|
|
1925
|
+
// 跳过JavaScript内置特殊方法名
|
|
1926
|
+
const builtin_methods = [
|
|
1927
|
+
"constructor",
|
|
1928
|
+
"toString",
|
|
1929
|
+
"valueOf",
|
|
1930
|
+
"toJSON",
|
|
1931
|
+
];
|
|
1932
|
+
if (builtin_methods.includes(method_name)) {
|
|
1933
|
+
return;
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
// 根据方法名是否以下划线开头,选择不同的检测规则
|
|
1937
|
+
const rule_name = method_name.startsWith("_") ? "private-method-naming" : "method-name";
|
|
1938
|
+
const detector = new Detector({ [rule_name]: options });
|
|
1939
|
+
const result = detector.checkName(method_name, rule_name);
|
|
1940
|
+
|
|
1941
|
+
if (!result.valid) {
|
|
1942
|
+
result.errors.forEach((error) => {
|
|
1943
|
+
context.report({
|
|
1944
|
+
node: node.left.property,
|
|
1945
|
+
message: `原型方法名"${method_name}"不符合规范: ${error}`,
|
|
1946
|
+
});
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
},
|
|
1951
|
+
};
|
|
1952
|
+
},
|
|
1953
|
+
};
|
|
1954
|
+
|
|
1882
1955
|
/**
|
|
1883
1956
|
* 命名规范ESLint插件
|
|
1884
1957
|
*/
|
|
@@ -1893,6 +1966,7 @@ const naming_rules = {
|
|
|
1893
1966
|
"param-name": param_name_rule,
|
|
1894
1967
|
"property-name": prop_name_rule,
|
|
1895
1968
|
"instance-property": instance_property_rule,
|
|
1969
|
+
"prototype-method": prototype_method_rule,
|
|
1896
1970
|
};
|
|
1897
1971
|
|
|
1898
1972
|
module.exports = {
|