Skip to content

Commit e4f09c1

Browse files
committed
fix(compiler-core/v-on): handle falsy values when caching v-on handlers
fix #2605
1 parent 3cd30c5 commit e4f09c1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

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

+41-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,47 @@ describe('compiler: transform v-on', () => {
476476
index: 1,
477477
value: {
478478
type: NodeTypes.COMPOUND_EXPRESSION,
479-
children: [`(...args) => (`, { content: `_ctx.foo(...args)` }, `)`]
479+
children: [
480+
`(...args) => (`,
481+
{ content: `_ctx.foo && _ctx.foo(...args)` },
482+
`)`
483+
]
484+
}
485+
})
486+
})
487+
488+
test('compound member expression handler', () => {
489+
const { root, node } = parseWithVOn(`<div v-on:click="foo.bar" />`, {
490+
prefixIdentifiers: true,
491+
cacheHandlers: true
492+
})
493+
expect(root.cached).toBe(1)
494+
const vnodeCall = node.codegenNode as VNodeCall
495+
// should not treat cached handler as dynamicProp, so no flags
496+
expect(vnodeCall.patchFlag).toBeUndefined()
497+
expect(
498+
(vnodeCall.props as ObjectExpression).properties[0].value
499+
).toMatchObject({
500+
type: NodeTypes.JS_CACHE_EXPRESSION,
501+
index: 1,
502+
value: {
503+
type: NodeTypes.COMPOUND_EXPRESSION,
504+
children: [
505+
`(...args) => (`,
506+
{
507+
children: [
508+
{ content: `_ctx.foo` },
509+
`.`,
510+
{ content: `bar` },
511+
` && `,
512+
{ content: `_ctx.foo` },
513+
`.`,
514+
{ content: `bar` },
515+
`(...args)`
516+
]
517+
},
518+
`)`
519+
]
480520
}
481521
})
482522
})

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ export const transformOn: DirectiveTransform = (
108108
// avoiding the need to be patched.
109109
if (shouldCache && isMemberExp) {
110110
if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
111-
exp.content += `(...args)`
111+
exp.content = `${exp.content} && ${exp.content}(...args)`
112112
} else {
113-
exp.children.push(`(...args)`)
113+
exp.children = [...exp.children, ` && `, ...exp.children, `(...args)`]
114114
}
115115
}
116116
}

0 commit comments

Comments
 (0)