From e9a2b3556e630769306846262037ee74f7beff59 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 19 Mar 2021 21:21:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(mp):=20=E4=BC=98=E5=8C=96=E5=90=AB=E6=9C=89?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E8=BF=90=E7=AE=97=E7=9A=84=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E7=BC=96=E8=AF=91=20question/118651?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/compiler-extra.spec.js | 4 +- .../lib/script/traverse/visitor.js | 74 ++++++++++--------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 0e048c608..a75db7311 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -739,8 +739,8 @@ describe('mp:compiler-extra', () => { ) assertCodegen( '{{getValue(key)}}', - '{{$root.m2}}', - 'with(this){var m0=test1(key);var m1=test2(key);var m2=m0&&m1?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1,m2:m2}})}' + '{{$root.m1}}', + 'with(this){var m0=test1(key)&&test2(key);var m1=m0?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}' ) assertCodegen( '{{getValue(item)}}', diff --git a/packages/uni-template-compiler/lib/script/traverse/visitor.js b/packages/uni-template-compiler/lib/script/traverse/visitor.js index 6f41068cc..15e7c14e6 100644 --- a/packages/uni-template-compiler/lib/script/traverse/visitor.js +++ b/packages/uni-template-compiler/lib/script/traverse/visitor.js @@ -15,7 +15,7 @@ const { getTagName } = require('../../h5') -const { +const { hasOwn, hyphenate, traverseFilter, @@ -149,41 +149,41 @@ module.exports = { const methodName = callee.name switch (methodName) { case METHOD_CREATE_ELEMENT: - { - const tagNode = path.node.arguments[0] - if (t.isStringLiteral(tagNode)) { - // 需要把标签增加到 class 样式中 - const tagName = getTagName(tagNode.value) - if (tagName !== tagNode.value) { - addStaticClass(path, '_' + tagNode.value) - } - tagNode.value = getComponentName(hyphenate(tagName)) - - // 组件增加 vueId - if (this.options.platform.isComponent(tagNode.value)) { - addVueId(path, this) - } - - // 查找全局组件 - checkUsingGlobalComponents( - tagNode.value, - this.options.globalUsingComponents, - this - ) - } - if (this.options.scopeId) { - addStaticClass(path, this.options.scopeId) - } - - const dataPath = path.get('arguments.1') - dataPath && dataPath.isObjectExpression() && traverseData(dataPath, this, tagNode.value) + { + const tagNode = path.node.arguments[0] + if (t.isStringLiteral(tagNode)) { + // 需要把标签增加到 class 样式中 + const tagName = getTagName(tagNode.value) + if (tagName !== tagNode.value) { + addStaticClass(path, '_' + tagNode.value) + } + tagNode.value = getComponentName(hyphenate(tagName)) + + // 组件增加 vueId + if (this.options.platform.isComponent(tagNode.value)) { + addVueId(path, this) + } + + // 查找全局组件 + checkUsingGlobalComponents( + tagNode.value, + this.options.globalUsingComponents, + this + ) + } + if (this.options.scopeId) { + addStaticClass(path, this.options.scopeId) + } + + const dataPath = path.get('arguments.1') + dataPath && dataPath.isObjectExpression() && traverseData(dataPath, this, tagNode.value) } break case METHOD_TO_STRING: - { - const stringNodes = path.node.arguments[0] - stringNodes.$toString = true - path.replaceWith(stringNodes) + { + const stringNodes = path.node.arguments[0] + stringNodes.$toString = true + path.replaceWith(stringNodes) } break case METHOD_RENDER_LIST: @@ -202,7 +202,11 @@ module.exports = { // event return path.skip() } - + path = path.findParent((path) => path.isLogicalExpression()) || path + path.skip() + if (path.findParent((path) => path.shouldSkip)) { + return + } path.replaceWith( getMemberExpr( path, @@ -265,4 +269,4 @@ module.exports = { path.replaceWith(root) } -} +} -- GitLab