Skip to content

Commit 4fb6034

Browse files
yyx990803hefeng
authored and
hefeng
committed
fix: return inline invocation return value in v-on handlers
close vuejs#7628
1 parent eaad54b commit 4fb6034

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/compiler/codegen/events.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @flow */
22

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

67
// KeyboardEvent.keyCode aliases
@@ -94,6 +95,7 @@ function genHandler (
9495

9596
const isMethodPath = simplePathRE.test(handler.value)
9697
const isFunctionExpression = fnExpRE.test(handler.value)
98+
const isFunctionInvocation = fnInvokeRE.test(handler.value)
9799

98100
if (!handler.modifiers) {
99101
if (isMethodPath || isFunctionExpression) {
@@ -103,7 +105,9 @@ function genHandler (
103105
if (__WEEX__ && handler.params) {
104106
return genWeexHandler(handler.params, handler.value)
105107
}
106-
return `function($event){${handler.value}}` // inline statement
108+
return `function($event){${
109+
isFunctionInvocation ? `return (${handler.value})` : handler.value
110+
}}` // inline statement
107111
} else {
108112
let code = ''
109113
let genModifierCode = ''
@@ -138,7 +142,9 @@ function genHandler (
138142
? `return ${handler.value}($event)`
139143
: isFunctionExpression
140144
? `return (${handler.value})($event)`
141-
: handler.value
145+
: isFunctionInvocation
146+
? `return (${handler.value})`
147+
: handler.value
142148
/* istanbul ignore if */
143149
if (__WEEX__ && handler.params) {
144150
return genWeexHandler(handler.params, code + handlerCode)

test/unit/features/directives/on.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -923,4 +923,28 @@ describe('Directive v-on', () => {
923923
expect(spy.calls.count()).toBe(0)
924924
}).then(done)
925925
})
926+
927+
// #7628
928+
it('handler should return the return value of inline function invocation', () => {
929+
let value
930+
new Vue({
931+
template: `<test @foo="bar()"></test>`,
932+
methods: {
933+
bar() {
934+
return 1
935+
}
936+
},
937+
components: {
938+
test: {
939+
created() {
940+
value = this.$listeners.foo()
941+
},
942+
render(h) {
943+
return h('div')
944+
}
945+
}
946+
}
947+
}).$mount()
948+
expect(value).toBe(1)
949+
})
926950
})

0 commit comments

Comments
 (0)