Skip to content

Commit d223122

Browse files
committed
fix: do not wrap objects in brackets
1 parent e3c714e commit d223122

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/utils/template.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,7 @@ function handleNode(
126126
}
127127
}
128128

129-
export async function transpileVueTemplate(
130-
content: string,
131-
root: RootNode,
132-
offset = 0,
133-
transform: (code: string) => Promise<string>,
134-
): Promise<string> {
129+
export async function transpileVueTemplate(content: string, root: RootNode, offset = 0, transform: (code: string) => Promise<string>): Promise<string> {
135130
const { MagicString } = await import('vue/compiler-sfc')
136131
const expressions: Expression[] = []
137132

@@ -221,15 +216,16 @@ function getSurrounding(code: string, start: number, end: number) {
221216
: undefined
222217
}
223218

224-
async function transformJsSnippet(
225-
code: string,
226-
transform: (code: string) => Promise<string>,
227-
): Promise<string> {
219+
async function transformJsSnippet(code: string, transform: (code: string) => Promise<string>): Promise<string> {
228220
// `{ key: val } as any` in `<div :style="{ key: val } as any" />` is a valid js snippet,
229221
// but it can't be transformed.
230222
// We can warp it with `()` to make it a valid js file
231223

232-
let res = await transform(`(${code})`)
224+
// Check if the code is a v-slot destructuring expression like "{ active, ...slotProps }"
225+
// These should not be wrapped in parentheses as Vue template syntax doesn't support it
226+
const isObject = /^\s*\{.*\}\s*$/.test(code)
227+
228+
let res = isObject ? await transform(code) : await transform(`(${code})`)
233229

234230
res = res.trim()
235231

test/template.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ describe('transform typescript template', () => {
4444
).toEqual(`<div @click="(e) => handleClick(e)" />`)
4545
})
4646

47+
it('destructuring', async () => {
48+
expect(
49+
await fixture(`<MyComponent v-slot="{ active, ...slotProps }">{{ active }}</MyComponent>`),
50+
).toEqual(`<MyComponent v-slot="{ active, ...slotProps }">{{ active }}</MyComponent>`)
51+
})
52+
4753
it('custom directives', async () => {
4854
expect(
4955
await fixture(`<div v-highlight="(highlight as boolean)" />`),

0 commit comments

Comments
 (0)