-
Notifications
You must be signed in to change notification settings - Fork 1
/
staticFunc.js
41 lines (38 loc) · 1.05 KB
/
staticFunc.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
const estraverse = require('estraverse')
const Literal = require('./util/Literal')
module.exports = parsed => {
const functions = {}
estraverse.traverse(parsed, {
enter: node => {
if (
node.type === 'FunctionDeclaration' &&
node.body.type === 'BlockStatement' &&
node.body.body.length === 1 &&
node.body.body[0].type === 'ReturnStatement' &&
node.body.body[0].argument &&
node.body.body[0].argument.type === 'Literal'
) {
const { name } = node.id
const { value } = node.body.body[0].argument
functions[name] = value
}
}
})
return estraverse.replace(parsed, {
enter: node => {
if (
node.type === 'FunctionDeclaration' &&
functions[node.id.name] != null
) {
return estraverse.VisitorOption.Remove
}
if (
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
functions[node.callee.name] != null
) {
return Literal(functions[node.callee.name])
}
}
})
}