Skip to content

Commit 3898f36

Browse files
authored
fix: avoid failing when repeat -a command (#63)
1 parent 3a93301 commit 3898f36

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

transformations/__testfixtures__/vue-router-v4/router-addRoute.output.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ routes.forEach(item => router.addRoute(item))
77

88
router.beforeEach((to,from, next) => {
99
if(permission){
10-
if (Array.isArray(store.getters.addRouters)) {
11-
store.getters.addRouters.forEach(item => router.addRoute(item));
12-
} else {
13-
router.addRoute(store.getters.addRouters);
14-
};
10+
store.getters.addRouters.forEach(item => router.addRoute(item))
1511
}
1612
})
1713

transformations/router/router-update-addRoute.ts

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ASTTransformation } from '../../src/wrapAstTransformation'
33

44
// addRoute() addRoutes()-> forEach addRoute
55
export const transformAST: ASTTransformation = context => {
6+
67
const { root, j } = context
78
const addRouteExpression = root.find(j.CallExpression, {
89
callee: {
@@ -14,39 +15,55 @@ export const transformAST: ASTTransformation = context => {
1415
}
1516
})
1617

17-
addRouteExpression.replaceWith(({ node }) => {
18-
const routerArgs: any = node.arguments[0]
19-
const routerCallee: any = node.callee
20-
21-
if (
22-
!(
23-
routerCallee.object.name === 'router' ||
24-
routerCallee.object.name === 'route'
25-
)
26-
) {
27-
return node
28-
}
29-
30-
const callState = j.callExpression(
31-
j.memberExpression(j.identifier('Array'), j.identifier('isArray'), false),
32-
[routerArgs]
33-
)
18+
if (addRouteExpression.length) {
19+
addRouteExpression.replaceWith(({ node }) => {
20+
// Multiple parameters are not supported
21+
const routerArgs: any = node.arguments[0]
22+
const routerCallee: any = node.callee
3423

35-
const trueCall = j.callExpression(
36-
j.memberExpression(routerArgs, j.identifier('forEach')),
37-
[
38-
j.arrowFunctionExpression(
39-
[j.identifier('item')],
40-
j.callExpression(routerCallee, [j.identifier('item')])
24+
if (
25+
!(
26+
routerCallee.object.name === 'router' ||
27+
routerCallee.object.name === 'route'
4128
)
42-
]
43-
)
29+
) {
30+
return node
31+
}
4432

45-
const trueBlock = j.blockStatement([j.expressionStatement(trueCall)])
46-
const falseBlock = j.blockStatement([j.expressionStatement(node)])
33+
const arrowFun = root.find(j.ArrowFunctionExpression, {
34+
params: [
35+
{
36+
type: 'Identifier',
37+
name: routerArgs.name
38+
}
39+
],
40+
body: {
41+
type: 'CallExpression',
42+
callee: {
43+
type: 'MemberExpression',
44+
property: {
45+
type: 'Identifier',
46+
name: 'addRoute'
47+
}
48+
}
49+
}
50+
})
4751

48-
return j.ifStatement(callState, trueBlock, falseBlock)
49-
})
52+
if (arrowFun.length) {
53+
return node
54+
}
55+
56+
return j.callExpression(
57+
j.memberExpression(routerArgs, j.identifier('forEach')),
58+
[
59+
j.arrowFunctionExpression(
60+
[j.identifier('item')],
61+
j.callExpression(routerCallee, [j.identifier('item')])
62+
)
63+
]
64+
)
65+
})
66+
}
5067

5168
const addRoutesExpression = root.find(j.CallExpression, {
5269
callee: {

0 commit comments

Comments
 (0)