Skip to content

Commit d3a27d5

Browse files
feat: move app.xxx between createApp() and .mount() (#103)
1 parent 4635eb5 commit d3a27d5

File tree

7 files changed

+149
-23
lines changed

7 files changed

+149
-23
lines changed

bin/vue-codemod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { transform as packageTransform } from '../src/packageTransformation'
1919

2020
import type { TransformationModule } from '../src/runTransformation'
2121
import { formatterOutput } from '../src/report'
22-
import { ruleDescripition } from '../src/ruleDescription'
22+
import { ruleDescription } from '../src/ruleDescription'
2323
import cliProgress from 'cli-progress'
2424

2525
const debug = createDebug('vue-codemod:cli')
@@ -233,13 +233,13 @@ function processTransformation(
233233
if (formatter === 'log')
234234
logger.timeEnd(`Processing use ${transformationName} transformation`)
235235
if (
236-
ruleDescripition.hasOwnProperty(transformationName) &&
236+
ruleDescription.hasOwnProperty(transformationName) &&
237237
(formatter === 'detail' || formatter === 'log')
238238
) {
239239
let ruleOutput: { [key: string]: any } = {}
240240
ruleOutput.rule_name = transformationName
241241
// @ts-ignore
242-
ruleOutput.website = ruleDescripition[transformationName].description
242+
ruleOutput.website = ruleDescription[transformationName].description
243243
ruleOutput.transformed_files = ruleProcessFile
244244
console.log('\x1B[0m', ruleOutput)
245245
if (formatter === 'log') logger.log(ruleOutput)

src/ruleDescription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export let ruleDescripition = {
1+
export let ruleDescription = {
22
'new-component-api': {
33
description:
44
'https://v3.vuejs.org/guide/migration/global-api.html#a-new-global-api-createapp'

transformations/__tests__/global-filter.spec.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ app.config.globalProperties.$filters = {
1717
'transform global filter'
1818
)
1919

20+
defineInlineTest(
21+
transform,
22+
{},
23+
`const app = Vue.createApp(App).use(store).use(router)
24+
app.component()
25+
Vue.filter('capitalize', function(value) {
26+
return value
27+
})`,
28+
`const app = Vue.createApp(App).use(store).use(router)
29+
app.component()
30+
app.config.globalProperties.$filters = {
31+
capitalize(value) {
32+
return value
33+
}
34+
};`,
35+
'transform global filter'
36+
)
37+
2038
defineInlineTest(
2139
transform,
2240
{},
@@ -31,7 +49,3 @@ Vue.filter('capitalize', function(value) {
3149
`,
3250
'transform global filter(no effect and will warn)'
3351
)
34-
35-
36-
37-
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { defineInlineTest } from 'jscodeshift/src/testUtils'
2+
3+
const transform = require('../move-app-mount')
4+
5+
6+
defineInlineTest(
7+
transform,
8+
{},
9+
`app.directive('demo', {})
10+
const app = Vue.createApp(App).use(button_counter).use(router).use(store);
11+
app.mount("#app");
12+
app.component('myComponent',{})
13+
app.config.globalProperties.$filters = {
14+
myFilter(value) {
15+
if (!value)return ''
16+
return value.toUpperCase()
17+
}
18+
};`,
19+
`const app = Vue.createApp(App).use(button_counter).use(router).use(store);
20+
app.directive('demo', {})
21+
app.component('myComponent',{})
22+
app.config.globalProperties.$filters = {
23+
myFilter(value) {
24+
if (!value)return ''
25+
return value.toUpperCase()
26+
}
27+
};
28+
app.mount("#app");`,
29+
'move app.mount to after all the app.* '
30+
)
31+
32+
defineInlineTest(
33+
transform,
34+
{},
35+
`Vue.directive('demo', {})
36+
Vue.createApp(App).use(button_counter).use(router).use(store).mount("#app");
37+
Vue.component('myComponent',{})
38+
`,
39+
`Vue.directive('demo', {})
40+
Vue.createApp(App).use(button_counter).use(router).use(store).mount("#app");
41+
Vue.component('myComponent',{})`,
42+
'do not change the code without app.mount'
43+
)

transformations/global-filter.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@ import { getCntFunc } from '../src/report'
55
export const transformAST: ASTTransformation = ({ root, j }) => {
66
const cntFunc = getCntFunc('global-filter', global.outputReport)
77
// find the createApp()
8-
const appDeclare = root.find(j.VariableDeclarator, {
9-
id: { type: 'Identifier' },
10-
init: {
11-
type: 'CallExpression',
12-
callee: {
13-
object: {
14-
name: 'Vue'
15-
},
16-
property: {
17-
name: 'createApp'
18-
}
19-
}
8+
const constApp = root.find(j.VariableDeclarator, {
9+
id: {
10+
name: 'app'
2011
}
2112
})
13+
if (constApp.length <= 0) {
14+
return
15+
}
16+
const vueCreateApp = j(constApp?.at(0).get().value.init).find(
17+
j.MemberExpression,
18+
{
19+
object: {
20+
name: 'Vue'
21+
},
22+
property: {
23+
name: 'createApp'
24+
}
25+
}
26+
)
2227

23-
if (!appDeclare.length) {
28+
if (!constApp.length || !vueCreateApp.length) {
2429
return
2530
}
26-
const appName = appDeclare.at(0).get().node.id.name
31+
const appName = constApp.at(0).get().value.id.name
2732

2833
// Vue.filter('filterName', function(value) {}) =>
2934
// app.config.globalProperties.$filters = { filterName(value) {} }

transformations/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const transformationMap: {
2020
'remove-vue-set-and-delete': require('./remove-vue-set-and-delete'),
2121
'rename-lifecycle': require('./rename-lifecycle'),
2222
'add-emit-declaration': require('./add-emit-declaration'),
23-
'global-filter': require('./global-filter'),
2423
'tree-shaking': require('./tree-shaking'),
2524
'v-model': require('./v-model'),
2625
'render-to-resolveComponent': require('./render-to-resolveComponent'),
@@ -44,8 +43,12 @@ const transformationMap: {
4443
'router-update-addRoute': require('./router/router-update-addRoute'),
4544

4645
'const-app': require('./const-app'),
46+
// need to use 'const app=Vue.createApp'
47+
'global-filter': require('./global-filter'),
48+
'move-app-mount': require('./move-app-mount'),
4749

4850
// manual (must be used at the end of list)
51+
// rule's name must be start with 'manual-'
4952
'manual-remove-Vue': require('./manual/manual-remove-Vue'),
5053
'manual-remove-VueRouter': require('./manual/manual-remove-VueRouter'),
5154
'manual-remove-on-off-once': require('./manual/manual-remove-on-off-once'),

transformations/move-app-mount.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import wrap from '../src/wrapAstTransformation'
2+
import type { ASTTransformation } from '../src/wrapAstTransformation'
3+
import { getCntFunc } from '../src/report'
4+
5+
export const transformAST: ASTTransformation = ({ root, j }) => {
6+
const cntFunc = getCntFunc('move-app-mount', global.outputReport)
7+
8+
const appMount = root.find(j.ExpressionStatement, {
9+
expression: {
10+
callee: {
11+
object: {
12+
name: 'app'
13+
},
14+
property: {
15+
name: 'mount'
16+
}
17+
}
18+
}
19+
})
20+
if (appMount.length !== 1) {
21+
return
22+
}
23+
const appMountOnly = appMount.get()
24+
let lastend = appMountOnly.value.end
25+
let appMountEnd = appMountOnly.value.end
26+
let lastNode = appMountOnly
27+
const context = appMountOnly.value.expression?.arguments[0].value
28+
root.find(j.ExpressionStatement).forEach(node => {
29+
if (
30+
j(node).find(j.Identifier, {
31+
name: 'app'
32+
}).length > 0
33+
) {
34+
// @ts-ignore
35+
const end = node.value.end
36+
if (end < appMountEnd) {
37+
appMount.insertBefore(node.value)
38+
j(node).remove()
39+
}
40+
if (end > lastend) {
41+
lastend = end
42+
lastNode = node
43+
}
44+
}
45+
})
46+
if (lastend !== appMountOnly.value.end) {
47+
appMount.remove()
48+
j(lastNode).insertAfter(
49+
j.expressionStatement(
50+
j.callExpression(
51+
j.memberExpression(j.identifier('app'), j.identifier('mount')),
52+
[j.stringLiteral(context)]
53+
)
54+
)
55+
)
56+
}
57+
cntFunc()
58+
}
59+
60+
export default wrap(transformAST)
61+
export const parser = 'babylon'

0 commit comments

Comments
 (0)