forked from cxapython/catvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST-study.js
37 lines (35 loc) · 1.39 KB
/
AST-study.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
// parser生成AST树
const parser = require("@babel/parser");
// traverse遍历操作
const traverse =require("@babel/traverse");
// generator生成js代码
const generator = require("@babel/generator");
// typesAST树所有类型
const types = require("@babel/types");
var fs = require("fs")
const code = fs.readFileSync('ob_js.js', 'utf-8');
const code_1 = fs.readFileSync('md5.js', 'utf-8');
const ast = parser.parse(code);
traverse.default(ast,{
StringLiteral:function(path) {
delete path.node.extra.raw
},
BinaryExpression:function (path) {
if (path.node.left.type === 'StringLiteral' && path.node.right.type === 'StringLiteral' && path.node.operator === '+') {
path.replaceWithSourceString("'"+path.node.left.value + path.node.right.value+"'")
}
},
MemberExpression:function (path) {
if (path.node.property.type === 'StringLiteral' && (path.node.object.type === 'Identifier' || path.node.object.type === 'MemberExpression')){
path.node.property = types.identifier(path.node.property.value)
path.node.computed = false
}
},
CallExpression:function (path) {
if (path.node.callee.name === '$b'){
path.replaceWithSourceString("'"+eval(code_1 + path.toString())+"'")
}
}
})
js = generator.default(ast).code
console.log(js)