提交 a7dee553 编写于 作者: Q qiang

fix: 修复 v-for 嵌套 v-if 时,编译到小程序端运行报错的问题 fixed #2011

上级 ade9d7e0
......@@ -702,5 +702,10 @@ describe('mp:compiler-extra', () => {
'<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig.length}}"><view>{{item.m0}}</view></block></view></block>',
'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(
'<view v-for="(item,index) in list" :key="index"><view v-if="item.length>0">{{getValue(item)}}</view></view>',
'<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig.length>0}}"><view>{{item.m0}}</view></block></view></block>',
'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}})}'
)
})
})
......@@ -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)
}
}
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册