Skip to content

Commit

Permalink
fix: return inline invocation return value in v-on handlers
Browse files Browse the repository at this point in the history
close #7628
  • Loading branch information
yyx990803 committed Dec 20, 2018
1 parent b00868c commit 0ebb0f3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/compiler/codegen/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/
const fnInvokeRE = /\([^)]*?\)$/
const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/

// KeyboardEvent.keyCode aliases
Expand Down Expand Up @@ -94,6 +95,7 @@ function genHandler (

const isMethodPath = simplePathRE.test(handler.value)
const isFunctionExpression = fnExpRE.test(handler.value)
const isFunctionInvocation = fnInvokeRE.test(handler.value)

if (!handler.modifiers) {
if (isMethodPath || isFunctionExpression) {
Expand All @@ -103,7 +105,9 @@ function genHandler (
if (__WEEX__ && handler.params) {
return genWeexHandler(handler.params, handler.value)
}
return `function($event){${handler.value}}` // inline statement
return `function($event){${
isFunctionInvocation ? `return (${handler.value})` : handler.value
}}` // inline statement
} else {
let code = ''
let genModifierCode = ''
Expand Down Expand Up @@ -138,7 +142,9 @@ function genHandler (
? `return ${handler.value}($event)`
: isFunctionExpression
? `return (${handler.value})($event)`
: handler.value
: isFunctionInvocation
? `return (${handler.value})`
: handler.value
/* istanbul ignore if */
if (__WEEX__ && handler.params) {
return genWeexHandler(handler.params, code + handlerCode)
Expand Down
24 changes: 24 additions & 0 deletions test/unit/features/directives/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,28 @@ describe('Directive v-on', () => {
expect(spy.calls.count()).toBe(0)
}).then(done)
})

// #7628
it('handler should return the return value of inline function invocation', () => {
let value
new Vue({
template: `<test @foo="bar()"></test>`,
methods: {
bar() {
return 1
}
},
components: {
test: {
created() {
value = this.$listeners.foo()
},
render(h) {
return h('div')
}
}
}
}).$mount()
expect(value).toBe(1)
})
})

0 comments on commit 0ebb0f3

Please sign in to comment.