From 7c234e52701e52c1102c9e812e3a724af7309702 Mon Sep 17 00:00:00 2001 From: qiang Date: Sat, 1 Aug 2020 15:33:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=B9=B3=E5=8F=B0=20v-if=3D"false"=20=E6=97=B6?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E5=86=85=E9=83=A8=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=92=8C=E8=BF=87=E6=BB=A4=E5=99=A8=E4=BB=8D=E7=84=B6=E4=BC=9A?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E7=9A=84=E9=97=AE=E9=A2=98=20fixed=20#334=20?= =?UTF-8?q?#1874?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/compiler-extra.spec.js | 31 ++++++++++++---- .../lib/script/traverse/member-expr.js | 35 ++++++++++++++++++- .../lib/script/traverse/statements.js | 14 +++++--- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index e8bc904d7..b720fe778 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -553,22 +553,22 @@ describe('mp:compiler-extra', () => { assertCodegen( '{{handle(item)}}', '{{item.m0}}', - 'with(this){var l0=__map(list,function(item,index){var m0=handle(item);return{$orig:__get_orig(item),m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( '{{handle(item)}}{{item.title}}', '{{item.m0+item.$orig.title}}', - 'with(this){var l0=__map(list,function(item,index){var m0=handle(item);return{$orig:__get_orig(item),m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( '{{handle(item1)}}{{item1.title}}', '{{item1.m0+item1.$orig.title}}', - 'with(this){var l1=__map(list,function(item,index){var l0=__map(list1,function(item1,index1){var m0=handle(item1);return{$orig:__get_orig(item1),m0:m0}});return{$orig:__get_orig(item),l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + 'with(this){var l1=__map(list,function(item,index){var $orig=__get_orig(item);var l0=__map(list1,function(item1,index1){var $orig=__get_orig(item1);var m0=handle(item1);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' ) assertCodegen( 'title: {{ section.title|prefix }}{{ sub_titles|prefix }}', '{{"title: "+section.f0}}{{sub_titles.f1}}', - 'with(this){var l1=__map(sections,function(section,index){var f0=_f("prefix")(section.title);var l0=__map(section.sub_titles,function(sub_titles,_index){var f1=_f("prefix")(sub_titles);return{$orig:__get_orig(sub_titles),f1:f1}});return{$orig:__get_orig(section),f0:f0,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + 'with(this){var l1=__map(sections,function(section,index){var $orig=__get_orig(section);var f0=_f("prefix")(section.title);var l0=__map(section.sub_titles,function(sub_titles,_index){var $orig=__get_orig(sub_titles);var f1=_f("prefix")(sub_titles);return{$orig:$orig,f1:f1}});return{$orig:$orig,f0:f0,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' ) assertCodegen( @@ -579,12 +579,12 @@ describe('mp:compiler-extra', () => { assertCodegen( '{{item.item.id | test | test1}}', '{{item.f0}}', - 'with(this){var l0=__map(list,function(item,index){var f0=_f("test1")(_f("test")(item.item.id));return{$orig:__get_orig(item),f0:f0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var f0=_f("test1")(_f("test")(item.item.id));return{$orig:$orig,f0:f0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( '{{ item.split(\'\').join(\' \') }}', '{{item.g0}}', - 'with(this){var l0=__map(list,function(item,i){var g0=item.split("").join(" ");return{$orig:__get_orig(item),g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,i){var $orig=__get_orig(item);var g0=item.split("").join(" ");return{$orig:$orig,g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( ` @@ -595,7 +595,7 @@ describe('mp:compiler-extra', () => { `, '{{\'\'+item2.m0+\'\'}}', - 'with(this){var l1=__map(tabList,function(item,index){var l0=__map(list[item.key],function(item2,index2){var m0=formatIt(item2.id);return{$orig:__get_orig(item2),m0:m0}});return{$orig:__get_orig(item),l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + 'with(this){var l1=__map(tabList,function(item,index){var $orig=__get_orig(item);var l0=__map(list[item.key],function(item2,index2){var $orig=__get_orig(item2);var m0=formatIt(item2.id);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' ) }) @@ -676,4 +676,21 @@ describe('mp:compiler-extra', () => { '' ) }) + it('generate v-if', () => { + assertCodegen( + '{{getValue(key)}}', + '{{$root.m0}}', + 'with(this){var m0=show?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0}})}' + ) + assertCodegen( + '{{getValue(key)}}', + '{{$root.m1}}', + 'with(this){var m0=getValue(key);var m1=m0?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}' + ) + assertCodegen( + '{{getValue(item)}}', + '{{item.m0}}', + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + ) + }) }) diff --git a/packages/uni-template-compiler/lib/script/traverse/member-expr.js b/packages/uni-template-compiler/lib/script/traverse/member-expr.js index 9678461d8..06e16d73a 100644 --- a/packages/uni-template-compiler/lib/script/traverse/member-expr.js +++ b/packages/uni-template-compiler/lib/script/traverse/member-expr.js @@ -1,4 +1,9 @@ const t = require('@babel/types') +const traverse = require('@babel/traverse').default + +const { + VAR_ROOT +} = require('../../constants') function isMatch (name, forItem, forIndex) { return name === forItem || name === forIndex @@ -33,8 +38,36 @@ function findScoped (path, state) { return scoped || state } +function findTest (path, state) { + let tests + while (path.parentPath && path.key !== 'body') { + if (path.key === 'consequent' || path.key === 'alternate') { + let test = t.arrayExpression([t.clone(path.container.test)]) + traverse(test, { + noScope: true, + MemberExpression (path) { + const names = state.scoped.map(scoped => scoped.forItem) + const node = path.node + const objectName = node.object.name + if (objectName === VAR_ROOT || names.includes(objectName)) { + path.replaceWith(node.property) + } + } + }) + test = test.elements[0] + if (path.key === 'alternate') { + test = t.unaryExpression('!', test) + } + tests = tests ? t.logicalExpression('&&', test, tests) : test + } + path = path.parentPath + } + return tests +} + module.exports = function getMemberExpr (path, name, init, state, variableDeclaration = true) { const scoped = findScoped(path, state) + const test = findTest(path, state) if (!variableDeclaration) { scoped.declarationArray.push(t.expressionStatement(init)) @@ -45,7 +78,7 @@ module.exports = function getMemberExpr (path, name, init, state, variableDeclar scoped.propertyArray.push(t.objectProperty(identifier, identifier)) scoped.declarationArray.push( - t.variableDeclaration('var', [t.variableDeclarator(identifier, init)]) + t.variableDeclaration('var', [t.variableDeclarator(identifier, test ? t.conditionalExpression(test, init, t.nullLiteral()) : init)]) ) state.identifierArray.push(identifier) diff --git a/packages/uni-template-compiler/lib/script/traverse/statements.js b/packages/uni-template-compiler/lib/script/traverse/statements.js index ea53c462e..8d2725c29 100644 --- a/packages/uni-template-compiler/lib/script/traverse/statements.js +++ b/packages/uni-template-compiler/lib/script/traverse/statements.js @@ -43,20 +43,24 @@ function getMapCallExpression ( forIndex ) { const blockStatement = [] + // var $orgi = __get_orig(forItem) + blockStatement.push(t.variableDeclaration('var', [ + t.variableDeclarator(t.identifier(VAR_ORIGINAL), t.callExpression(t.identifier(INTERNAL_GET_ORIG), [ + t.identifier(forItem) + ])) + ])) if (declarationArray.length) { declarationArray.forEach(declaration => { blockStatement.push(declaration) }) blockStatement.push(t.returnStatement( - // return {$orgi:__get_orig(forItem)} + // return {$orgi:$orgi} t.objectExpression( [ t.objectProperty( t.identifier(VAR_ORIGINAL), - t.callExpression(t.identifier(INTERNAL_GET_ORIG), [ - t.identifier(forItem) - ]) + t.identifier(VAR_ORIGINAL) ) ].concat(objectPropertyArray) ) @@ -116,4 +120,4 @@ module.exports = { getMapCallExpression, getDataExpressionStatement, getEventExpressionStatement -} +} -- GitLab