-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
implicit-params.js
39 lines (33 loc) · 1.06 KB
/
implicit-params.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
import it from 'param.macro'
import { findTargetCallee, PartialError } from './util'
export default function transformImplicitParams (t, refs) {
refs.forEach(referencePath => {
const parent =
findTargetCallee(referencePath) ??
referencePath.findParent(it.isVariableDeclarator())?.get('init')
if (!parent) {
throw new PartialError(
'Implicit parameters must be used as function arguments or the\n' +
'right side of a variable declaration, ie. `const identity = it`)'
)
}
if (parent.getData('it.wasTransformed')) {
parent.scope.rename(
referencePath.node.name,
parent.getData('it.idName')
)
return
}
const id = parent.scope.generateUidIdentifier('it')
parent.scope.rename(referencePath.node.name, id.name)
const fn = t.arrowFunctionExpression(
[id],
t.blockStatement([
t.returnStatement(parent.node)
])
)
const [result] = parent.replaceWith(fn)
result.setData('it.wasTransformed', true)
result.setData('it.idName', id.name)
})
}