forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-collectionstrings.js
More file actions
89 lines (83 loc) · 2.08 KB
/
remove-collectionstrings.js
File metadata and controls
89 lines (83 loc) · 2.08 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
const j = require('jscodeshift')
// This script transforms the following code:
// const collectionStrings = []
// for (const property in collections) {
// collectionStrings.push(property.toString())
// }
// }
// const trx = await transaction(collectionStrings)
//
// into
//
// const trx = await transaction(collections)
export default (file, _api, { schemaPath }) => {
const ast = j(file.source)
// Remove the collectionStrings variable:
// const collectionStrings = []
ast
.find(j.VariableDeclaration, {
kind: 'const',
declarations: [
{
id: { type: 'Identifier', name: 'collectionStrings' },
},
],
})
.remove()
// Remove the for loop:
// for (const property in collections) {
// collectionStrings.push(property.toString())
// }
ast
.find(j.ForInStatement, {
type: 'ForInStatement',
left: {
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
id: { type: 'Identifier', name: 'property' },
},
],
},
right: { type: 'Identifier', name: 'collections' },
body: { type: 'BlockStatement', body: [{ type: 'ExpressionStatement' }] },
})
.remove()
// Change the call to transaction from
// const trx = await transaction(collectionStrings)
// to
// const trx = await transaction(collections)
const trx = ast
.find(j.VariableDeclaration, {
kind: 'const',
declarations: [
{
id: {
type: 'Identifier',
name: 'trx',
},
init: {
type: 'AwaitExpression',
argument: {
type: 'CallExpression',
callee: { name: 'transaction' },
},
},
},
],
})
.replaceWith((path) => {
const node = path.value
node.declarations[0].init.argument.arguments[0] = j.identifier(
'collections',
)
return node
})
return ast.toSource({
trailingComma: true,
arrowParensAlways: true,
quote: 'single',
})
}