Skip to content

Commit d9e3ff6

Browse files
author
Alan Wang
authored
fix: avoid conflicts between defined variables and imported specifier (#64)
1 parent 3898f36 commit d9e3ff6

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import VueRouter from 'vue-router';
2+
import Home from '../views/Home.vue';
3+
4+
const routes = [
5+
{
6+
path: '/',
7+
name: 'home',
8+
component: Home,
9+
},
10+
{
11+
path: '/about',
12+
name: 'about',
13+
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
14+
},
15+
];
16+
const createRouter = () => new VueRouter({
17+
mode: 'history',
18+
base: process.env.BASE_URL,
19+
routes
20+
});
21+
22+
const router = createRouter()
23+
24+
export default router;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createRouter as newCreateRouter, createWebHistory } from 'vue-router';
2+
import Home from '../views/Home.vue';
3+
4+
const routes = [
5+
{
6+
path: '/',
7+
name: 'home',
8+
component: Home,
9+
},
10+
{
11+
path: '/about',
12+
name: 'about',
13+
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
14+
},
15+
];
16+
const createRouter = () => newCreateRouter({
17+
history: createWebHistory(process.env.BASE_URL),
18+
routes
19+
});
20+
21+
const router = createRouter()
22+
23+
export default router;

transformations/__tests__/vue-router-v4.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ jest.autoMockOff()
33
import { defineTest } from 'jscodeshift/src/testUtils'
44

55
defineTest(__dirname, 'vue-router-v4', {}, 'vue-router-v4/create-router')
6-
76
defineTest(__dirname, 'vue-router-v4', {}, 'vue-router-v4/create-history')
8-
7+
defineTest(__dirname, 'vue-router-v4', {}, 'vue-router-v4/rename-create-router')
98
defineTest(__dirname, 'router/router4-onready-to-isready', {}, 'vue-router-v4/router-ready')
10-
119
defineTest(__dirname, 'router/router-update-addRoute', {}, 'vue-router-v4/router-addRoute')

transformations/vue-router-v4.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,28 @@ export const transformAST: ASTTransformation = context => {
2626
}
2727
})
2828

29-
addImport(context, {
30-
specifier: { type: 'named', imported: 'createRouter' },
31-
source: 'vue-router'
29+
let localCreateRouter = 'createRouter'
30+
// find whether createRouter has been used
31+
const createRouterIdentifiers = root.find(j.Identifier, {
32+
name: 'createRouter'
3233
})
34+
if (createRouterIdentifiers.length) {
35+
// rename createRouter to newCreateRouter
36+
localCreateRouter = 'newCreateRouter'
37+
addImport(context, {
38+
specifier: {
39+
type: 'named',
40+
imported: 'createRouter',
41+
local: localCreateRouter
42+
},
43+
source: 'vue-router'
44+
})
45+
} else {
46+
addImport(context, {
47+
specifier: { type: 'named', imported: 'createRouter' },
48+
source: 'vue-router'
49+
})
50+
}
3351
newVueRouter.replaceWith(({ node }) => {
3452
// mode: 'history' -> history: createWebHistory(), etc
3553
let historyMode = 'createWebHashHistory'
@@ -89,7 +107,7 @@ export const transformAST: ASTTransformation = context => {
89107
)
90108
)
91109

92-
return j.callExpression(j.identifier('createRouter'), node.arguments)
110+
return j.callExpression(j.identifier(localCreateRouter), node.arguments)
93111
})
94112

95113
// VueRouter.START_LOCATION => import {START_LOCATION} from 'vue-router'

0 commit comments

Comments
 (0)