-
Notifications
You must be signed in to change notification settings - Fork 6
/
Parser.js
50 lines (50 loc) · 1.41 KB
/
Parser.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
function Parser () {
return function (input) {
try {
var syntax = esprima.parse(input)
} catch(e) {
return []
}
var elements = []
var dict = {
VariableDeclaration: function (el) {
var tmp = []
el.declarations.forEach(function (val) {
tmp.push(val.id.name)
})
return tmp
},
FunctionDeclaration: function (el) {
return [el.id.name]
},
ExpressionStatement: function (el) {
if (el.expression.type === 'AssignmentExpression') {
if (el.expression.left.type === 'Identifier') {
return [el.expression.left.name]
} else if (el.expression.left.type === 'MemberExpression') {
return [el.expression.left.object.name]
}
} else if (el.expression.type === 'SequenceExpression') {
var tmp = []
el.expression.expressions.forEach(function (exp) {
if (exp.type === 'AssignmentExpression') {
tmp.push(exp.left.name)
}
})
return tmp
}else if (el.expression.type === 'CallExpression') {
if (el.expression.callee.object) {
return [el.expression.callee.object.name]
}
}
}
}
syntax.body.forEach(function (el) {
var ids = dict[el.type](el)
if (ids) {
elements = elements.concat(ids)
}
})
return elements
}
}