Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2a7609

Browse files
committed
feat: implement root-option-to-use transformation
1 parenteaf1ec0 commitc2a7609

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+
importVuefrom'vue'
2+
importAppfrom'./App.vue';
3+
importrouterfrom'./router';
4+
5+
newVue({
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+
importAppfrom'./App.vue';
3+
importrouterfrom'./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
importVuefrom'vue';
2-
importVuexfrom'vuex';
3-
42
importAppfrom'./App.vue';
53
importstorefrom'./store';
6-
7-
Vue.use(Vuex)
4+
importanotherStorefrom'./another-store';
85

96
newVue({
107
store,
118
render:h=>h(App),
129
}).$mount('#app');
10+
11+
newVue({
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+
importAppfrom'./App.vue';
3+
importstorefrom'./store';
4+
importanotherStorefrom'./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+
constaddImport=require('../utils/add-import')
24+
addImport(context,{imported:'createApp'},'vue')
25+
1926
mountCalls.replaceWith(({ node})=>{
2027
letoptions=node.callee.object.arguments[0]
2128
constel=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
constroot=j(fileInfo.source)
55
constcontext={ j, root}
66

7-
constaddImport=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-
constaddImport=require('./utilities/add-import')
1+
constaddImport=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=functionremoveContextualH(context){
9+
module.exports=functionremoveContextualH(context){
1010
const{ j, root}=context
11-
12-
constrenderFns=(root.find(j.Property,{
11+
12+
constrenderFns=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=functionrootOptionToUse(context,rootOptionName){
9+
const{ j, root}=context
10+
11+
constappRoots=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+
constrootOptions=createAppCall.arguments[0]
21+
constpropertyIndex=rootOptions.properties.findIndex(
22+
p=>p.key.name===rootOptionName
23+
)
24+
const[{value:pluginInstance}]=rootOptions.properties.splice(
25+
propertyIndex,
26+
1
27+
)
28+
29+
returnj.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+
constj=api.jscodeshift
4+
constroot=j(fileInfo.source)
5+
6+
// TODO: Vue.use(router) might be in either`router/index.js` or `main.js`
7+
8+
returnroot.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+
constj=api.jscodeshift
4+
constroot=j(fileInfo.source)
5+
6+
returnroot.toSource({lineTerminator:'\n'})
7+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp