Skip to content

Commit

Permalink
fix(mp): multiple v-for scope with v-if
Browse files Browse the repository at this point in the history
  • Loading branch information
zhetengbiji committed Jan 16, 2023
1 parent e402ded commit 570d7b6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@ describe('mp:compiler-extra', () => {
'<view><block wx:for="{{list[$root.m0]}}" wx:for-item="item" wx:for-index="index"><view><view>{{item}}</view></view></block></view>',
'with(this){var m0=get(test);$mp.data=Object.assign({},{$root:{m0:m0}})}'
)
assertCodegen(
'<view v-for="(item,index) in items" :key="index"><view v-if="item"><input v-for="(item1,index1) in item" :key="index1" :placehold="getValue(item1)" :value="getValue(item)"></view></view>',
'<block wx:for="{{$root.l1}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig}}"><view><block wx:for="{{item.l0}}" wx:for-item="item1" wx:for-index="index1" wx:key="index1"><input placehold="{{item1.m0}}" value="{{item1.m1}}"/></block></view></block></view></block>',
'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 ', () => {
Expand Down
16 changes: 14 additions & 2 deletions packages/uni-template-compiler/lib/script/traverse/member-expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ module.exports = function traverseRenderList (path, state) {
forExtra: getForExtra(forItem, forIndex, path, state),
propertyArray: [],
declarationArray: [],
renderSlotStatementArray: []
renderSlotStatementArray: [],
path
}

const forState = {
Expand Down

0 comments on commit 570d7b6

Please sign in to comment.