提交 e9a2b355 编写于 作者: Q qiang

fix(mp): 优化含有逻辑运算的复杂表达式编译 question/118651

上级 90b254e5
...@@ -739,8 +739,8 @@ describe('mp:compiler-extra', () => { ...@@ -739,8 +739,8 @@ describe('mp:compiler-extra', () => {
) )
assertCodegen( assertCodegen(
'<view v-if="test1(key)&&test2(key)">{{getValue(key)}}</view>', '<view v-if="test1(key)&&test2(key)">{{getValue(key)}}</view>',
'<block wx:if="{{$root.m0&&$root.m1}}"><view>{{$root.m2}}</view></block>', '<block wx:if="{{$root.m0}}"><view>{{$root.m1}}</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}})}' 'with(this){var m0=test1(key)&&test2(key);var m1=m0?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}'
) )
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>',
......
...@@ -15,7 +15,7 @@ const { ...@@ -15,7 +15,7 @@ const {
getTagName getTagName
} = require('../../h5') } = require('../../h5')
const { const {
hasOwn, hasOwn,
hyphenate, hyphenate,
traverseFilter, traverseFilter,
...@@ -149,41 +149,41 @@ module.exports = { ...@@ -149,41 +149,41 @@ module.exports = {
const methodName = callee.name const methodName = callee.name
switch (methodName) { switch (methodName) {
case METHOD_CREATE_ELEMENT: case METHOD_CREATE_ELEMENT:
{ {
const tagNode = path.node.arguments[0] const tagNode = path.node.arguments[0]
if (t.isStringLiteral(tagNode)) { if (t.isStringLiteral(tagNode)) {
// 需要把标签增加到 class 样式中 // 需要把标签增加到 class 样式中
const tagName = getTagName(tagNode.value) const tagName = getTagName(tagNode.value)
if (tagName !== tagNode.value) { if (tagName !== tagNode.value) {
addStaticClass(path, '_' + tagNode.value) addStaticClass(path, '_' + tagNode.value)
} }
tagNode.value = getComponentName(hyphenate(tagName)) tagNode.value = getComponentName(hyphenate(tagName))
// 组件增加 vueId // 组件增加 vueId
if (this.options.platform.isComponent(tagNode.value)) { if (this.options.platform.isComponent(tagNode.value)) {
addVueId(path, this) addVueId(path, this)
} }
// 查找全局组件 // 查找全局组件
checkUsingGlobalComponents( checkUsingGlobalComponents(
tagNode.value, tagNode.value,
this.options.globalUsingComponents, this.options.globalUsingComponents,
this this
) )
} }
if (this.options.scopeId) { if (this.options.scopeId) {
addStaticClass(path, this.options.scopeId) addStaticClass(path, this.options.scopeId)
} }
const dataPath = path.get('arguments.1') const dataPath = path.get('arguments.1')
dataPath && dataPath.isObjectExpression() && traverseData(dataPath, this, tagNode.value) dataPath && dataPath.isObjectExpression() && traverseData(dataPath, this, tagNode.value)
} }
break break
case METHOD_TO_STRING: case METHOD_TO_STRING:
{ {
const stringNodes = path.node.arguments[0] const stringNodes = path.node.arguments[0]
stringNodes.$toString = true stringNodes.$toString = true
path.replaceWith(stringNodes) path.replaceWith(stringNodes)
} }
break break
case METHOD_RENDER_LIST: case METHOD_RENDER_LIST:
...@@ -202,7 +202,11 @@ module.exports = { ...@@ -202,7 +202,11 @@ module.exports = {
// event // event
return path.skip() return path.skip()
} }
path = path.findParent((path) => path.isLogicalExpression()) || path
path.skip()
if (path.findParent((path) => path.shouldSkip)) {
return
}
path.replaceWith( path.replaceWith(
getMemberExpr( getMemberExpr(
path, path,
...@@ -265,4 +269,4 @@ module.exports = { ...@@ -265,4 +269,4 @@ module.exports = {
path.replaceWith(root) path.replaceWith(root)
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册