diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 2d65093afd7f3cce04bf137f6426baa0e2dd98b2..f2385697fdb92c0b262a6389eabfe68373ef6f8a 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -736,6 +736,10 @@ describe('mp:compiler-extra', () => { '{{ item }}', '{{item}}' ) + assertCodegen( + '{{ item }}', + '{{item}}' + ) assertCodegen( '{{ item2 }}+{{index}}', '{{item2+"+"+index}}', @@ -751,6 +755,11 @@ describe('mp:compiler-extra', () => { '', 'with(this){var l0=array();if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return test(item)}}$mp.data=Object.assign({},{$root:{l0:l0}})}' ) + assertCodegen( + '', + '', + 'with(this){var l0=array();if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;$event.stopPropagation();return test(item)}}$mp.data=Object.assign({},{$root:{l0:l0}})}' + ) assertCodegen( '{{item}}', '{{item}}', diff --git a/packages/uni-template-compiler/lib/script/traverse/data/event.js b/packages/uni-template-compiler/lib/script/traverse/data/event.js index 7c0186139fee1089c14ad762e12182ddbbede1c3..2e520d91067fc86a813e6b3cb77c9fc136b35d57 100644 --- a/packages/uni-template-compiler/lib/script/traverse/data/event.js +++ b/packages/uni-template-compiler/lib/script/traverse/data/event.js @@ -320,19 +320,21 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, } } - const testCatch = function (path) { - // TODO 仅使用 name 容易误判 - if (path.node.object.name === '$event' && path.node.property.name === - 'stopPropagation') { - isCatch = true - path.stop() + const testCatch = function (stop) { + return function (path) { + // TODO 仅使用 name 容易误判 + if (path.node.object.name === '$event' && path.node.property.name === + 'stopPropagation') { + isCatch = true + stop && path.stop() + } } } // 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理 if (anonymous && isSafeScoped(state)) { funcPath.traverse({ noScope: true, - MemberExpression: testCatch, + MemberExpression: testCatch(), AssignmentExpression (path) { // "update:title": function($event) {title = $event} const left = path.node.left const right = path.node.right @@ -370,7 +372,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, if (anonymous) { // 处理复杂表达式中使用的局部变量(主要在v-for中定义) funcPath.traverse({ - MemberExpression: testCatch, + MemberExpression: testCatch(), Identifier (path) { const scope = path.scope const node = path.node