@@ -3,6 +3,7 @@ import type { ASTTransformation } from '../../src/wrapAstTransformation'
3
3
4
4
// addRoute() addRoutes()-> forEach addRoute
5
5
export const transformAST : ASTTransformation = context => {
6
+
6
7
const { root, j } = context
7
8
const addRouteExpression = root . find ( j . CallExpression , {
8
9
callee : {
@@ -14,39 +15,55 @@ export const transformAST: ASTTransformation = context => {
14
15
}
15
16
} )
16
17
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
34
23
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'
41
28
)
42
- ]
43
- )
29
+ ) {
30
+ return node
31
+ }
44
32
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
+ } )
47
51
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
+ }
50
67
51
68
const addRoutesExpression = root . find ( j . CallExpression , {
52
69
callee : {
0 commit comments