Skip to content

Commit

Permalink
fix(compiler-core/v-on): handle falsy values when caching v-on handlers
Browse files Browse the repository at this point in the history
fix #2605
  • Loading branch information
yyx990803 committed Nov 30, 2020
1 parent 3cd30c5 commit e4f09c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
42 changes: 41 additions & 1 deletion packages/compiler-core/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,47 @@ describe('compiler: transform v-on', () => {
index: 1,
value: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`(...args) => (`, { content: `_ctx.foo(...args)` }, `)`]
children: [
`(...args) => (`,
{ content: `_ctx.foo && _ctx.foo(...args)` },
`)`
]
}
})
})

test('compound member expression handler', () => {
const { root, node } = parseWithVOn(`<div v-on:click="foo.bar" />`, {
prefixIdentifiers: true,
cacheHandlers: true
})
expect(root.cached).toBe(1)
const vnodeCall = node.codegenNode as VNodeCall
// should not treat cached handler as dynamicProp, so no flags
expect(vnodeCall.patchFlag).toBeUndefined()
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.JS_CACHE_EXPRESSION,
index: 1,
value: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [
`(...args) => (`,
{
children: [
{ content: `_ctx.foo` },
`.`,
{ content: `bar` },
` && `,
{ content: `_ctx.foo` },
`.`,
{ content: `bar` },
`(...args)`
]
},
`)`
]
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-core/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export const transformOn: DirectiveTransform = (
// avoiding the need to be patched.
if (shouldCache && isMemberExp) {
if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
exp.content += `(...args)`
exp.content = `${exp.content} && ${exp.content}(...args)`
} else {
exp.children.push(`(...args)`)
exp.children = [...exp.children, ` && `, ...exp.children, `(...args)`]
}
}
}
Expand Down

0 comments on commit e4f09c1

Please sign in to comment.