提交 1ac12bed 编写于 作者: L liuxiaohang

fix(mp-weixin): 修复for循环中,事件引用循环值报错

上级 158a9934
...@@ -699,6 +699,11 @@ describe('mp:compiler-extra', () => { ...@@ -699,6 +699,11 @@ 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, 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>',
'with(this){if(!_isMounted){e0=function($event,item2){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item2=_temp2.item2;var _temp,_temp2;return test(item2)}}}'
)
}) })
it('generate bool attr', () => { it('generate bool attr', () => {
assertCodegen( assertCodegen(
......
...@@ -304,47 +304,60 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, ...@@ -304,47 +304,60 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
} }
} }
anonymous && funcPath.traverse({ // 判断是否是复杂表达式 数组 或 对象
noScope: true, const isNotDynamicExpression = (state) => {
MemberExpression (path) { if (!(state.scoped[0])) {
if (path.node.object.name === '$event' && path.node.property.name === return true
'stopPropagation') { }
isCatch = true const value = state.scoped[0].forExtra[0].elements[0].value
path.stop() return !(typeof value === 'string' && (value.startsWith('[') || value.startsWith('}')))
} }
},
AssignmentExpression (path) { // "update:title": function($event) {title = $event} // 如果v-for遍历的值为 数组、对象 则进入复杂表达式
const left = path.node.left if (isNotDynamicExpression(state)) {
const right = path.node.right anonymous && funcPath.traverse({
// v-bind:title.sync="title" noScope: true,
if (t.isIdentifier(left) && MemberExpression (path) {
t.isIdentifier(right) && if (path.node.object.name === '$event' && path.node.property.name ===
right.name === '$event' && 'stopPropagation') {
type.indexOf('update:') === 0) { isCatch = true
methods.push(t.arrayExpression( // ['$set',['title','$event']] path.stop()
[ }
t.stringLiteral(INTERNAL_SET_SYNC), },
t.arrayExpression([ AssignmentExpression (path) { // "update:title": function($event) {title = $event}
t.identifier(left.name), const left = path.node.left
t.stringLiteral(left.name), const right = path.node.right
t.stringLiteral('$event') // v-bind:title.sync="title"
]) if (t.isIdentifier(left) &&
] t.isIdentifier(right) &&
)) right.name === '$event' &&
anonymous = false type.indexOf('update:') === 0) {
path.stop() methods.push(t.arrayExpression( // ['$set',['title','$event']]
} [
}, t.stringLiteral(INTERNAL_SET_SYNC),
ReturnStatement (path) { t.arrayExpression([
const argument = path.node.argument t.identifier(left.name),
if (t.isCallExpression(argument)) { t.stringLiteral(left.name),
if (t.isIdentifier(argument.callee)) { // || t.isMemberExpression(argument.callee) t.stringLiteral('$event')
])
]
))
anonymous = false anonymous = false
parseEventByCallExpression(argument, methods) path.stop()
}
},
ReturnStatement (path) {
const argument = path.node.argument
if (t.isCallExpression(argument)) {
if (t.isIdentifier(argument.callee)) { // || t.isMemberExpression(argument.callee)
anonymous = false
parseEventByCallExpression(argument, methods)
}
} }
} }
} })
}) }
if (anonymous) { if (anonymous) {
// 处理复杂表达式中使用的局部变量(主要在v-for中定义) // 处理复杂表达式中使用的局部变量(主要在v-for中定义)
funcPath.traverse({ funcPath.traverse({
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册