From 825aa771341b6d0bed3ec09ec8a7f0e02a7cac47 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 11 Feb 2022 17:59:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(mp):=20=E4=BF=AE=E5=A4=8D=20v-for=20?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E5=A4=8D=E6=9D=82=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E6=97=B6=20stop=20=E4=BF=AE=E9=A5=B0=E7=AC=A6=E6=97=A0?= =?UTF-8?q?=E6=95=88=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 | 14 ++++++++++++++ .../lib/script/traverse/data/event.js | 17 ++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index d8686cfd9..2d65093af 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -346,6 +346,20 @@ describe('mp:compiler-extra', () => { '', '' ) + assertCodegen( + '{{item}}', + '{{item}}' + ) + assertCodegen( + '{{item}}', + '{{item}}', + 'with(this){if(!_isMounted){e0=function($event){$event.stopPropagation();return func($event)}}}' + ) + assertCodegen( + '{{item}}', + '{{item}}', + 'with(this){var l0=array();if(!_isMounted){e0=function($event){$event.stopPropagation();return func($event)}}$mp.data=Object.assign({},{$root:{l0:l0}})}' + ) assertCodegen( '', '' 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 2ec5b812d..7c0186139 100644 --- a/packages/uni-template-compiler/lib/script/traverse/data/event.js +++ b/packages/uni-template-compiler/lib/script/traverse/data/event.js @@ -320,17 +320,19 @@ 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() + } + } // 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理 if (anonymous && isSafeScoped(state)) { funcPath.traverse({ noScope: true, - MemberExpression (path) { - if (path.node.object.name === '$event' && path.node.property.name === - 'stopPropagation') { - isCatch = true - path.stop() - } - }, + MemberExpression: testCatch, AssignmentExpression (path) { // "update:title": function($event) {title = $event} const left = path.node.left const right = path.node.right @@ -368,6 +370,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, if (anonymous) { // 处理复杂表达式中使用的局部变量(主要在v-for中定义) funcPath.traverse({ + MemberExpression: testCatch, Identifier (path) { const scope = path.scope const node = path.node -- GitLab