-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (37 loc) · 1.16 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
import fs from 'fs';
import recast from 'recast';
const data = fs.readFileSync('./testData.js', 'utf8');
const n = recast.types;
const b = recast.types.builders;
const ast = recast.parse(data).program;
n.visit(ast, {
visitFunctionDeclaration: function (path) {
path.get('body', 'body').unshift(makeConsole(`'${path.node.id.name}'`));
// path.get('body').unshift(makeConsole(`'${path.node.id.name}'`));
this.traverse(path);
},
visitFunctionExpression: function (path) {
// TODO use check
if (path.parent.value.type === 'VariableDeclarator') {
path.get('body', 'body').unshift(makeConsole(`'${path.parent.node.id.name}'`))
}
if (path.parent.value.type === 'MethodDefinition') {
path.get('body', 'body').unshift(makeConsole(`'${path.parent.node.key.name}'`))
}
this.traverse(path);
}
});
function makeConsole(param) {
const d = b.expressionStatement(
b.callExpression(
b.memberExpression(
b.identifier('console'),
b.identifier('log'),
),
[b.identifier(`${param}`)],
)
);
return d;
}
const modifiedCode = recast.print(ast).code;
fs.writeFileSync('./testData.js', modifiedCode, 'utf-8');