From 46604f5003bdf0afe2cede73f69a44a4c16638d0 Mon Sep 17 00:00:00 2001 From: yuche Date: Tue, 25 Jun 2019 16:33:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(transformer):=20=E5=BE=AA=E7=8E=AF=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=86=99=20return=20=E7=94=9F=E6=88=90=E5=8C=BF?= =?UTF-8?q?=E5=90=8D=E5=87=BD=E6=95=B0=E9=94=99=E8=AF=AF=EF=BC=8Cclose=20#?= =?UTF-8?q?3536?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-transformer-wx/src/class.ts | 63 ++++++++++++++--------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/packages/taro-transformer-wx/src/class.ts b/packages/taro-transformer-wx/src/class.ts index 24c5cdbff3d3..e735ea34f486 100644 --- a/packages/taro-transformer-wx/src/class.ts +++ b/packages/taro-transformer-wx/src/class.ts @@ -270,32 +270,45 @@ class Transformer { ] ) )]) - stemParent.insertBefore(indexKeyDecl) - const arrayFunc = t.memberExpression( - t.memberExpression(t.thisExpression(), t.identifier(anonymousFuncName + 'Map')), - t.identifier(indexKey), - true - ) - classBody.push( - t.classMethod('method', t.identifier(anonymousFuncName), [t.identifier(indexKey), t.identifier('e')], t.blockStatement([ - isCatch ? t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('e'), t.identifier('stopPropagation')), [])) : t.emptyStatement(), - t.returnStatement(t.logicalExpression('&&', arrayFunc, t.callExpression(arrayFunc, [t.identifier('e')]))) - ])) - ) - exprPath.replaceWith(t.callExpression( - t.memberExpression( - t.memberExpression(t.thisExpression(), t.identifier(anonymousFuncName)), - t.identifier('bind') - ), - [t.thisExpression(), t.identifier(indexKey)] - )) - stemParent.insertBefore( - t.expressionStatement(t.assignmentExpression( - '=', - arrayFunc, - expr + + const func = loopCallExpr.node.arguments[0] + if (t.isArrowFunctionExpression(func)) { + if (!t.isBlockStatement(func.body)) { + func.body = t.blockStatement([ + indexKeyDecl, + t.returnStatement(func.body) + ]) + } else { + func.body.body.push(indexKeyDecl) + } + const arrayFunc = t.memberExpression( + t.memberExpression(t.thisExpression(), t.identifier(anonymousFuncName + 'Map')), + t.identifier(indexKey), + true + ) + classBody.push( + t.classMethod('method', t.identifier(anonymousFuncName), [t.identifier(indexKey), t.identifier('e')], t.blockStatement([ + isCatch ? t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('e'), t.identifier('stopPropagation')), [])) : t.emptyStatement(), + t.returnStatement(t.logicalExpression('&&', arrayFunc, t.callExpression(arrayFunc, [t.identifier('e')]))) + ])) + ) + exprPath.replaceWith(t.callExpression( + t.memberExpression( + t.memberExpression(t.thisExpression(), t.identifier(anonymousFuncName)), + t.identifier('bind') + ), + [t.thisExpression(), t.identifier(indexKey)] )) - ) + func.body.body.push( + t.expressionStatement(t.assignmentExpression( + '=', + arrayFunc, + expr + )) + ) + } else { + throw codeFrameError(func, '返回 JSX 的循环语句必须使用箭头函数') + } } else { classBody.push( t.classMethod('method', t.identifier(anonymousFuncName), [t.identifier('e')], t.blockStatement([