提交 73d63f12 编写于 作者: Q qiang

fix: 优化 test 表达式查询 question/105048

上级 02a189de
......@@ -697,6 +697,11 @@ describe('mp:compiler-extra', () => {
'<block wx:if="{{$root.m0}}"><view>{{$root.m1}}</view></block>',
'with(this){var m0=getValue(key);var m1=m0?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}'
)
assertCodegen(
'<view v-if="test1(key)&&test2(key)">{{getValue(key)}}</view>',
'<block wx:if="{{$root.m0&&$root.m1}}"><view>{{$root.m2}}</view></block>',
'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}})}'
)
assertCodegen(
'<view v-for="(item,index) in list" :key="index"><view v-if="item">{{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}}"><view>{{item.m0}}</view></block></view></block>',
......
......@@ -71,18 +71,14 @@ function findTest (path, state) {
const property = node.property
const propertyName = property.name
if (objectName === VAR_ROOT || (names.includes(objectName) && (propertyName === IDENTIFIER_METHOD || propertyName === IDENTIFIER_FILTER || propertyName === IDENTIFIER_GLOBAL))) {
let property
traverse(testOrig, {
noScope: true,
Identifier (identifierPath) {
const node = identifierPath.node
if (node.name === propertyName) {
property = node
identifierPath.stop()
}
}
})
memberExpressionPath.replaceWith(property)
const array = []
let tempPath = memberExpressionPath
while (tempPath.parentPath) {
const key = tempPath.key
array.unshift(typeof key === 'number' ? `[${key}]` : `.${key}`)
tempPath = tempPath.parentPath
}
memberExpressionPath.replaceWith(path.parentPath.get('test' + array.join('')).node.property)
}
}
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册