Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yangkaifeng
uni-app
提交
4186e18a
U
uni-app
项目概览
yangkaifeng
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4186e18a
编写于
9月 24, 2020
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 修复小程序 v-for 中部分事件表达式执行报错的问题 fixed #2132
上级
90048107
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
32 addition
and
5 deletion
+32
-5
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
...es/uni-template-compiler/__tests__/compiler-extra.spec.js
+5
-0
packages/uni-template-compiler/lib/script/traverse/data/event.js
...s/uni-template-compiler/lib/script/traverse/data/event.js
+27
-5
未找到文件。
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
浏览文件 @
4186e18a
...
...
@@ -337,6 +337,11 @@ describe('mp:compiler-extra', () => {
'
<block wx:for="{{list}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><view data-event-opts="{{[[
\'
tap
\'
,[[
\'
e0
\'
,[
\'
$event
\'
]]]]]}}" data-event-params="{{({item})}}" bindtap="__e"></view></view></block>
'
,
'
with(this){if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return $test.test(item,item.length,undefined)}}}
'
)
assertCodegen
(
'
<view v-for="(item,index) in list" :key="index"><view @click="event=>showEventInfo(event,item)"></view></view>
'
,
'
<block wx:for="{{list}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><view data-event-opts="{{[[
\'
tap
\'
,[[
\'
e0
\'
,[
\'
$event
\'
]]]]]}}" data-event-params="{{({item})}}" bindtap="__e"></view></view></block>
'
,
'
with(this){if(!_isMounted){e0=(event,item,...args)=>{var _temp=args[args.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return showEventInfo(event,item)}}}
'
)
})
it
(
'
generate class binding
'
,
()
=>
{
...
...
packages/uni-template-compiler/lib/script/traverse/data/event.js
浏览文件 @
4186e18a
...
...
@@ -202,6 +202,10 @@ function parseEventByCallExpression (callExpr, methods) {
methods
.
push
(
t
.
arrayExpression
(
arrayExpression
))
}
function
isValuePath
(
path
)
{
return
path
.
key
!==
'
key
'
&&
path
.
key
!==
'
id
'
&&
(
path
.
key
!==
'
property
'
||
path
.
parent
.
computed
)
&&
!
(
path
.
key
===
'
value
'
&&
path
.
parentPath
.
parentPath
.
isObjectPattern
())
&&
!
(
path
.
key
===
'
left
'
&&
path
.
parentPath
.
parentPath
.
parentPath
.
isObjectPattern
())
}
function
parseEvent
(
keyPath
,
valuePath
,
state
,
isComponent
,
isNativeOn
=
false
,
tagName
,
ret
)
{
const
key
=
keyPath
.
node
let
type
=
key
.
value
||
key
.
name
||
''
...
...
@@ -268,18 +272,31 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
state
.
errors
.
add
(
`
${
tagName
}
组件
${
type
}
事件仅支持 @
${
type
}
="methodName" 方式绑定`
)
}
else
if
(
funcPath
.
isArrowFunctionExpression
())
{
// e=>count++
methods
.
push
(
addEventExpressionStatement
(
funcPath
,
state
,
isCustom
))
}
else
{
let
anonymous
=
true
// "click":function($event) {click1(item);click2(item);}
const
body
=
funcPath
.
node
.
body
&&
funcPath
.
node
.
body
.
body
if
(
body
&&
body
.
length
)
{
const
funcParams
=
funcPath
.
node
.
params
if
(
body
&&
body
.
length
&&
funcParams
&&
funcParams
.
length
===
1
)
{
const
exprStatements
=
body
.
filter
(
node
=>
{
return
t
.
isExpressionStatement
(
node
)
&&
t
.
isCallExpression
(
node
.
expression
)
})
if
(
exprStatements
.
length
===
body
.
length
)
{
const
paramPath
=
funcPath
.
get
(
'
params
'
)[
0
]
const
paramName
=
paramPath
.
node
.
name
if
(
paramName
!==
'
$event
'
)
{
funcPath
.
get
(
'
body
'
).
traverse
({
Identifier
(
path
)
{
const
node
=
path
.
node
const
binding
=
path
.
scope
.
getBinding
(
node
.
name
)
if
(
binding
&&
binding
.
identifier
===
paramPath
.
node
&&
isValuePath
(
path
))
{
path
.
replaceWith
(
t
.
identifier
(
'
$event
'
))
}
}
})
paramPath
.
replaceWith
(
t
.
identifier
(
'
$event
'
))
}
anonymous
=
false
exprStatements
.
forEach
(
exprStatement
=>
{
parseEventByCallExpression
(
exprStatement
.
expression
,
methods
)
...
...
@@ -335,7 +352,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
const
scope
=
path
.
scope
const
node
=
path
.
node
const
name
=
node
.
name
if
(
path
.
key
!==
'
key
'
&&
(
path
.
key
!==
'
property
'
||
path
.
parent
.
computed
)
&&
scope
&&
!
scope
.
hasOwnBinding
(
name
)
&&
scope
.
hasBinding
(
name
)
&&
!
params
.
includes
(
name
)
&&
name
!==
'
undefined
'
)
{
if
(
isValuePath
(
path
)
&&
scope
&&
!
scope
.
hasOwnBinding
(
name
)
&&
scope
.
hasBinding
(
name
)
&&
!
params
.
includes
(
name
)
&&
name
!==
'
undefined
'
)
{
params
.
push
(
name
)
}
}
...
...
@@ -344,10 +361,15 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
funcPath
.
node
.
params
.
push
(
t
.
identifier
(
name
))
})
if
(
params
.
length
)
{
let
argumentsName
=
'
arguments
'
if
(
funcPath
.
isArrowFunctionExpression
())
{
argumentsName
=
'
args
'
funcPath
.
node
.
params
.
push
(
t
.
restElement
(
t
.
identifier
(
argumentsName
)))
}
const
datasetUid
=
funcPath
.
scope
.
generateDeclaredUidIdentifier
().
name
const
paramsUid
=
funcPath
.
scope
.
generateDeclaredUidIdentifier
().
name
const
dataset
=
ATTR_DATA_EVENT_PARAMS
.
substring
(
5
)
const
code
=
`var
${
datasetUid
}
=
arguments[arguments
.length-1].currentTarget.dataset,
${
paramsUid
}
=
${
datasetUid
}
.
${
dataset
.
replace
(
/-
([
a-z
])
/
,
(
_
,
str
)
=>
str
.
toUpperCase
())}
||
${
datasetUid
}
['
${
dataset
}
'],
${
params
.
map
(
item
=>
`
${
item
}
=
${
paramsUid
}
.
${
item
}
`
).
join
(
'
,
'
)}
`
const
code
=
`var
${
datasetUid
}
=
${
argumentsName
}
[
${
argumentsName
}
.length-1].currentTarget.dataset,
${
paramsUid
}
=
${
datasetUid
}
.
${
dataset
.
replace
(
/-
([
a-z
])
/
,
(
_
,
str
)
=>
str
.
toUpperCase
())}
||
${
datasetUid
}
['
${
dataset
}
'],
${
params
.
map
(
item
=>
`
${
item
}
=
${
paramsUid
}
.
${
item
}
`
).
join
(
'
,
'
)}
`
funcPath
.
node
.
body
.
body
.
unshift
(
parser
.
parse
(
code
).
program
.
body
[
0
])
}
methods
.
push
(
addEventExpressionStatement
(
funcPath
,
state
,
isComponent
,
isNativeOn
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录