forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-dotenv-from-tests.js
More file actions
64 lines (59 loc) · 1.47 KB
/
remove-dotenv-from-tests.js
File metadata and controls
64 lines (59 loc) · 1.47 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
// This is the pattern we are matching:
// const dotenv = require('dotenv-safe')
// dotenv.config()
function removeRequireDotEnv(root, j) {
const dotenvVariables = root
.findVariableDeclarators()
.filter(j.filters.VariableDeclarator.requiresModule('dotenv-safe'))
if (dotenvVariables.size() > 0) {
const dotenvVariableName = dotenvVariables.nodes()[0].id.name
root
.find(j.CallExpression, {
callee: {
object: {
name: dotenvVariableName,
},
property: {
name: 'config',
},
},
})
.remove()
}
dotenvVariables.remove()
}
// This is the pattern we are matching:
// require('dotenv-safe').config({
// allowEmptyValues: true,
// })
function removeRequireDotConfigCall(root, j) {
root
.find(j.CallExpression, {
callee: {
property: {
name: 'config',
},
// verify that we call config on the result of require()
object: {
callee: {
name: 'require',
},
// but only when the argument to require is dotenv-safe
arguments: [
{
value: 'dotenv-safe',
},
],
},
},
})
.remove()
}
module.exports = function (fileInfo, api, _options) {
const j = api.jscodeshift
const root = j(fileInfo.source)
removeRequireDotConfigCall(root, j)
removeRequireDotEnv(root, j)
// match a function call
return root.toSource()
}