This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrereselect.js
104 lines (98 loc) · 2.52 KB
/
rereselect.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// https://astexplorer.net/
export default function transformer(file, api) {
const j = api.jscodeshift;
var feature = 'tw-tasklists/'
var root = j(file.source)
function queryFn(fn, args) {
return j.arrowFunctionExpression(
[j.identifier('query')],
j.blockStatement([
...fn.params.map((a,i) => {
return j.variableDeclaration(
'const',
[
j.variableDeclarator(
a,
j.callExpression(j.identifier('query'), [
args[i]
])
)
]
)
}),
...fn.body.type === 'BlockStatement' ? fn.body.body : [j.returnStatement(fn.body)]
])
)
}
root
.find(j.VariableDeclarator, {
init: {
callee: { name: 'createSelector' }
}
})
.forEach(path => {
var fn = path.node.init.arguments.slice(-1)[0]
j(path.get('init')).replaceWith(
j.callExpression(
j.identifier('makeNamedSelector'),
[
j.stringLiteral(feature + path.node.id.name),
queryFn(fn, path.node.init.arguments)
]
)
)
})
root
.find(j.VariableDeclarator, {
init: {
body: {
callee: { name: 'createSelector' }
}
}
})
.forEach(path => {
var fn = path.node.init.body.arguments.slice(-1)[0]
j(path.get('init')).replaceWith(
j.callExpression(
j.identifier('makeParameterizedSelector'),
[
j.stringLiteral(feature + path.node.id.name),
j.arrowFunctionExpression(
path.node.init.params,
queryFn(fn, path.node.init.body.arguments)
)
]
)
)
})
root
.find(j.VariableDeclarator, {
init: {
callee: {
object: { name: '_' },
property: { name: 'memoize', }
},
arguments: [{
body: {
callee: { name: 'createSelector' }
}
}]
}
})
.forEach(path => {
var fn = path.node.init.arguments[0].body.arguments.slice(-1)[0]
j(path.get('init')).replaceWith(
j.callExpression(
j.identifier('makeParameterizedSelector'),
[
j.stringLiteral(feature + path.node.id.name),
j.arrowFunctionExpression(
path.node.init.arguments[0].params,
queryFn(fn, path.node.init.arguments[0].body.arguments)
)
]
)
)
})
return root.toSource();
}