From 1ac12bedd278888987f488ac62ba7b26b1ca7e9d Mon Sep 17 00:00:00 2001
From: liuxiaohang <283700113@qq.com>
Date: Thu, 12 Nov 2020 15:18:05 +0800
Subject: [PATCH] =?UTF-8?q?fix(mp-weixin):=20=20=E4=BF=AE=E5=A4=8Dfor?=
=?UTF-8?q?=E5=BE=AA=E7=8E=AF=E4=B8=AD=EF=BC=8C=E4=BA=8B=E4=BB=B6=E5=BC=95?=
=?UTF-8?q?=E7=94=A8=E5=BE=AA=E7=8E=AF=E5=80=BC=E6=8A=A5=E9=94=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../__tests__/compiler-extra.spec.js | 5 ++
.../lib/script/traverse/data/event.js | 89 +++++++++++--------
2 files changed, 56 insertions(+), 38 deletions(-)
diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
index 73343bfaea..8cee04c31c 100644
--- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
@@ -699,6 +699,11 @@ describe('mp:compiler-extra', () => {
'{{ item }}',
'{{item}}'
)
+ assertCodegen(
+ '{{ item2 }}+{{index}}',
+ '{{item2+"+"+index}}',
+ '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', () => {
assertCodegen(
diff --git a/packages/uni-template-compiler/lib/script/traverse/data/event.js b/packages/uni-template-compiler/lib/script/traverse/data/event.js
index 2cb47a7d39..ce784af4f6 100644
--- a/packages/uni-template-compiler/lib/script/traverse/data/event.js
+++ b/packages/uni-template-compiler/lib/script/traverse/data/event.js
@@ -304,47 +304,60 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
}
}
- anonymous && funcPath.traverse({
- noScope: true,
- MemberExpression (path) {
- if (path.node.object.name === '$event' && path.node.property.name ===
- 'stopPropagation') {
- isCatch = true
- path.stop()
- }
- },
- AssignmentExpression (path) { // "update:title": function($event) {title = $event}
- const left = path.node.left
- const right = path.node.right
- // v-bind:title.sync="title"
- if (t.isIdentifier(left) &&
- t.isIdentifier(right) &&
- right.name === '$event' &&
- type.indexOf('update:') === 0) {
- methods.push(t.arrayExpression( // ['$set',['title','$event']]
- [
- t.stringLiteral(INTERNAL_SET_SYNC),
- t.arrayExpression([
- t.identifier(left.name),
- t.stringLiteral(left.name),
- t.stringLiteral('$event')
- ])
- ]
- ))
- anonymous = false
- path.stop()
- }
- },
- ReturnStatement (path) {
- const argument = path.node.argument
- if (t.isCallExpression(argument)) {
- if (t.isIdentifier(argument.callee)) { // || t.isMemberExpression(argument.callee)
+ // 判断是否是复杂表达式 数组 或 对象
+ const isNotDynamicExpression = (state) => {
+ if (!(state.scoped[0])) {
+ return true
+ }
+ const value = state.scoped[0].forExtra[0].elements[0].value
+ return !(typeof value === 'string' && (value.startsWith('[') || value.startsWith('}')))
+ }
+
+ // 如果v-for遍历的值为 数组、对象 则进入复杂表达式
+ if (isNotDynamicExpression(state)) {
+ anonymous && funcPath.traverse({
+ noScope: true,
+ MemberExpression (path) {
+ if (path.node.object.name === '$event' && path.node.property.name ===
+ 'stopPropagation') {
+ isCatch = true
+ path.stop()
+ }
+ },
+ AssignmentExpression (path) { // "update:title": function($event) {title = $event}
+ const left = path.node.left
+ const right = path.node.right
+ // v-bind:title.sync="title"
+ if (t.isIdentifier(left) &&
+ t.isIdentifier(right) &&
+ right.name === '$event' &&
+ type.indexOf('update:') === 0) {
+ methods.push(t.arrayExpression( // ['$set',['title','$event']]
+ [
+ t.stringLiteral(INTERNAL_SET_SYNC),
+ t.arrayExpression([
+ t.identifier(left.name),
+ t.stringLiteral(left.name),
+ t.stringLiteral('$event')
+ ])
+ ]
+ ))
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) {
// 处理复杂表达式中使用的局部变量(主要在v-for中定义)
funcPath.traverse({
--
GitLab