Skip to content

Commit 9432737

Browse files
committed
fix: cover more cases in v-on inline return value
1 parent 0ebb0f3 commit 9432737

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

src/compiler/codegen/events.js

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

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

77
// KeyboardEvent.keyCode aliases
@@ -95,7 +95,7 @@ function genHandler (
9595

9696
const isMethodPath = simplePathRE.test(handler.value)
9797
const isFunctionExpression = fnExpRE.test(handler.value)
98-
const isFunctionInvocation = fnInvokeRE.test(handler.value)
98+
const isFunctionInvocation = simplePathRE.test(handler.value.replace(fnInvokeRE, ''))
9999

100100
if (!handler.modifiers) {
101101
if (isMethodPath || isFunctionExpression) {
@@ -106,7 +106,7 @@ function genHandler (
106106
return genWeexHandler(handler.params, handler.value)
107107
}
108108
return `function($event){${
109-
isFunctionInvocation ? `return (${handler.value})` : handler.value
109+
isFunctionInvocation ? `return ${handler.value}` : handler.value
110110
}}` // inline statement
111111
} else {
112112
let code = ''
@@ -143,7 +143,7 @@ function genHandler (
143143
: isFunctionExpression
144144
? `return (${handler.value})($event)`
145145
: isFunctionInvocation
146-
? `return (${handler.value})`
146+
? `return ${handler.value}`
147147
: handler.value
148148
/* istanbul ignore if */
149149
if (__WEEX__ && handler.params) {

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -304,32 +304,32 @@ describe('codegen', () => {
304304
it('generate events with method call', () => {
305305
assertCodegen(
306306
'<input @input="onInput($event);">',
307-
`with(this){return _c('input',{on:{"input":function($event){onInput($event);}}})}`
307+
`with(this){return _c('input',{on:{"input":function($event){return onInput($event);}}})}`
308308
)
309309
// empty arguments
310310
assertCodegen(
311311
'<input @input="onInput();">',
312-
`with(this){return _c('input',{on:{"input":function($event){onInput();}}})}`
312+
`with(this){return _c('input',{on:{"input":function($event){return onInput();}}})}`
313313
)
314314
// without semicolon
315315
assertCodegen(
316316
'<input @input="onInput($event)">',
317-
`with(this){return _c('input',{on:{"input":function($event){onInput($event)}}})}`
317+
`with(this){return _c('input',{on:{"input":function($event){return onInput($event)}}})}`
318318
)
319319
// multiple args
320320
assertCodegen(
321321
'<input @input="onInput($event, \'abc\', 5);">',
322-
`with(this){return _c('input',{on:{"input":function($event){onInput($event, 'abc', 5);}}})}`
322+
`with(this){return _c('input',{on:{"input":function($event){return onInput($event, 'abc', 5);}}})}`
323323
)
324324
// expression in args
325325
assertCodegen(
326326
'<input @input="onInput($event, 2+2);">',
327-
`with(this){return _c('input',{on:{"input":function($event){onInput($event, 2+2);}}})}`
327+
`with(this){return _c('input',{on:{"input":function($event){return onInput($event, 2+2);}}})}`
328328
)
329329
// tricky symbols in args
330330
assertCodegen(
331-
'<input @input="onInput(\');[\'());\');">',
332-
`with(this){return _c('input',{on:{"input":function($event){onInput(');[\'());');}}})}`
331+
`<input @input="onInput(');[\\'());');">`,
332+
`with(this){return _c('input',{on:{"input":function($event){onInput(');[\\'());');}}})}`
333333
)
334334
})
335335

test/unit/modules/vdom/patch/edge-cases.spec.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ describe('vdom patch: edge cases', () => {
4949
bind (el, binding, vnode) {
5050
waitForUpdate(() => {
5151
expect(vnode.children[0].data.on.click()).toBe(5)
52-
}).then(() => {
5352
expect(vnode.children[2].data.on.click(dummyEvt)).toBe(5)
54-
}).then(() => {
55-
expect(vnode.children[4].data.on.click()).not.toBeDefined()
56-
}).then(() => {
57-
expect(vnode.children[6].data.on.click(dummyEvt)).not.toBeDefined()
53+
expect(vnode.children[4].data.on.click()).toBe(10)
54+
expect(vnode.children[6].data.on.click(dummyEvt)).toBe(10)
5855
}).then(done)
5956
}
6057
}

0 commit comments

Comments
 (0)