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
60 lines (56 loc) · 1.49 KB
/
replace-console-with-log.js
File metadata and controls
60 lines (56 loc) · 1.49 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
// 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
var ast = j.callExpression(j.identifier('log'), 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
var ast = j.callExpression(j.identifier('error'), 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.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)
// match a function call
return root.toSource()
}