From a445c1405e17fdc412e86627cb57a6fcc1a5c76a Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sat, 8 Feb 2020 22:43:47 +0800 Subject: [PATCH] fix(v3): allow root elements with v-if, v-else-if and v-else --- .../compiler-app-plus-extra.service.spec.js | 6 ++++++ .../__tests__/compiler-app-plus-extra.view.spec.js | 6 ++++++ packages/uni-template-compiler/__tests__/demo.js | 13 ++++--------- .../lib/app/parser/base-parser.js | 10 ++++++++-- packages/uni-template-compiler/lib/app/service.js | 5 +++++ packages/uni-template-compiler/lib/app/view.js | 7 ++++++- 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js index 5db21b7a1..9538ae89c 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js @@ -136,6 +136,12 @@ describe('codegen', () => { '', `with(this){return _c('view',{attrs:{"data-b":_$s(0,'a-data-b',b),"_i":0}})}` ) + }) + it('generate v-if directive', () => { + assertCodegen( + '123d', + `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 */ diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js index 00e7dbfe7..23b14375a 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js @@ -75,6 +75,12 @@ describe('codegen', () => { '', `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( + '123d', + `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 */ diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js index 88d2fae46..d97e014ea 100644 --- a/packages/uni-template-compiler/__tests__/demo.js +++ b/packages/uni-template-compiler/__tests__/demo.js @@ -18,13 +18,8 @@ const scopedPath = path.resolve(__dirname, '../../') const compiler = require('../lib') const res = compiler.compile( - ` - - - - - - + ` +123d `, { miniprogram: true, resourcePath: '/User/fxy/Documents/test.wxml', @@ -37,9 +32,9 @@ const res = compiler.compile( mp: { platform: 'mp-weixin' }, - filterModules: ['swipe'] + filterModules: ['swipe'], // service: true, - // view: true + view: true }) console.log(require('util').inspect(res, { diff --git a/packages/uni-template-compiler/lib/app/parser/base-parser.js b/packages/uni-template-compiler/lib/app/parser/base-parser.js index 30fe8da69..538e294df 100644 --- a/packages/uni-template-compiler/lib/app/parser/base-parser.js +++ b/packages/uni-template-compiler/lib/app/parser/base-parser.js @@ -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) { if (!el.if) { return @@ -26,11 +30,13 @@ function parseIf (el, createGenVar, isScopedSlot) { isScopedSlot = false } 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) } }) - 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) { diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js index fc46da050..f7cfad5ff 100644 --- a/packages/uni-template-compiler/lib/app/service.js +++ b/packages/uni-template-compiler/lib/app/service.js @@ -156,6 +156,11 @@ function transformNode (el, parent, state, isScopedSlot) { function postTransformNode (el, options) { if (!el.parent) { // 从根节点开始递归处理 + if (options.root) { // 当根节点是由if,elseif,else组成 + parseIf(options.root, createGenVar) + } else { + options.root = el + } traverseNode(el, false, { forIteratorId: 0, transformNode, diff --git a/packages/uni-template-compiler/lib/app/view.js b/packages/uni-template-compiler/lib/app/view.js index 46242f920..7f7f179e4 100644 --- a/packages/uni-template-compiler/lib/app/view.js +++ b/packages/uni-template-compiler/lib/app/view.js @@ -155,6 +155,11 @@ function transformNode (el, parent, state, isScopedSlot) { function postTransformNode (el, options) { if (!el.parent) { // 从根节点开始递归处理 + if (options.root) { // 当根节点是由if,elseif,else组成 + parseIf(options.root, createGenVar) + } else { + options.root = el + } traverseNode(el, false, { forIteratorId: 0, transformNode, @@ -235,4 +240,4 @@ module.exports = { }, postTransformNode, genData -} +} -- GitLab