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

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

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