-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel-plugin.js
40 lines (39 loc) · 1.24 KB
/
babel-plugin.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
const methods = ['log', 'info', 'warn', 'error', 'debug']
const cwd = process.cwd()
module.exports = function ({ types: t }) {
return {
visitor: {
CallExpression(path, state) {
if (
path.get('callee').isMemberExpression() &&
path.get('callee.object').isIdentifier({ name: 'console' }) &&
methods.filter((method) =>
path.get('callee.property').isIdentifier({ name: method }),
).length > 0
) {
let file = state.file.opts.filename || "unknown"
if (typeof state.opts.resolveFile === 'function') {
file = state.opts.resolveFile(file)
}
file = file.split(cwd)[1] || file
const fmt = `${file}:${path.node.loc.start.line}:${path.node.loc.start.column}`
path.replaceWith(
t.callExpression(
t.callExpression(
t.memberExpression(
t.identifier('omnilog'),
path.get('callee.property').node,
),
[
...path.get('arguments').map((arg) => arg.node),
t.stringLiteral(fmt),
],
),
[],
),
)
}
},
},
}
}