-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathast.js
More file actions
48 lines (42 loc) · 1.09 KB
/
Copy pathast.js
File metadata and controls
48 lines (42 loc) · 1.09 KB
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
let code = `
const computedBehavior = require('miniprogram-computed')
export default {
data: {},
lifetimes: {
pullDown() {}
},
methods: {},
watch: {},
compoted: {}
};
`
let res = `
Page({
data: {},
lifetimes: {
pullDown() {}
},
methods: {},
watch: {},
compoted: {}
});
`
var recast = require("recast");
let ast = recast.parse(code)
let a = recast.parse(res)
var builders = recast.types.builders;
const { expressionStatement, callExpression,identifier,variableDeclaration,literal,variableDeclarator } = builders
// builders.ExpressionStatement()
// builders.callExpression
//ast.program.body[0].declaration.properties
// ast.program.body.unshift(variableDeclaration('const',[variableDeclarator('computedBehavior',callExpression(identifier('require'), [literal('miniprogram-computed')]))]))
recast.visit(ast, {
visitExportDefaultDeclaration: function (path) {
const node = path.node
const rep = expressionStatement(callExpression(identifier('Page'), [node.declaration]))
path.replace(rep)
return false
}
})
console.log(recast.print(ast).code)
console.log('end')