Skip to content

Commit 267d977

Browse files
committed
split app mount expression
createApp().mount() => app=createApp();app.mount()
1 parent 448f2f7 commit 267d977

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

Diff for: generator/codemods/global-api/create-app-mount.js

+35-17
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ module.exports = function createAppMount(context) {
77
const { j, root } = context
88

99
// new Vue(...).$mount()
10-
const mountCalls = root.find(j.CallExpression, n => {
11-
return (
12-
n.callee.type === 'MemberExpression' &&
13-
n.callee.property.name === '$mount' &&
14-
n.callee.object.type === 'NewExpression' &&
15-
n.callee.object.callee.name === 'Vue'
16-
)
10+
const mountCalls = root.find(j.ExpressionStatement, {
11+
expression: {
12+
type: 'CallExpression',
13+
callee: {
14+
type: 'MemberExpression',
15+
object: {
16+
type: 'NewExpression',
17+
callee: {
18+
type: 'Identifier',
19+
name: 'Vue'
20+
}
21+
},
22+
property: { type: 'Identifier', name: '$mount' }
23+
}
24+
}
1725
})
1826

1927
if (!mountCalls.length) {
@@ -23,16 +31,26 @@ module.exports = function createAppMount(context) {
2331
const addImport = require('../utils/add-import')
2432
addImport(context, { imported: 'createApp' }, 'vue')
2533

26-
mountCalls.replaceWith(({ node }) => {
27-
let rootProps = node.callee.object.arguments[0]
28-
const el = node.arguments[0]
34+
const rootProps = mountCalls.at(0).get().node.expression.callee.object
35+
.arguments
36+
mountCalls.insertBefore(
37+
j.variableDeclaration('const', [
38+
j.variableDeclarator(
39+
j.identifier('app'),
40+
j.callExpression(j.identifier('createApp'), rootProps)
41+
)
42+
])
43+
)
2944

30-
return j.callExpression(
31-
j.memberExpression(
32-
j.callExpression(j.identifier('createApp'), [rootProps]),
33-
j.identifier('mount')
34-
),
35-
[el]
45+
const args = mountCalls.at(0).get().node.expression.arguments
46+
mountCalls.insertBefore(
47+
j.expressionStatement(
48+
j.callExpression(
49+
j.memberExpression(j.identifier('app'), j.identifier('mount'), false),
50+
args
51+
)
3652
)
37-
})
53+
)
54+
55+
mountCalls.remove()
3856
}

0 commit comments

Comments
 (0)