-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (115 loc) · 2.92 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var homunculus = require('homunculus');
var Node = homunculus.getClass('Node', 'js');
var Token = homunculus.getClass('Token');
var res;
var flag;
function isAsync(code, f) {
res = false;
flag = f || 'callback';
var parser = homunculus.getParser('js');
var ast = parser.parse(code);
parse(ast, {});
return res;
}
function parse(ast, context) {
preVar(ast, context);
search(ast, context, true);
}
function preVar(ast, context) {
if(ast.isToken()) {
return;
}
switch(ast.name()) {
case Node.FNDECL:
case Node.FNEXPR:
break;
case Node.VARDECL:
var id = ast.first().token().content();
var assign = ast.leaf(1);
if(assign.name() == Node.ASSIGN) {
varThis(context, id, assign);
}
break;
default:
ast.leaves().forEach(function(leaf) {
preVar(leaf, context);
});
}
}
function varThis(context, id, ast) {
if(!ast) {
return;
}
var assign = ast.leaf(1);
switch(assign.name()) {
case Node.PRMREXPR:
var t = assign.first();
if(t && t.isToken() && t.token().content() == 'this') {
context[id] = true;
}
break;
case Node.ASSIGNEXPR:
varThis(context, id, assign.leaf(2));
break;
}
}
function search(ast, context, inGlobal) {
if(ast.isToken()) {
return;
}
switch(ast.name()) {
case Node.CALLEXPR:
var mmb = ast.first();
var args = ast.leaf(1);
if(mmb.name() == Node.MMBEXPR && args.name() == Node.ARGS) {
var prmr = mmb.first();
if(prmr.name() == Node.PRMREXPR) {
var t = prmr.first();
if(t && t.isToken()) {
var id = t.token().content();
if(id == 'this' && inGlobal
|| context.hasOwnProperty(id)
) {
var dot = mmb.leaf(1);
if(dot && dot.isToken()) {
switch(dot.token().content()) {
case '.':
var name = mmb.leaf(2);
if(name && name.isToken() && name.token().content() == flag) {
return res = true;
}
break;
case '[':
prmr = mmb.leaf(2);
if(prmr && prmr.name() == Node.PRMREXPR) {
t = prmr.first();
if(t && t.isToken()) {
t = t.token();
if(t.type() == Token.STRING && t.val() == flag) {
return res = true;
}
}
}
break;
}
}
}
}
}
}
break;
}
ast.leaves().forEach(function(leaf) {
if(res) {
return;
}
switch(ast.name()) {
case Node.FNDECL:
case Node.FNEXPR:
inGlobal = false;
break;
}
search(leaf, context, inGlobal);
});
}
module.exports = isAsync;