Skip to content

Commit

Permalink
fix: cover more cases in v-on inline return value
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 20, 2018
1 parent 0ebb0f3 commit 9432737
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/compiler/codegen/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

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

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

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

if (!handler.modifiers) {
if (isMethodPath || isFunctionExpression) {
Expand All @@ -106,7 +106,7 @@ function genHandler (
return genWeexHandler(handler.params, handler.value)
}
return `function($event){${
isFunctionInvocation ? `return (${handler.value})` : handler.value
isFunctionInvocation ? `return ${handler.value}` : handler.value
}}` // inline statement
} else {
let code = ''
Expand Down Expand Up @@ -143,7 +143,7 @@ function genHandler (
: isFunctionExpression
? `return (${handler.value})($event)`
: isFunctionInvocation
? `return (${handler.value})`
? `return ${handler.value}`
: handler.value
/* istanbul ignore if */
if (__WEEX__ && handler.params) {
Expand Down
14 changes: 7 additions & 7 deletions test/unit/modules/compiler/codegen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,32 +304,32 @@ describe('codegen', () => {
it('generate events with method call', () => {
assertCodegen(
'<input @input="onInput($event);">',
`with(this){return _c('input',{on:{"input":function($event){onInput($event);}}})}`
`with(this){return _c('input',{on:{"input":function($event){return onInput($event);}}})}`
)
// empty arguments
assertCodegen(
'<input @input="onInput();">',
`with(this){return _c('input',{on:{"input":function($event){onInput();}}})}`
`with(this){return _c('input',{on:{"input":function($event){return onInput();}}})}`
)
// without semicolon
assertCodegen(
'<input @input="onInput($event)">',
`with(this){return _c('input',{on:{"input":function($event){onInput($event)}}})}`
`with(this){return _c('input',{on:{"input":function($event){return onInput($event)}}})}`
)
// multiple args
assertCodegen(
'<input @input="onInput($event, \'abc\', 5);">',
`with(this){return _c('input',{on:{"input":function($event){onInput($event, 'abc', 5);}}})}`
`with(this){return _c('input',{on:{"input":function($event){return onInput($event, 'abc', 5);}}})}`
)
// expression in args
assertCodegen(
'<input @input="onInput($event, 2+2);">',
`with(this){return _c('input',{on:{"input":function($event){onInput($event, 2+2);}}})}`
`with(this){return _c('input',{on:{"input":function($event){return onInput($event, 2+2);}}})}`
)
// tricky symbols in args
assertCodegen(
'<input @input="onInput(\');[\'());\');">',
`with(this){return _c('input',{on:{"input":function($event){onInput(');[\'());');}}})}`
`<input @input="onInput(');[\\'());');">`,
`with(this){return _c('input',{on:{"input":function($event){onInput(');[\\'());');}}})}`
)
})

Expand Down
7 changes: 2 additions & 5 deletions test/unit/modules/vdom/patch/edge-cases.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@ describe('vdom patch: edge cases', () => {
bind (el, binding, vnode) {
waitForUpdate(() => {
expect(vnode.children[0].data.on.click()).toBe(5)
}).then(() => {
expect(vnode.children[2].data.on.click(dummyEvt)).toBe(5)
}).then(() => {
expect(vnode.children[4].data.on.click()).not.toBeDefined()
}).then(() => {
expect(vnode.children[6].data.on.click(dummyEvt)).not.toBeDefined()
expect(vnode.children[4].data.on.click()).toBe(10)
expect(vnode.children[6].data.on.click(dummyEvt)).toBe(10)
}).then(done)
}
}
Expand Down

0 comments on commit 9432737

Please sign in to comment.