mm_eslint 1.0.6 → 1.0.7
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 +77 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1879,6 +1879,82 @@ 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
|
+
// 跳过私有方法的检测(私有方法由private-naming规则处理)
|
|
1926
|
+
if (method_name.startsWith("_")) {
|
|
1927
|
+
return;
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
// 跳过JavaScript内置特殊方法名
|
|
1931
|
+
const builtin_methods = [
|
|
1932
|
+
"constructor",
|
|
1933
|
+
"toString",
|
|
1934
|
+
"valueOf",
|
|
1935
|
+
"toJSON",
|
|
1936
|
+
];
|
|
1937
|
+
if (builtin_methods.includes(method_name)) {
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
const detector = new Detector({ "method-name": options });
|
|
1942
|
+
const result = detector.checkName(method_name, "method-name");
|
|
1943
|
+
|
|
1944
|
+
if (!result.valid) {
|
|
1945
|
+
result.errors.forEach((error) => {
|
|
1946
|
+
context.report({
|
|
1947
|
+
node: node.left.property,
|
|
1948
|
+
message: `原型方法名"${method_name}"不符合规范: ${error}`,
|
|
1949
|
+
});
|
|
1950
|
+
});
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
},
|
|
1954
|
+
};
|
|
1955
|
+
},
|
|
1956
|
+
};
|
|
1957
|
+
|
|
1882
1958
|
/**
|
|
1883
1959
|
* 命名规范ESLint插件
|
|
1884
1960
|
*/
|
|
@@ -1893,6 +1969,7 @@ const naming_rules = {
|
|
|
1893
1969
|
"param-name": param_name_rule,
|
|
1894
1970
|
"property-name": prop_name_rule,
|
|
1895
1971
|
"instance-property": instance_property_rule,
|
|
1972
|
+
"prototype-method": prototype_method_rule,
|
|
1896
1973
|
};
|
|
1897
1974
|
|
|
1898
1975
|
module.exports = {
|