-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathroot-prop-to-use.ts
64 lines (55 loc) · 1.65 KB
/
root-prop-to-use.ts
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
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
import * as N from 'jscodeshift'
type Params = {
rootPropName: string
}
/**
* Expected to be run after the `createApp` transformation.
* Transforms expressions like `createApp({ router })` to `createApp().use(router)`
*/
export const transformAST: ASTTransformation<Params> = (
{ root, j },
{ rootPropName }
) => {
const appRoots = root.find(j.CallExpression, (node: N.CallExpression) => {
if (
node.arguments.length === 1 &&
j.ObjectExpression.check(node.arguments[0])
) {
if (j.Identifier.check(node.callee) && node.callee.name === 'createApp') {
return true
}
if (
j.MemberExpression.check(node.callee) &&
j.Identifier.check(node.callee.object) &&
node.callee.object.name === 'Vue' &&
j.Identifier.check(node.callee.property) &&
node.callee.property.name === 'createApp'
) {
return true
}
}
})
appRoots.replaceWith(({ node: createAppCall }) => {
const rootProps = createAppCall.arguments[0] as N.ObjectExpression
const propertyIndex = rootProps.properties.findIndex(
// @ts-ignore
(p) => p.key && p.key.name === rootPropName
)
if (propertyIndex === -1) {
return createAppCall
}
// @ts-ignore
const [{ value: pluginInstance }] = rootProps.properties.splice(
propertyIndex,
1
)
return j.callExpression(
j.memberExpression(createAppCall, j.identifier('use')),
[pluginInstance]
)
})
}
export default wrap(transformAST)
export const parser = 'babylon'