Skip to content

Commit c2a7609

Browse files
committed
feat: implement root-option-to-use transformation
1 parent eaf1ec0 commit c2a7609

19 files changed

+96
-43
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Vue from 'vue'
2+
import App from './App.vue';
3+
import router from './router';
4+
5+
new Vue({
6+
router,
7+
render: (h) => h(App),
8+
}).$mount('#app');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createApp } from 'vue';
2+
import App from './App.vue';
3+
import router from './router';
4+
5+
createApp(App).use(router).mount('#app');

generator/codemods/global-api/__testfixtures__/vuex-basic-2.input.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

generator/codemods/global-api/__testfixtures__/vuex-basic-2.output.js

Whitespace-only changes.

generator/codemods/global-api/__testfixtures__/vuex-basic.output.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Vue from 'vue';
2-
import Vuex from 'vuex';
3-
42
import App from './App.vue';
53
import store from './store';
6-
7-
Vue.use(Vuex)
4+
import anotherStore from './another-store';
85

96
new Vue({
107
store,
118
render: h => h(App),
129
}).$mount('#app');
10+
11+
new Vue({
12+
store: anotherStore,
13+
render: h => h(App),
14+
}).$mount('#app');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createApp } from 'vue';
2+
import App from './App.vue';
3+
import store from './store';
4+
import anotherStore from './another-store';
5+
6+
createApp(App).use(store).mount('#app');
7+
8+
createApp(App).use(anotherStore).mount('#app');

generator/codemods/global-api/__tests__/global-api-test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ const { defineTest } = require('jscodeshift/dist/testUtils')
55
defineTest(__dirname, 'index', null, 'basic')
66
defineTest(__dirname, 'index', null, 'custom-root-option')
77
// defineTest(__dirname, 'index', null, 'el')
8-
// defineTest(__dirname, 'index', null, 'vuex-basic')
9-
// defineTest(__dirname, 'index', null, 'vuex-basic-2')
10-
// defineTest(__dirname, 'index', null, 'vuex-store')
8+
defineTest(__dirname, 'index', null, 'vue-router')
9+
defineTest(__dirname, 'index', null, 'vuex')

generator/codemods/global-api/create-app-mount.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ module.exports = function createAppMount(context) {
1616
)
1717
})
1818

19+
if (!mountCalls.length) {
20+
return
21+
}
22+
23+
const addImport = require('../utils/add-import')
24+
addImport(context, { imported: 'createApp' }, 'vue')
25+
1926
mountCalls.replaceWith(({ node }) => {
2027
let options = node.callee.object.arguments[0]
2128
const el = node.arguments[0]

generator/codemods/global-api/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ module.exports = function(fileInfo, api) {
44
const root = j(fileInfo.source)
55
const context = { j, root }
66

7-
const addImport = require('./utilities/add-import')
8-
addImport(context, { imported: 'createApp' }, 'vue')
9-
107
require('./create-app-mount')(context)
11-
require('./vuex')(context)
12-
require('./router')(context)
8+
require('./root-option-to-use')(context, 'store')
9+
require('./root-option-to-use')(context, 'router')
1310
require('./remove-trivial-root')(context)
1411
require('./remove-production-tip')(context)
1512
require('./remove-contextual-h')(context)

generator/codemods/global-api/remove-contextual-h.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const addImport = require('./utilities/add-import')
1+
const addImport = require('../utils/add-import')
22

33
/**
44
* replace `render: h => h(App)` with `render: () => h(App)
55
* @param {Object} context
66
* @param {import('jscodeshift').JSCodeshift} context.j
77
* @param {ReturnType<import('jscodeshift').Core>} context.root
88
*/
9-
module.exports = function removeContextualH (context) {
9+
module.exports = function removeContextualH(context) {
1010
const { j, root } = context
11-
12-
const renderFns = (root.find(j.Property, {
11+
12+
const renderFns = root.find(j.Property, {
1313
key: {
1414
name: 'render'
1515
},
1616
value: {
1717
type: 'ArrowFunctionExpression'
1818
}
19-
}))
19+
})
2020
if (renderFns.length) {
2121
addImport(context, { imported: 'h' }, 'vue')
2222
renderFns.forEach(({ node }) => {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Expected to be run after the `createApp` transformation.
3+
* Transforms expressions like `createApp({ router })` to `createApp().use(router)`
4+
* @param {Object} context
5+
* @param {import('jscodeshift').JSCodeshift} context.j
6+
* @param {ReturnType<import('jscodeshift').Core>} context.root
7+
*/
8+
module.exports = function rootOptionToUse(context, rootOptionName) {
9+
const { j, root } = context
10+
11+
const appRoots = root.find(j.CallExpression, {
12+
callee: { name: 'createApp' },
13+
arguments: args =>
14+
args.length === 1 &&
15+
args[0].type === 'ObjectExpression' &&
16+
args[0].properties.find(p => p.key.name === rootOptionName)
17+
})
18+
19+
appRoots.replaceWith(({ node: createAppCall }) => {
20+
const rootOptions = createAppCall.arguments[0]
21+
const propertyIndex = rootOptions.properties.findIndex(
22+
p => p.key.name === rootOptionName
23+
)
24+
const [{ value: pluginInstance }] = rootOptions.properties.splice(
25+
propertyIndex,
26+
1
27+
)
28+
29+
return j.callExpression(
30+
j.memberExpression(
31+
j.callExpression(j.identifier('createApp'), [rootOptions]),
32+
j.identifier('use')
33+
),
34+
[pluginInstance]
35+
)
36+
})
37+
}

generator/codemods/global-api/router.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

generator/codemods/global-api/vuex.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

generator/codemods/router/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** @type {import('jscodeshift').Transform} */
2+
module.exports = function(fileInfo, api) {
3+
const j = api.jscodeshift
4+
const root = j(fileInfo.source)
5+
6+
// TODO: Vue.use(router) might be in either`router/index.js` or `main.js`
7+
8+
return root.toSource({ lineTerminator: '\n' })
9+
}

generator/codemods/vuex/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('jscodeshift').Transform} */
2+
module.exports = function(fileInfo, api) {
3+
const j = api.jscodeshift
4+
const root = j(fileInfo.source)
5+
6+
return root.toSource({ lineTerminator: '\n' })
7+
}

0 commit comments

Comments
 (0)