UNPKG

2.13 kBJavaScriptView Raw
1function guessMethodName(node, anc) {
2 var actualName = node.id && node.id.name || node.key && node.key.name;
3 if (actualName && ((node.kind == "set") || (node.kind == "get")))
4 {
5 actualName = node.kind + " " + actualName;
6 }
7 var isAnonymous = !actualName;
8 var methodType = null;
9 var guessName = "(Anonymous)";
10 var property = "ObjectProperty";
11 if (isAnonymous) {
12 if (anc) {
13 switch (anc.type) {
14 case "VariableDeclarator":
15 if (anc.id && anc.id.type == "Identifier") {
16 guessName = anc.id.name;
17 }
18 break;
19 case "ObjectProperty":
20 case "ObjectMethod":
21 if (anc.key && anc.key.type == "Identifier") {
22 guessName = anc.key.name;
23 methodType = "getterSetter";
24 }
25 break;
26 case "AssignmentExpression":
27 if (anc.left && anc.left.type == "MemberExpression" && anc.left.property && anc.left.property.type == "Identifier") {
28 guessName = anc.left.property.name;
29 } else if (anc.left && anc.left.type == "Identifier") {
30 guessName = anc.left.name;
31 }
32 break;
33 case "CallExpression":
34 //This is an anonymous function passed to a function, nothing useful to add
35 break;
36 case "MethodDefinition":
37 if (anc.key && anc.key.type == "Identifier") {
38 guessName = anc.key.name;
39 methodType = "method";
40 }
41 break;
42 default:
43 break;
44 }
45 }
46 }
47 return {
48 actualName: actualName, //If the method is anonymous, this will be null/undefined
49 name: actualName || guessName,
50 type: methodType,
51 isAnonymous: isAnonymous
52 }
53}
54module.exports.guessMethodName = guessMethodName;
\No newline at end of file