Skip to content

Commit 9e2dd61

Browse files
committed
feat: support double quote in v-for
1 parent 11f8ea2 commit 9e2dd61

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/core/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export function overwrite(
2020
end: number | undefined,
2121
content: string,
2222
s: MagicString,
23+
method: 'prependLeft' | 'prependRight' | 'appendLeft' | 'appendRight' = 'prependLeft',
2324
) {
2425
if (start === end) {
25-
s.prependLeft(start!, content)
26+
s[method](start!, content)
2627
}
2728
else {
2829
s.overwrite(start!, end!, content)

src/core/transform.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export function transformVueJsxVapor(
4545
&& /("|<.*?\/.*?>)/.test(s.sliceNode(node.expression))
4646
) {
4747
rootNodes.unshift({
48-
node: node.expression,
48+
node: parent.name.name === 'v-for' && node.expression.type === 'BinaryExpression'
49+
? node.expression.right
50+
: node.expression,
4951
postCallbacks: [],
5052
isAttributeValue: true,
5153
})
@@ -176,7 +178,7 @@ export function transformVueJsxVapor(
176178
postCallbacks?.forEach(callback => callback?.())
177179

178180
const result = compile(node)
179-
.replaceAll(/__PLACEHOLDER_(\d)/g, (_, $1) => placeholders[$1])
181+
.replaceAll(/__PLACEHOLDER_(\d+)/g, (_, $1) => placeholders[$1])
180182
if (isAttributeValue) {
181183
s.overwriteNode(node, `__PLACEHOLDER_${placeholders.push(result) - 1}`)
182184
}

src/core/v-for.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { MagicString } from '@vue-macros/common'
22
import type { CallExpression, Node } from '@babel/types'
33
import type { RootNodes } from './transform'
4+
import { overwrite } from './common'
45

56
export 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

Comments
 (0)