-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathroot-prop-to-use.js
37 lines (34 loc) · 1.12 KB
/
root-prop-to-use.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
/**
* Expected to be run after the `createApp` transformation.
* Transforms expressions like `createApp({ router })` to `createApp().use(router)`
* @param {Object} context
* @param {import('jscodeshift').JSCodeshift} context.j
* @param {ReturnType<import('jscodeshift').Core>} context.root
*/
module.exports = function rootPropToUse(context, rootPropName) {
const { j, root } = context
const appRoots = root.find(j.CallExpression, {
callee: { name: 'createApp' },
arguments: args =>
args.length === 1 &&
args[0].type === 'ObjectExpression' &&
args[0].properties.find(p => p.key.name === rootPropName)
})
appRoots.replaceWith(({ node: createAppCall }) => {
const rootProps = createAppCall.arguments[0]
const propertyIndex = rootProps.properties.findIndex(
p => p.key.name === rootPropName
)
const [{ value: pluginInstance }] = rootProps.properties.splice(
propertyIndex,
1
)
return j.callExpression(
j.memberExpression(
j.callExpression(j.identifier('createApp'), [rootProps]),
j.identifier('use')
),
[pluginInstance]
)
})
}