diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
index 052e92b38a9649d4290f72e93edf20cfc338c171..b8863cf8ac62ec8e209bb5edd55af2d3f247ea6b 100644
--- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
@@ -727,6 +727,16 @@ describe('mp:compiler-extra', () => {
'{{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)}}}'
)
+ assertCodegen(
+ '',
+ '',
+ 'with(this){var l0=array();$mp.data=Object.assign({},{$root:{l0:l0}})}'
+ )
+ assertCodegen(
+ '',
+ '',
+ '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}})}'
+ )
})
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 ce784af4f6d868b2cacab9a61bbf262f1f9d7c97..c9b601cdf2f60100402898c46f007d574fc67b8e 100644
--- a/packages/uni-template-compiler/lib/script/traverse/data/event.js
+++ b/packages/uni-template-compiler/lib/script/traverse/data/event.js
@@ -206,6 +206,22 @@ 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())
}
+/**
+ * 判断 v-for 中是否包含复杂表达式:数组、对象、方法
+ */
+const isSafeScoped = (state) => {
+ const scopedArray = state.scoped
+ for (let index = 0; index < scopedArray.length; index++) {
+ const scoped = scopedArray[index]
+ const arrayExtra = scoped.forExtra[0].elements[0].value
+ // 简易判断
+ if (typeof arrayExtra === 'string' && (arrayExtra.startsWith('[') || arrayExtra.startsWith('{') || /\(.*\)/.test(arrayExtra))) {
+ return false
+ }
+ }
+ return true
+}
+
function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, tagName, ret) {
const key = keyPath.node
let type = key.value || key.name || ''
@@ -304,18 +320,9 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
}
}
- // 判断是否是复杂表达式 数组 或 对象
- 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({
+ // 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理
+ if (anonymous && isSafeScoped(state)) {
+ funcPath.traverse({
noScope: true,
MemberExpression (path) {
if (path.node.object.name === '$event' && path.node.property.name ===