Skip to content

Commit ca7d421

Browse files
authored
fix(compiler-core): add support for arrow aysnc function with unbracketed (#5789)
close #5788
1 parent f750c41 commit ca7d421

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,23 @@ describe('compiler: transform v-on', () => {
286286
})
287287
})
288288

289+
test('should NOT wrap as function if expression is already function expression (async)', () => {
290+
const { node } = parseWithVOn(
291+
`<div @click="async $event => await foo($event)"/>`,
292+
)
293+
expect((node.codegenNode as VNodeCall).props).toMatchObject({
294+
properties: [
295+
{
296+
key: { content: `onClick` },
297+
value: {
298+
type: NodeTypes.SIMPLE_EXPRESSION,
299+
content: `async $event => await foo($event)`,
300+
},
301+
},
302+
],
303+
})
304+
})
305+
289306
test('should NOT wrap as function if expression is already function expression (with newlines)', () => {
290307
const { node } = parseWithVOn(
291308
`<div @click="
@@ -630,6 +647,39 @@ describe('compiler: transform v-on', () => {
630647
})
631648
})
632649

650+
test('inline async arrow function with no bracket expression handler', () => {
651+
const { root, node } = parseWithVOn(
652+
`<div v-on:click="async e => await foo(e)" />`,
653+
{
654+
prefixIdentifiers: true,
655+
cacheHandlers: true,
656+
},
657+
)
658+
659+
expect(root.cached).toBe(1)
660+
const vnodeCall = node.codegenNode as VNodeCall
661+
// should not treat cached handler as dynamicProp, so no flags
662+
expect(vnodeCall.patchFlag).toBeUndefined()
663+
expect(
664+
(vnodeCall.props as ObjectExpression).properties[0].value,
665+
).toMatchObject({
666+
type: NodeTypes.JS_CACHE_EXPRESSION,
667+
index: 0,
668+
value: {
669+
type: NodeTypes.COMPOUND_EXPRESSION,
670+
children: [
671+
`async `,
672+
{ content: `e` },
673+
` => await `,
674+
{ content: `_ctx.foo` },
675+
`(`,
676+
{ content: `e` },
677+
`)`,
678+
],
679+
},
680+
})
681+
})
682+
633683
test('inline async function expression handler', () => {
634684
const { root, node } = parseWithVOn(
635685
`<div v-on:click="async function () { await foo() } " />`,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { hasScopeRef, isMemberExpression } from '../utils'
1717
import { TO_HANDLER_KEY } from '../runtimeHelpers'
1818

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

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

0 commit comments

Comments
 (0)