This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
no-private-func.js
60 lines (48 loc) · 1.81 KB
/
no-private-func.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
54
55
56
57
58
59
60
'use strict';
const antlr4 = require('antlr4'),
Trees = antlr4.tree.Trees;
module.exports = {
create(context) {
let privateFuncs = [];
const isFunctionInvocation = (node) => {
let openParen = node.children[1];
return openParen && openParen.getText() === '(';
};
const isLocalPrivateFunctionInvocation = (funcName) => {
return privateFuncs.some(privateFunc => privateFunc.toUpperCase() === funcName.toUpperCase());
}
const report = (node) => {
context.report({
message: `A private function was invoked outside of the local scope.`,
loc: {
start: node.start,
end: node.stop
}
});
}
return {
'startRule:exit': function(node) {
privateFuncs = Trees.findAllRuleNodes(node, 3)
.map(element => element.children[0].children[1].getText())
.filter(func => func.startsWith('_'));
},
'expression:exit': function(node) {
if (!isFunctionInvocation(node)) {
return;
}
let invocationPath = node.children[0].getText();
if (invocationPath.indexOf('.') > -1) {
const invocationParts = invocationPath.split('.');
const funcName = invocationParts[invocationParts.length - 1];
if (funcName.startsWith('_')) {
report(node);
}
return;
}
if (invocationPath.startsWith('_') && !isLocalPrivateFunctionInvocation(invocationPath)) {
report(node);
}
}
};
}
};