Skip to content

Commit d0e30f5

Browse files
committed
fix: remove brackets after transform
1 parent 9b41b4a commit d0e30f5

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/utils/template.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,7 @@ async function transformJsSnippet(code: string, transform: (code: string) => Pro
221221
// but it can't be transformed.
222222
// We can warp it with `()` to make it a valid js file
223223

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})`)
224+
let res = await transform(`(${code})`)
229225

230226
res = res.trim()
231227

@@ -234,5 +230,13 @@ async function transformJsSnippet(code: string, transform: (code: string) => Pro
234230
res = res.slice(0, -1)
235231
}
236232

233+
// Check if the code was a v-slot destructuring expression like "{ active, ...slotProps }"
234+
// These should not be wrapped in parentheses as Vue template syntax doesn't support it
235+
const isObject = /^\s*\{.*\}\s*$/.test(code)
236+
if (isObject) {
237+
// Remove the parentheses
238+
res = res.match(/^\((.*)\)$/)?.[1] ?? res
239+
}
240+
237241
return res
238242
}

test/template.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ describe('transform typescript template', () => {
4848
expect(
4949
await fixture(`<MyComponent v-slot="{ active, ...slotProps }">{{ active }}</MyComponent>`),
5050
).toEqual(`<MyComponent v-slot="{ active, ...slotProps }">{{ active }}</MyComponent>`)
51+
52+
expect(
53+
await fixture(
54+
`<MyComponent v-slot="{ remaining, duration } as { remaining: number, duration: number }">{{ remaining }}</MyComponent>`,
55+
),
56+
).toMatchInlineSnapshot(`"<MyComponent v-slot="{ remaining, duration }">{{ remaining }}</MyComponent>"`)
5157
})
5258

5359
it('custom directives', async () => {

0 commit comments

Comments
 (0)