diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index d8686cfd9efad09bf81428b5aa30bdf09337877c..2d65093afd7f3cce04bf137f6426baa0e2dd98b2 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 2ec5b812d3e0ce1d8c05f556b640229788001b91..7c0186139fee1089c14ad762e12182ddbbede1c3 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