forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace-console-with-log.js
More file actions
89 lines (84 loc) · 2.21 KB
/
Copy pathreplace-console-with-log.js
File metadata and controls
89 lines (84 loc) · 2.21 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This is the pattern we are matching:
// console.log("something")
function replaceConsoleDotLogWithLog(root, j) {
root
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: { name: 'console' },
property: { name: 'log' },
},
})
.replaceWith((nodePath) => {
const { node } = nodePath
const ast = j.memberExpression(
j.identifier('logger'),
j.callExpression(j.identifier('info'), [...node.arguments]),
)
return ast
})
}
function replaceConsoleDotErrorWithError(root, j) {
root
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: { name: 'console' },
property: { name: 'error' },
},
})
.replaceWith((nodePath) => {
const { node } = nodePath
const ast = j.memberExpression(
j.identifier('logger'),
j.callExpression(j.identifier('error'), [...node.arguments]),
)
return ast
})
}
function replaceConsoleDotInfoWithInfo(root, j) {
root
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: { name: 'console' },
property: { name: 'info' },
},
})
.replaceWith((nodePath) => {
const { node } = nodePath
var ast = j.memberExpression(
j.identifier('logger'),
j.callExpression(j.identifier('info'), [...node.arguments]),
)
return ast
})
}
function replaceConsoleDotWarnWithWarn(root, j) {
root
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: { name: 'console' },
property: { name: 'warn' },
},
})
.replaceWith((nodePath) => {
const { node } = nodePath
var ast = j.memberExpression(
j.identifier('logger'),
j.callExpression(j.identifier('warn'), [...node.arguments]),
)
return ast
})
}
module.exports = function (fileInfo, api, _options) {
const j = api.jscodeshift
const root = j(fileInfo.source)
replaceConsoleDotLogWithLog(root, j)
replaceConsoleDotErrorWithError(root, j)
replaceConsoleDotWarnWithWarn(root, j)
replaceConsoleDotInfoWithInfo(root, j)
// match a function call
return root.toSource()
}