提交 a445c140 编写于 作者: fxy060608's avatar fxy060608

fix(v3): allow root elements with v-if, v-else-if and v-else

上级 30118f5b
...@@ -136,6 +136,12 @@ describe('codegen', () => { ...@@ -136,6 +136,12 @@ describe('codegen', () => {
'<view data-a="1" :data-b="b"></view>', '<view data-a="1" :data-b="b"></view>',
`with(this){return _c('view',{attrs:{"data-b":_$s(0,'a-data-b',b),"_i":0}})}` `with(this){return _c('view',{attrs:{"data-b":_$s(0,'a-data-b',b),"_i":0}})}`
) )
})
it('generate v-if directive', () => {
assertCodegen(
'<text v-if="a">1</text><text v-else-if="b">2</text><text v-else-if="c">3</text><text v-else>d</text>',
`with(this){return (_$s(0,'i',a))?_c('text'):(_$s(1,'e',b))?_c('text'):(_$s(2,'e',c))?_c('text'):_c('text')}`
)
}) })
}) })
/* eslint-enable quotes */ /* eslint-enable quotes */
...@@ -75,6 +75,12 @@ describe('codegen', () => { ...@@ -75,6 +75,12 @@ describe('codegen', () => {
'<view data-a="1" :data-b="b"></view>', '<view data-a="1" :data-b="b"></view>',
`with(this){return _c('v-uni-view',{attrs:{"data-a":"1","data-b":_$g(0,'a-data-b'),"_i":0}})}` `with(this){return _c('v-uni-view',{attrs:{"data-a":"1","data-b":_$g(0,'a-data-b'),"_i":0}})}`
) )
})
it('generate v-if directive', () => {
assertCodegen(
'<text v-if="a">1</text><text v-else-if="b">2</text><text v-else-if="c">3</text><text v-else>d</text>',
`with(this){return (_$g(0,'i'))?_c('v-uni-text',{attrs:{"_i":0}},[_v("1")]):(_$g(1,'e'))?_c('v-uni-text',{attrs:{"_i":1}},[_v("2")]):(_$g(2,'e'))?_c('v-uni-text',{attrs:{"_i":2}},[_v("3")]):_c('v-uni-text',{attrs:{"_i":3}},[_v("d")])}`
)
}) })
}) })
/* eslint-enable quotes */ /* eslint-enable quotes */
...@@ -18,13 +18,8 @@ const scopedPath = path.resolve(__dirname, '../../') ...@@ -18,13 +18,8 @@ const scopedPath = path.resolve(__dirname, '../../')
const compiler = require('../lib') const compiler = require('../lib')
const res = compiler.compile( const res = compiler.compile(
` `
<view class="h-page"> <text v-if="a">1</text><text v-else-if="b">2</text><text v-else-if="c">3</text><text v-else>d</text>
<slot></slot>
<h-dialog></h-dialog>
<h-navbar></h-navbar>
<h-toast></h-toast>
</view>
`, { `, {
miniprogram: true, miniprogram: true,
resourcePath: '/User/fxy/Documents/test.wxml', resourcePath: '/User/fxy/Documents/test.wxml',
...@@ -37,9 +32,9 @@ const res = compiler.compile( ...@@ -37,9 +32,9 @@ const res = compiler.compile(
mp: { mp: {
platform: 'mp-weixin' platform: 'mp-weixin'
}, },
filterModules: ['swipe'] filterModules: ['swipe'],
// service: true, // service: true,
// view: true view: true
}) })
console.log(require('util').inspect(res, { console.log(require('util').inspect(res, {
......
...@@ -18,6 +18,10 @@ function parseIs (el, genVar) { ...@@ -18,6 +18,10 @@ function parseIs (el, genVar) {
} }
} }
function isProcessed (exp) {
return String(exp).indexOf('_$') === 0
}
// 当根节点是由if,elseif,else组成,会调用多次parseIf来解析root
function parseIf (el, createGenVar, isScopedSlot) { function parseIf (el, createGenVar, isScopedSlot) {
if (!el.if) { if (!el.if) {
return return
...@@ -26,11 +30,13 @@ function parseIf (el, createGenVar, isScopedSlot) { ...@@ -26,11 +30,13 @@ function parseIf (el, createGenVar, isScopedSlot) {
isScopedSlot = false isScopedSlot = false
} }
el.ifConditions.forEach(con => { el.ifConditions.forEach(con => {
if (isVar(con.exp)) { if (!isProcessed(con.exp) && isVar(con.exp)) {
con.exp = createGenVar(con.block.attrsMap[ID], isScopedSlot)(con.block.elseif ? V_ELSE_IF : V_IF, con.exp) con.exp = createGenVar(con.block.attrsMap[ID], isScopedSlot)(con.block.elseif ? V_ELSE_IF : V_IF, con.exp)
} }
}) })
el.if = createGenVar(el.attrsMap[ID], isScopedSlot)(V_IF, el.if) if (!isProcessed(el.if)) {
el.if = createGenVar(el.attrsMap[ID], isScopedSlot)(V_IF, el.if)
}
} }
function parseFor (el, createGenVar, isScopedSlot, fill = false) { function parseFor (el, createGenVar, isScopedSlot, fill = false) {
......
...@@ -156,6 +156,11 @@ function transformNode (el, parent, state, isScopedSlot) { ...@@ -156,6 +156,11 @@ function transformNode (el, parent, state, isScopedSlot) {
function postTransformNode (el, options) { function postTransformNode (el, options) {
if (!el.parent) { // 从根节点开始递归处理 if (!el.parent) { // 从根节点开始递归处理
if (options.root) { // 当根节点是由if,elseif,else组成
parseIf(options.root, createGenVar)
} else {
options.root = el
}
traverseNode(el, false, { traverseNode(el, false, {
forIteratorId: 0, forIteratorId: 0,
transformNode, transformNode,
......
...@@ -155,6 +155,11 @@ function transformNode (el, parent, state, isScopedSlot) { ...@@ -155,6 +155,11 @@ function transformNode (el, parent, state, isScopedSlot) {
function postTransformNode (el, options) { function postTransformNode (el, options) {
if (!el.parent) { // 从根节点开始递归处理 if (!el.parent) { // 从根节点开始递归处理
if (options.root) { // 当根节点是由if,elseif,else组成
parseIf(options.root, createGenVar)
} else {
options.root = el
}
traverseNode(el, false, { traverseNode(el, false, {
forIteratorId: 0, forIteratorId: 0,
transformNode, transformNode,
...@@ -235,4 +240,4 @@ module.exports = { ...@@ -235,4 +240,4 @@ module.exports = {
}, },
postTransformNode, postTransformNode,
genData genData
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册