Skip to content

Commit f5d1169

Browse files
committed
codemod for createSelector
1 parent a95469a commit f5d1169

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

transforms/to-array-dependencies.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = function(file, api) {
2+
const j = api.jscodeshift
3+
const firstArgIsArray = p => {
4+
const [firstArg] = p.value.arguments
5+
6+
if (firstArg.type === 'ArrayExpression') return true
7+
if (firstArg.type !== 'Identifier') return false
8+
9+
const x = p.scope.lookup(firstArg.name)
10+
const decl = j(x.getBindings()[firstArg.name][0]).closest(
11+
j.VariableDeclaration
12+
)
13+
14+
return (
15+
Boolean(decl.size()) &&
16+
decl.nodes()[0].declarations[0].init.type === 'ArrayExpression'
17+
)
18+
}
19+
return j(file.source)
20+
.find(j.CallExpression, {
21+
callee: {
22+
name: 'createSelector'
23+
}
24+
})
25+
.filter(
26+
p =>
27+
p.value.arguments.length > 2 ||
28+
(p.value.arguments.length === 2 && !firstArgIsArray(p))
29+
)
30+
.replaceWith(p =>
31+
j.callExpression(j.identifier('createSelector'), [
32+
j.arrayExpression(p.value.arguments.slice(0, -1)),
33+
p.value.arguments[p.value.arguments.length - 1]
34+
])
35+
)
36+
.toSource()
37+
}

0 commit comments

Comments
 (0)