-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods-naming.js
53 lines (49 loc) · 1.84 KB
/
methods-naming.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module.exports = {
meta: {
type: "problem",
docs: {
description: "Check if a method with JSDoc is properly private, protected or public",
category: "Possible Errors",
recommended: true
},
schema: [] // This rule does not take any options.
},
create: function(context) {
return {
MethodDefinition: function(node) {
const commentsBefore = context.getCommentsBefore(node);
const jsDocComment = commentsBefore.find(comment =>
comment.type === "Block" && comment.value.startsWith("*")
);
if (jsDocComment) {
const isPrivate = node.key.name.startsWith("__");
const isProtected = node.key.name.startsWith("_") && !node.key.name.startsWith("__");
const isPublicJsDoc = jsDocComment.value.includes("@public");
const isProtectedJsDoc = jsDocComment.value.includes("@protected");
const isPrivateJsDoc = jsDocComment.value.includes("@private");
if (isPrivate && isPublicJsDoc) {
context.report({
node: node,
message: "Methods starting with '__' should not be marked with '@public'."
});
} else if (isProtected && isPublicJsDoc) {
context.report({
node: node,
message: "Methods starting with '_' should not be marked with '@public'."
});
} else if (!isPrivate && isPrivateJsDoc) {
context.report({
node: node,
message: "Methods not starting with '__' should not be marked with '@private'."
});
} else if (!isProtected && isProtectedJsDoc) {
context.report({
node: node,
message: "Methods not starting with '_' should not be marked with '@protected'."
});
}
}
}
};
}
};