Skip to content

Commit fc968d6

Browse files
authored
fix(compiler-core): v-on inline async function expression handler (#4569)
fix #4568
1 parent 141a5e1 commit fc968d6

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/compiler-core/__tests__/transforms/vOn.spec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,58 @@ describe('compiler: transform v-on', () => {
563563
})
564564
})
565565

566+
test('inline async arrow function expression handler', () => {
567+
const { root, node } = parseWithVOn(
568+
`<div v-on:click="async () => await foo()" />`,
569+
{
570+
prefixIdentifiers: true,
571+
cacheHandlers: true
572+
}
573+
)
574+
expect(root.cached).toBe(1)
575+
const vnodeCall = node.codegenNode as VNodeCall
576+
// should not treat cached handler as dynamicProp, so no flags
577+
expect(vnodeCall.patchFlag).toBeUndefined()
578+
expect(
579+
(vnodeCall.props as ObjectExpression).properties[0].value
580+
).toMatchObject({
581+
type: NodeTypes.JS_CACHE_EXPRESSION,
582+
index: 0,
583+
value: {
584+
type: NodeTypes.COMPOUND_EXPRESSION,
585+
children: [`async () => await `, { content: `_ctx.foo` }, `()`]
586+
}
587+
})
588+
})
589+
590+
test('inline async function expression handler', () => {
591+
const { root, node } = parseWithVOn(
592+
`<div v-on:click="async function () { await foo() } " />`,
593+
{
594+
prefixIdentifiers: true,
595+
cacheHandlers: true
596+
}
597+
)
598+
expect(root.cached).toBe(1)
599+
const vnodeCall = node.codegenNode as VNodeCall
600+
// should not treat cached handler as dynamicProp, so no flags
601+
expect(vnodeCall.patchFlag).toBeUndefined()
602+
expect(
603+
(vnodeCall.props as ObjectExpression).properties[0].value
604+
).toMatchObject({
605+
type: NodeTypes.JS_CACHE_EXPRESSION,
606+
index: 0,
607+
value: {
608+
type: NodeTypes.COMPOUND_EXPRESSION,
609+
children: [
610+
`async function () { await `,
611+
{ content: `_ctx.foo` },
612+
`() } `
613+
]
614+
}
615+
})
616+
})
617+
566618
test('inline statement handler', () => {
567619
const { root, node } = parseWithVOn(`<div v-on:click="foo++" />`, {
568620
prefixIdentifiers: true,

packages/compiler-core/src/transforms/vOn.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import { validateBrowserExpression } from '../validateExpression'
1616
import { hasScopeRef, isMemberExpression } from '../utils'
1717
import { TO_HANDLER_KEY } from '../runtimeHelpers'
1818

19-
const fnExpRE = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^\s*function(?:\s+[\w$]+)?\s*\(/
19+
const fnExpRE =
20+
/^\s*([\w$_]+|(async\s*)?\([^)]*?\))\s*=>|^\s*(async\s+)?function(?:\s+[\w$]+)?\s*\(/
2021

2122
export interface VOnDirectiveNode extends DirectiveNode {
2223
// v-on without arg is handled directly in ./transformElements.ts due to it affecting

0 commit comments

Comments
 (0)