From 570d7b6de2302b268f1204b5151b9929d4ca8153 Mon Sep 17 00:00:00 2001 From: qiang Date: Mon, 16 Jan 2023 16:02:18 +0800 Subject: [PATCH] fix(mp): multiple v-for scope with v-if --- .../__tests__/compiler-extra.spec.js | 5 +++++ .../lib/script/traverse/member-expr.js | 16 ++++++++++++++-- .../lib/script/traverse/render-list.js | 3 ++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 50eee20c720..3bfa827e39e 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -700,6 +700,11 @@ describe('mp:compiler-extra', () => { '{{item}}', 'with(this){var m0=get(test);$mp.data=Object.assign({},{$root:{m0:m0}})}' ) + assertCodegen( + '', + '', + 'with(this){var l1=__map(items,function(item,index){var $orig=__get_orig(item);var l0=item?__map(item,function(item1,index1){var $orig=__get_orig(item1);var m0=getValue(item1);var m1=getValue(item);return{$orig:$orig,m0:m0,m1:m1}}):null;return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + ) }) it('generate TemplateLiteral ', () => { 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 fe9100ffe3f..fbf4136f58c 100644 --- a/packages/uni-template-compiler/lib/script/traverse/member-expr.js +++ b/packages/uni-template-compiler/lib/script/traverse/member-expr.js @@ -5,7 +5,8 @@ const { VAR_ROOT, IDENTIFIER_METHOD, IDENTIFIER_FILTER, - IDENTIFIER_GLOBAL + IDENTIFIER_GLOBAL, + METHOD_RENDER_LIST } = require('../../constants') function isMatch (name, forItem, forIndex) { @@ -19,8 +20,19 @@ function findScoped (path, test, state) { const scoped = state.scoped.find(scoped => { const { forItem, - forIndex + forIndex, + path: listPath } = scoped + const funPath = path.findParent(path => path.isFunctionExpression() && path.parentPath.node.callee.name === METHOD_RENDER_LIST) + if (funPath && funPath.parentPath === listPath) { + // TODO 为兼容历史结构仅在当前 list 父级存在 v-if 返回 + const parent = listPath.findParent(path => path.isFunctionExpression() || path.isConditionalExpression()) + if (parent && parent.isConditionalExpression()) { + return true + } + } else { + return false + } let match = false path.traverse({ noScope: true, diff --git a/packages/uni-template-compiler/lib/script/traverse/render-list.js b/packages/uni-template-compiler/lib/script/traverse/render-list.js index ae7c662cb13..7cb9f90c590 100644 --- a/packages/uni-template-compiler/lib/script/traverse/render-list.js +++ b/packages/uni-template-compiler/lib/script/traverse/render-list.js @@ -136,7 +136,8 @@ module.exports = function traverseRenderList (path, state) { forExtra: getForExtra(forItem, forIndex, path, state), propertyArray: [], declarationArray: [], - renderSlotStatementArray: [] + renderSlotStatementArray: [], + path } const forState = {