Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
我本凡人 一念云泥
uni-app
提交
7c234e52
uni-app
项目概览
我本凡人 一念云泥
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
7c234e52
编写于
8月 01, 2020
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 修复小程序平台 v-if="false" 时标签内部的方法和过滤器仍然会执行的问题 fixed #334 #1874
上级
0f4c7c7f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
67 addition
and
13 deletion
+67
-13
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
...es/uni-template-compiler/__tests__/compiler-extra.spec.js
+24
-7
packages/uni-template-compiler/lib/script/traverse/member-expr.js
.../uni-template-compiler/lib/script/traverse/member-expr.js
+34
-1
packages/uni-template-compiler/lib/script/traverse/statements.js
...s/uni-template-compiler/lib/script/traverse/statements.js
+9
-5
未找到文件。
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
浏览文件 @
7c234e52
...
...
@@ -553,22 +553,22 @@ describe('mp:compiler-extra', () => {
assertCodegen
(
'
<view><view v-for="(item, index) in list"><view>{{handle(item)}}</view></view></view>
'
,
'
<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view>{{item.m0}}</view></view></block></view>
'
,
'
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
(
'
<view><view v-for="(item, index) in list"><view>{{handle(item)}}{{item.title}}</view></view></view>
'
,
'
<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view>{{item.m0+item.$orig.title}}</view></view></block></view>
'
,
'
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
(
'
<view><view v-for="(item, index) in list"><view v-for="(item1,index1) in list1">{{handle(item1)}}{{item1.title}}</view></view></view>
'
,
'
<view><block wx:for="{{$root.l1}}" wx:for-item="item" wx:for-index="index"><view><block wx:for="{{item.l0}}" wx:for-item="item1" wx:for-index="index1"><view>{{item1.m0+item1.$orig.title}}</view></block></view></block></view>
'
,
'
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
(
'
<view v-for="(section, index) in sections" :key="index">title: {{ section.title|prefix }}<view v-for="(sub_titles, _index) in section.sub_titles" :key="_index">{{ sub_titles|prefix }}</view></view>
'
,
'
<block wx:for="{{$root.l1}}" wx:for-item="section" wx:for-index="index" wx:key="index"><view>{{"title: "+section.f0}}<block wx:for="{{section.l0}}" wx:for-item="sub_titles" wx:for-index="_index" wx:key="_index"><view>{{sub_titles.f1}}</view></block></view></block>
'
,
'
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
(
'
<view v-for="(item, index) in list" :key="item">{{item.item.id | test | test1}}</view>
'
,
'
<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="$orig"><view>{{item.f0}}</view></block>
'
,
'
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
(
'
<text v-for="(item, i) in list" v-bind:key="i" >{{ item.split(
\'\'
).join(
\'
\'
) }}</text>
'
,
'
<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="i" wx:key="i"><text>{{item.g0}}</text></block>
'
,
'
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
(
`<view class="content">
...
...
@@ -595,7 +595,7 @@ describe('mp:compiler-extra', () => {
</view>
</view>`
,
'
<view class="content"><block wx:for="{{$root.l1}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:for="{{item.l0}}" wx:for-item="item2" wx:for-index="index2" wx:key="index2"><view data-event-opts="{{[[
\'
tap
\'
,[[
\'
show
\'
,[
\'
$0
\'
,
\'
$1
\'
],[[[
\'
list.
\'
+item.$orig.key+
\'\'
,
\'\'
,index2,
\'
id
\'
]],[[
\'
list.
\'
+item.$orig.key+
\'\'
,
\'\'
,index2,
\'
id
\'
]]]]]]]}}" bindtap="__e">{{
\'\'
+item2.m0+
\'\'
}}</view></block></view></block></view>
'
,
'
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', () => {
'
<video controls></video>
'
)
})
it
(
'
generate v-if
'
,
()
=>
{
assertCodegen
(
'
<view v-if="show">{{getValue(key)}}</view>
'
,
'
<block wx:if="{{show}}"><view>{{$root.m0}}</view></block>
'
,
'
with(this){var m0=show?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0}})}
'
)
assertCodegen
(
'
<view v-if="getValue(key)">{{getValue(key)}}</view>
'
,
'
<block wx:if="{{$root.m0}}"><view>{{$root.m1}}</view></block>
'
,
'
with(this){var m0=getValue(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>
'
,
'
<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig}}"><view>{{item.m0}}</view></block></view></block>
'
,
'
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}})}
'
)
})
})
packages/uni-template-compiler/lib/script/traverse/member-expr.js
浏览文件 @
7c234e52
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
)
...
...
packages/uni-template-compiler/lib/script/traverse/statements.js
浏览文件 @
7c234e52
...
...
@@ -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
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录