Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦里逆天
uni-app
提交
efd75c54
U
uni-app
项目概览
梦里逆天
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
22
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
efd75c54
编写于
8月 15, 2020
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 小程序端支持 v-for 遍历复杂表达式 fixed #2012
上级
59948f40
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
42 addition
and
8 deletion
+42
-8
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
...es/uni-template-compiler/__tests__/compiler-extra.spec.js
+19
-0
packages/uni-template-compiler/lib/script/traverse/data/attrs.js
...s/uni-template-compiler/lib/script/traverse/data/attrs.js
+6
-4
packages/uni-template-compiler/lib/script/traverse/render-list.js
.../uni-template-compiler/lib/script/traverse/render-list.js
+5
-2
packages/uni-template-compiler/lib/util.js
packages/uni-template-compiler/lib/util.js
+12
-2
未找到文件。
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
浏览文件 @
efd75c54
...
@@ -602,6 +602,25 @@ describe('mp:compiler-extra', () => {
...
@@ -602,6 +602,25 @@ describe('mp:compiler-extra', () => {
'
<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>
'
,
'
<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 $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}})}
'
'
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}})}
'
)
)
assertCodegen
(
'
<view><view v-for="(item, key) in {x:
\'
x
\'
}"><view>{{item}}</view></view></view>
'
,
'
<view><block wx:for="{{({x:
\'
x
\'
})}}" wx:for-item="item" wx:for-index="key"><view><view>{{item}}</view></view></block></view>
'
)
assertCodegen
(
'
<view><view v-for="(item, key) in {
\'
x-x
\'
:
\'
x
\'
}"><view>{{item}}</view></view></view>
'
,
'
<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="key"><view><view>{{item}}</view></view></block></view>
'
,
'
with(this){var l0={"x-x":"x"};$mp.data=Object.assign({},{$root:{l0:l0}})}
'
)
assertCodegen
(
'
<view><view v-for="(item, index) in getList(test)"><view>{{item}}</view></view></view>
'
,
'
<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view>{{item}}</view></view></block></view>
'
,
'
with(this){var l0=getList(test);$mp.data=Object.assign({},{$root:{l0:l0}})}
'
)
assertCodegen
(
'
<view><view v-for="(item, index) in list[get(test)]"><view>{{item}}</view></view></view>
'
,
'
<view><block wx:for="{{list[$root.m0]}}" wx:for-item="item" wx:for-index="index"><view><view>{{item}}</view></view></block></view>
'
,
'
with(this){var m0=get(test);$mp.data=Object.assign({},{$root:{m0:m0}})}
'
)
})
})
it
(
'
generate TemplateLiteral
'
,
()
=>
{
it
(
'
generate TemplateLiteral
'
,
()
=>
{
...
...
packages/uni-template-compiler/lib/script/traverse/data/attrs.js
浏览文件 @
efd75c54
const
t
=
require
(
'
@babel/types
'
)
const
{
const
{
IDENTIFIER_ATTR
IDENTIFIER_ATTR
}
=
require
(
'
../../../constants
'
)
}
=
require
(
'
../../../constants
'
)
const
{
isSimpleObjectExpression
}
=
require
(
'
../../../util
'
)
const
getMemberExpr
=
require
(
'
../member-expr
'
)
const
getMemberExpr
=
require
(
'
../member-expr
'
)
module
.
exports
=
function
processAttrs
(
paths
,
path
,
state
,
isComponent
,
tagName
)
{
module
.
exports
=
function
processAttrs
(
paths
,
path
,
state
,
isComponent
,
tagName
)
{
...
@@ -11,8 +13,8 @@ module.exports = function processAttrs (paths, path, state, isComponent, tagName
...
@@ -11,8 +13,8 @@ module.exports = function processAttrs (paths, path, state, isComponent, tagName
if
(
attrsPath
)
{
if
(
attrsPath
)
{
attrsPath
.
get
(
'
value.properties
'
).
forEach
(
propertyPath
=>
{
attrsPath
.
get
(
'
value.properties
'
).
forEach
(
propertyPath
=>
{
const
valuePath
=
propertyPath
.
get
(
'
value
'
)
const
valuePath
=
propertyPath
.
get
(
'
value
'
)
// 对于
普通
的ObjectExpression不再单独处理,改为在转换temlplte时用()包裹(微信、QQ)
// 对于
简单
的ObjectExpression不再单独处理,改为在转换temlplte时用()包裹(微信、QQ)
if
(
valuePath
.
isObjectExpression
()
&&
valuePath
.
node
.
properties
.
find
(({
key
,
value
})
=>
!
t
.
isIdentifier
(
key
)
||
!
(
t
.
isIdentifier
(
value
)
||
t
.
isStringLiteral
(
value
)
||
t
.
isBooleanLiteral
(
value
)
||
t
.
isNumericLiteral
(
value
)
||
t
.
isNullLiteral
(
value
))
))
{
if
(
valuePath
.
isObjectExpression
()
&&
!
isSimpleObjectExpression
(
valuePath
.
node
))
{
valuePath
.
replaceWith
(
getMemberExpr
(
path
,
IDENTIFIER_ATTR
,
valuePath
.
node
,
state
))
valuePath
.
replaceWith
(
getMemberExpr
(
path
,
IDENTIFIER_ATTR
,
valuePath
.
node
,
state
))
}
}
})
})
...
...
packages/uni-template-compiler/lib/script/traverse/render-list.js
浏览文件 @
efd75c54
...
@@ -15,7 +15,8 @@ const {
...
@@ -15,7 +15,8 @@ const {
genCode
,
genCode
,
traverseKey
,
traverseKey
,
processMemberExpression
,
processMemberExpression
,
getForIndexIdentifier
getForIndexIdentifier
,
isSimpleObjectExpression
}
=
require
(
'
../../util
'
)
}
=
require
(
'
../../util
'
)
const
getMemberExpr
=
require
(
'
./member-expr
'
)
const
getMemberExpr
=
require
(
'
./member-expr
'
)
...
@@ -184,6 +185,8 @@ module.exports = function traverseRenderList (path, state) {
...
@@ -184,6 +185,8 @@ module.exports = function traverseRenderList (path, state) {
}
}
})
})
}
}
}
else
if
(
forPath
.
isCallExpression
()
||
(
forPath
.
isObjectExpression
()
&&
!
isSimpleObjectExpression
(
forPath
.
node
)))
{
forPath
.
replaceWith
(
getMemberExpr
(
forPath
,
IDENTIFIER_FOR
,
forPath
.
node
,
forState
))
}
else
{
}
else
{
forPath
.
traverse
(
require
(
'
./visitor
'
),
forState
)
forPath
.
traverse
(
require
(
'
./visitor
'
),
forState
)
}
}
...
@@ -195,4 +198,4 @@ module.exports = function traverseRenderList (path, state) {
...
@@ -195,4 +198,4 @@ module.exports = function traverseRenderList (path, state) {
forState
.
declarationArray
.
forEach
(
declaration
=>
{
forState
.
declarationArray
.
forEach
(
declaration
=>
{
state
.
declarationArray
.
push
(
declaration
)
state
.
declarationArray
.
push
(
declaration
)
})
})
}
}
packages/uni-template-compiler/lib/util.js
浏览文件 @
efd75c54
...
@@ -213,6 +213,15 @@ function makeMap (str, expectsLowerCase) {
...
@@ -213,6 +213,15 @@ function makeMap (str, expectsLowerCase) {
?
val
=>
map
[
val
.
toLowerCase
()]
?
val
=>
map
[
val
.
toLowerCase
()]
:
val
=>
map
[
val
]
:
val
=>
map
[
val
]
}
}
/**
* 微信、QQ小程序模板支持的简单类型
* @param {*} node
*/
function
isSimpleObjectExpression
(
node
)
{
return
t
.
isObjectExpression
(
node
)
&&
!
node
.
properties
.
find
(({
key
,
value
})
=>
!
t
.
isIdentifier
(
key
)
||
!
(
t
.
isIdentifier
(
value
)
||
t
.
isStringLiteral
(
value
)
||
t
.
isBooleanLiteral
(
value
)
||
t
.
isNumericLiteral
(
value
)
||
t
.
isNullLiteral
(
value
)))
}
module
.
exports
=
{
module
.
exports
=
{
hasOwn
,
hasOwn
,
isUnaryTag
:
makeMap
(
isUnaryTag
:
makeMap
(
...
@@ -242,5 +251,6 @@ module.exports = {
...
@@ -242,5 +251,6 @@ module.exports = {
return
str
return
str
}),
}),
processMemberExpression
,
processMemberExpression
,
getForIndexIdentifier
getForIndexIdentifier
,
}
isSimpleObjectExpression
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录