Skip to content

Commit 0b78ea9

Browse files
committed
properly handle inline function expressions in v-on
1 parent 303378f commit 0b78ea9

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/compiler/codegen/events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22

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

56
// keyCode aliases
@@ -41,7 +42,7 @@ function genHandler (
4142
} else if (Array.isArray(handler)) {
4243
return `[${handler.map(genHandler).join(',')}]`
4344
} else if (!handler.modifiers) {
44-
return simplePathRE.test(handler.value)
45+
return fnExpRE.test(handler.value) || simplePathRE.test(handler.value)
4546
? handler.value
4647
: `function($event){${handler.value}}`
4748
} else {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Directive v-on', () => {
2828
expect(event.type).toBe('click')
2929
})
3030

31-
it('should bind event to a inline method', () => {
31+
it('should bind event to a inline statement', () => {
3232
vm = new Vue({
3333
el,
3434
template: '<div v-on:click="foo(1,2,3,$event)"></div>',
@@ -46,6 +46,19 @@ describe('Directive v-on', () => {
4646
expect(firstArgs[3].type).toBe('click')
4747
})
4848

49+
it('should support inline function expression', () => {
50+
const spy = jasmine.createSpy()
51+
vm = new Vue({
52+
el,
53+
template: `<div class="test" @click="function (e) { log(e.target.className) }"></div>`,
54+
methods: {
55+
log: spy
56+
}
57+
}).$mount()
58+
triggerEvent(vm.$el, 'click')
59+
expect(spy).toHaveBeenCalledWith('test')
60+
})
61+
4962
it('should support shorthand', () => {
5063
vm = new Vue({
5164
el,

test/unit/modules/compiler/codegen.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,39 @@ describe('codegen', () => {
270270
)
271271
})
272272

273+
it('generate events with inline function expression', () => {
274+
// normal function
275+
assertCodegen(
276+
'<input @input="function () { current++ }">',
277+
`with(this){return _h('input',{on:{"input":function () { current++ }}})}`
278+
)
279+
// arrow with no args
280+
assertCodegen(
281+
'<input @input="()=>current++">',
282+
`with(this){return _h('input',{on:{"input":()=>current++}})}`
283+
)
284+
// arrow with parens, single arg
285+
assertCodegen(
286+
'<input @input="(e) => current++">',
287+
`with(this){return _h('input',{on:{"input":(e) => current++}})}`
288+
)
289+
// arrow with parens, multi args
290+
assertCodegen(
291+
'<input @input="(a, b, c) => current++">',
292+
`with(this){return _h('input',{on:{"input":(a, b, c) => current++}})}`
293+
)
294+
// arrow with destructuring
295+
assertCodegen(
296+
'<input @input="({ a, b }) => current++">',
297+
`with(this){return _h('input',{on:{"input":({ a, b }) => current++}})}`
298+
)
299+
// arrow single arg no parens
300+
assertCodegen(
301+
'<input @input="e=>current++">',
302+
`with(this){return _h('input',{on:{"input":e=>current++}})}`
303+
)
304+
})
305+
273306
// #3893
274307
it('should not treat handler with unexpected whitespace as inline statement', () => {
275308
assertCodegen(

0 commit comments

Comments
 (0)