提交 e9a2b355 编写于 作者: Q qiang

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

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