Skip to content

Commit 6719d0c

Browse files
committed
feat: implement vuex transform
1 parent 660cace commit 6719d0c

File tree

10 files changed

+128
-18
lines changed

10 files changed

+128
-18
lines changed

generator/codemods/router/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = function(fileInfo, api) {
33
const j = api.jscodeshift
44
const root = j(fileInfo.source)
55

6-
// TODO: Vue.use(router) might be in either`router/index.js` or `main.js`
6+
// TODO: new Router() -> createRouter()
7+
// TODO: mode: 'history' -> history: createWebHistory()
78

89
return root.toSource({ lineTerminator: '\n' })
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Store as TheStore } from 'vuex';
2+
3+
const store = new TheStore({
4+
state () {
5+
return {
6+
count: 1
7+
}
8+
}
9+
});
10+
11+
export default store;
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { createStore } from 'vuex'
1+
import { createStore } from 'vuex';
22

33
const store = createStore({
44
state () {
55
return {
66
count: 1
77
}
88
}
9-
})
9+
});
1010

11-
export default store
11+
export default store;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Store } from 'vuex';
2+
3+
const store = new Store({
4+
state () {
5+
return {
6+
count: 1
7+
}
8+
}
9+
});
10+
11+
export default store;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createStore } from 'vuex';
2+
3+
const store = createStore({
4+
state () {
5+
return {
6+
count: 1
7+
}
8+
}
9+
});
10+
11+
export default store;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Vuex from 'vuex';
2+
3+
const store = new Vuex.Store({
4+
state () {
5+
return {
6+
count: 1
7+
}
8+
}
9+
});
10+
11+
export default store;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Vuex from 'vuex';
2+
3+
const store = Vuex.createStore({
4+
state () {
5+
return {
6+
count: 1
7+
}
8+
}
9+
});
10+
11+
export default store;

generator/codemods/vuex/__testfixtures__/vuex-store.input.js

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
jest.autoMockOff()
2+
3+
const { defineTest } = require('jscodeshift/dist/testUtils')
4+
5+
defineTest(__dirname, 'index', null, 'store')
6+
defineTest(__dirname, 'index', null, 'vuex-dot-store')
7+
defineTest(__dirname, 'index', null, 'import-alias')

generator/codemods/vuex/index.js

+61
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,68 @@
1+
const addImport = require('../utils/add-import')
2+
const removeExtraneousImport = require('../utils/remove-extraneous-import')
3+
14
/** @type {import('jscodeshift').Transform} */
25
module.exports = function(fileInfo, api) {
36
const j = api.jscodeshift
47
const root = j(fileInfo.source)
8+
const context = { j, root }
9+
10+
// TODO: new Store() -> createStore()
11+
12+
const vuexImportDecls = root.find(j.ImportDeclaration, {
13+
source: {
14+
value: 'vuex'
15+
}
16+
})
17+
18+
const importedVuex = vuexImportDecls.find(j.ImportDefaultSpecifier)
19+
const importedStore = vuexImportDecls.find(j.ImportSpecifier, {
20+
imported: {
21+
name: 'Store'
22+
}
23+
})
24+
25+
if (importedVuex.length) {
26+
const localVuex = importedVuex.get(0).node.local.name
27+
const newVuexDotStore = root.find(j.NewExpression, {
28+
callee: {
29+
type: 'MemberExpression',
30+
object: {
31+
type: 'Identifier',
32+
name: localVuex
33+
},
34+
property: {
35+
name: 'Store'
36+
}
37+
}
38+
})
39+
40+
newVuexDotStore.replaceWith(({ node }) => {
41+
return j.callExpression(
42+
j.memberExpression(
43+
j.identifier(localVuex),
44+
j.identifier('createStore')
45+
),
46+
node.arguments
47+
)
48+
})
49+
}
50+
51+
if (importedStore.length) {
52+
const localStore = importedStore.get(0).node.local.name
53+
const newStore = root.find(j.NewExpression, {
54+
callee: {
55+
type: 'Identifier',
56+
name: localStore
57+
}
58+
})
59+
60+
addImport(context, { imported: 'createStore' }, 'vuex')
61+
newStore.replaceWith(({ node }) => {
62+
return j.callExpression(j.identifier('createStore'), node.arguments)
63+
})
64+
removeExtraneousImport(context, localStore)
65+
}
566

667
return root.toSource({ lineTerminator: '\n' })
768
}

0 commit comments

Comments
 (0)