Просмотр исходного кода

fix: cover more cases in v-on inline return value

Evan You 7 лет назад
Родитель
Сommit
9432737cf8

+ 4 - 4
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) {

+ 7 - 7
test/unit/modules/compiler/codegen.spec.js

@@ -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(');[\\'());');}}})}`
     )
   })
 

+ 2 - 5
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)
           }
         }