From c5b25d9fb15645e479f892498dd4780b56a2db1a Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 18 Feb 2022 12:22:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(mp):=20=E4=BF=AE=E5=A4=8D=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E5=8C=85=E5=90=ABstop=E4=BF=AE=E9=A5=B0=E7=AC=A6?= =?UTF-8?q?=E6=97=B6=E5=A4=8D=E6=9D=82=E8=A1=A8=E8=BE=BE=E5=BC=8F=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/compiler-extra.spec.js | 9 +++++++++ .../lib/script/traverse/data/event.js | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 2d65093af..f2385697f 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 7c0186139..2e520d910 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 -- GitLab