From a7dee553e517bcde6ac797efdf3681ebf3777dd2 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 13 Aug 2020 19:24:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20v-for=20=E5=B5=8C?= =?UTF-8?q?=E5=A5=97=20v-if=20=E6=97=B6=EF=BC=8C=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=88=B0=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=AB=AF=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=20fixed=20#2011?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/compiler-extra.spec.js | 5 ++++ .../lib/script/traverse/member-expr.js | 28 +++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 3cee2701ba..c06045891f 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -702,5 +702,10 @@ describe('mp:compiler-extra', () => { '{{item.m0}}', 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) + assertCodegen( + '{{getValue(item)}}', + '{{item.m0}}', + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length>0?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + ) }) }) diff --git a/packages/uni-template-compiler/lib/script/traverse/member-expr.js b/packages/uni-template-compiler/lib/script/traverse/member-expr.js index c8a42dd7fd..f24b487ceb 100644 --- a/packages/uni-template-compiler/lib/script/traverse/member-expr.js +++ b/packages/uni-template-compiler/lib/script/traverse/member-expr.js @@ -2,7 +2,9 @@ const t = require('@babel/types') const traverse = require('@babel/traverse').default const { - VAR_ROOT + VAR_ROOT, + IDENTIFIER_METHOD, + IDENTIFIER_FILTER } = require('../../constants') function isMatch (name, forItem, forIndex) { @@ -42,15 +44,29 @@ function findTest (path, state) { let tests while (path.parentPath && path.key !== 'body') { if (path.key === 'consequent' || path.key === 'alternate') { - let test = t.arrayExpression([t.clone(path.container.test)]) + const testOrig = path.container.test + let test = t.arrayExpression([t.cloneDeep(testOrig)]) traverse(test, { noScope: true, - MemberExpression (path_) { + MemberExpression (memberExpressionPath) { const names = state.scoped.map(scoped => scoped.forItem) - const node = path_.node + const node = memberExpressionPath.node const objectName = node.object.name - if (objectName === VAR_ROOT || (names.includes(objectName) && path.scope.hasOwnBinding(node.property.name))) { - path_.replaceWith(node.property) + const property = node.property + const propertyName = property.name + if (objectName === VAR_ROOT || (names.includes(objectName) && (propertyName === IDENTIFIER_METHOD || propertyName === IDENTIFIER_FILTER))) { + let property + traverse(testOrig, { + noScope: true, + Identifier (identifierPath) { + const node = identifierPath.node + if (node.name === propertyName) { + property = node + identifierPath.stop() + } + } + }) + memberExpressionPath.replaceWith(property) } } }) -- GitLab