diff --git a/src/compiler/codegen/events.js b/src/compiler/codegen/events.js index b87441925b4..1beaed76a02 100644 --- a/src/compiler/codegen/events.js +++ b/src/compiler/codegen/events.js @@ -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 @@ -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) { @@ -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 = '' @@ -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) { diff --git a/test/unit/modules/compiler/codegen.spec.js b/test/unit/modules/compiler/codegen.spec.js index f2c47ed4b6b..7f5fbeae333 100644 --- a/test/unit/modules/compiler/codegen.spec.js +++ b/test/unit/modules/compiler/codegen.spec.js @@ -304,32 +304,32 @@ describe('codegen', () => { it('generate events with method call', () => { assertCodegen( '', - `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( '', - `with(this){return _c('input',{on:{"input":function($event){onInput();}}})}` + `with(this){return _c('input',{on:{"input":function($event){return onInput();}}})}` ) // without semicolon assertCodegen( '', - `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( '', - `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( '', - `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( - '', - `with(this){return _c('input',{on:{"input":function($event){onInput(');[\'());');}}})}` + ``, + `with(this){return _c('input',{on:{"input":function($event){onInput(');[\\'());');}}})}` ) }) diff --git a/test/unit/modules/vdom/patch/edge-cases.spec.js b/test/unit/modules/vdom/patch/edge-cases.spec.js index 4c5702b47bf..a1c492d4b3e 100644 --- a/test/unit/modules/vdom/patch/edge-cases.spec.js +++ b/test/unit/modules/vdom/patch/edge-cases.spec.js @@ -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) } }