11import type { MagicString } from '@vue-macros/common'
22import type { CallExpression , Node } from '@babel/types'
33import type { RootNodes } from './transform'
4+ import { overwrite } from './common'
45
56export function transformVFor (
67 node : CallExpression ,
@@ -12,27 +13,28 @@ export function transformVFor(
1213 if (
1314 argument . type !== 'FunctionExpression'
1415 && argument . type !== 'ArrowFunctionExpression'
16+ || callee . type !== 'MemberExpression'
1517 )
1618 return
19+ rootNodes . unshift (
20+ {
21+ node : callee . object ,
22+ isAttributeValue : true ,
23+ } ,
24+ {
25+ node : {
26+ ...argument . body ,
27+ type : 'ReturnStatement' ,
28+ } ,
29+ isAttributeValue : true ,
30+ } ,
31+ )
1732
1833 const start = parent ?. type === 'JSXExpressionContainer' ? parent . start ! : node . start !
1934 const end = parent ?. type === 'JSXExpressionContainer' ? parent . end ! : node . end !
20-
21- const left = s . sliceNode ( argument . params )
22- const right = callee . type === 'MemberExpression'
23- ? s . sliceNode ( callee . object )
24- : null
25- const directive = ` v-for="(${ left } ) in ${ right } "`
26-
27- rootNodes . unshift ( {
28- node : {
29- ...argument . body ,
30- type : 'ReturnStatement' ,
31- } ,
32- isAttributeValue : true ,
33- } )
3435 return ( ) => {
35- s . overwrite ( start , argument . body . start ! , `<template${ directive } ><component :is="()=>` )
36+ overwrite ( start , callee . object . start ! , `<template v-for="(${ s . sliceNode ( argument . params ) } ) in ` , s , 'appendRight' )
37+ s . overwrite ( callee . object . end ! , argument . body . start ! , `"><component :is="()=>` )
3638 s . overwrite ( argument . body . end ! , end , `"/></template>` )
3739 }
3840}
0 commit comments