提交 c5b25d9f 编写于 作者: Q qiang

fix(mp): 修复事件中包含stop修饰符时复杂表达式编译错误的问题

上级 2c86c8a3
...@@ -736,6 +736,10 @@ describe('mp:compiler-extra', () => { ...@@ -736,6 +736,10 @@ describe('mp:compiler-extra', () => {
'<view v-for="item in list" @click="test(item)">{{ item }}</view>', '<view v-for="item in list" @click="test(item)">{{ item }}</view>',
'<block wx:for="{{list}}" wx:for-item="item" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'test\',[\'$0\'],[[[\'list\',\'\',__i0__]]]]]]]}}" bindtap="__e">{{item}}</view></block>' '<block wx:for="{{list}}" wx:for-item="item" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'test\',[\'$0\'],[[[\'list\',\'\',__i0__]]]]]]]}}" bindtap="__e">{{item}}</view></block>'
) )
assertCodegen(
'<view v-for="item in list" @click.stop="test(item)">{{ item }}</view>',
'<block wx:for="{{list}}" wx:for-item="item" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'test\',[\'$0\'],[[[\'list\',\'\',__i0__]]]]]]]}}" catchtap="__e">{{item}}</view></block>'
)
assertCodegen( assertCodegen(
'<view v-for="(item, index) in [{x:[1,2,3,4]}]"><view v-for="item2 in item.x" @click="test(item2)">{{ item2 }}+{{index}}</view></view>', '<view v-for="(item, index) in [{x:[1,2,3,4]}]"><view v-for="item2 in item.x" @click="test(item2)">{{ item2 }}+{{index}}</view></view>',
'<block wx:for="{{[{x:[1,2,3,4]}]}}" wx:for-item="item" wx:for-index="index"><view><block wx:for="{{item.x}}" wx:for-item="item2" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item2})}}" bindtap="__e">{{item2+"+"+index}}</view></block></view></block>', '<block wx:for="{{[{x:[1,2,3,4]}]}}" wx:for-item="item" wx:for-index="index"><view><block wx:for="{{item.x}}" wx:for-item="item2" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item2})}}" bindtap="__e">{{item2+"+"+index}}</view></block></view></block>',
...@@ -751,6 +755,11 @@ describe('mp:compiler-extra', () => { ...@@ -751,6 +755,11 @@ describe('mp:compiler-extra', () => {
'<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item})}}" bindtap="__e"></view></view></block>', '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item})}}" bindtap="__e"></view></view></block>',
'with(this){var l0=array();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(item)}}$mp.data=Object.assign({},{$root:{l0:l0}})}' 'with(this){var l0=array();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(item)}}$mp.data=Object.assign({},{$root:{l0:l0}})}'
) )
assertCodegen(
'<view v-for="(item, index) in array()"><view @click.stop="test(item)"></view></view>',
'<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item})}}" catchtap="__e"></view></view></block>',
'with(this){var l0=array();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;$event.stopPropagation();return test(item)}}$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen( assertCodegen(
'<view v-for="(item, index) in array()"><view @click="test(item)">{{item}}</view></view>', '<view v-for="(item, index) in array()"><view @click="test(item)">{{item}}</view></view>',
'<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item})}}" bindtap="__e">{{item}}</view></view></block>', '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item})}}" bindtap="__e">{{item}}</view></view></block>',
......
...@@ -320,19 +320,21 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, ...@@ -320,19 +320,21 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
} }
} }
const testCatch = function (path) { const testCatch = function (stop) {
// TODO 仅使用 name 容易误判 return function (path) {
if (path.node.object.name === '$event' && path.node.property.name === // TODO 仅使用 name 容易误判
'stopPropagation') { if (path.node.object.name === '$event' && path.node.property.name ===
isCatch = true 'stopPropagation') {
path.stop() isCatch = true
stop && path.stop()
}
} }
} }
// 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理 // 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理
if (anonymous && isSafeScoped(state)) { if (anonymous && isSafeScoped(state)) {
funcPath.traverse({ funcPath.traverse({
noScope: true, noScope: true,
MemberExpression: testCatch, MemberExpression: testCatch(),
AssignmentExpression (path) { // "update:title": function($event) {title = $event} AssignmentExpression (path) { // "update:title": function($event) {title = $event}
const left = path.node.left const left = path.node.left
const right = path.node.right const right = path.node.right
...@@ -370,7 +372,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, ...@@ -370,7 +372,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
if (anonymous) { if (anonymous) {
// 处理复杂表达式中使用的局部变量(主要在v-for中定义) // 处理复杂表达式中使用的局部变量(主要在v-for中定义)
funcPath.traverse({ funcPath.traverse({
MemberExpression: testCatch, MemberExpression: testCatch(),
Identifier (path) { Identifier (path) {
const scope = path.scope const scope = path.scope
const node = path.node const node = path.node
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册