From 0a66dbdd6a3172e07bb2838890159322c8f6241a Mon Sep 17 00:00:00 2001 From: yuche Date: Mon, 24 Jun 2019 14:14:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(transformer):=20=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=9C=A8=E5=BF=AB=E5=BA=94=E7=94=A8=E5=BE=AA=E7=8E=AF=E4=B8=AD?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8C=BF=E5=90=8D=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?#3495?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-transformer-wx/src/jsx.ts | 3 +++ packages/taro-transformer-wx/src/render.ts | 24 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/taro-transformer-wx/src/jsx.ts b/packages/taro-transformer-wx/src/jsx.ts index 637de1dc0f88..8f858271490d 100644 --- a/packages/taro-transformer-wx/src/jsx.ts +++ b/packages/taro-transformer-wx/src/jsx.ts @@ -82,6 +82,9 @@ export function setJSXAttr ( value?: t.StringLiteral | t.JSXExpressionContainer | t.JSXElement, path?: NodePath ) { + if ((name === Adapter.forIndex || name === Adapter.forItem) && Adapter.type === Adapters.quickapp) { + return + } const element = jsx.openingElement if (!t.isJSXIdentifier(element.name)) { return diff --git a/packages/taro-transformer-wx/src/render.ts b/packages/taro-transformer-wx/src/render.ts index a6d0fde46eb7..cfa21ab9927b 100644 --- a/packages/taro-transformer-wx/src/render.ts +++ b/packages/taro-transformer-wx/src/render.ts @@ -573,7 +573,7 @@ export class RenderParser { } else { forExpr = `(${indexName}, ${itemName}) in ${code}` } - setJSXAttr(jsxElementPath.node, Adapter.for, t.stringLiteral(forExpr)) + setJSXAttr(jsxElementPath.node, Adapter.for, t.stringLiteral(`{{${forExpr}}}`)) } // if (itemName && !indexName) { // const forExpr = gene @@ -1718,9 +1718,13 @@ export class RenderParser { }) const [ func ] = callee.node.arguments let indexId: t.Identifier | null = null + let itemId: t.Identifier | null = null if (t.isFunctionExpression(func) || t.isArrowFunctionExpression(func)) { const params = func.params as t.Identifier[] - indexId = params[1] + if (Array.isArray(params)) { + indexId = params[1] + itemId = params[0] + } } if (this.loopRefs.has(component.node) || loopRefComponent!) { hasLoopRef = true @@ -2052,7 +2056,21 @@ export class RenderParser { // setJSXAttr(returned, Adapter.for, t.identifier(stateName)) this.addRefIdentifier(callee, t.identifier(stateName)) // this.referencedIdentifiers.add(t.identifier(stateName)) - setJSXAttr(component.node, Adapter.for, t.jSXExpressionContainer(t.identifier(stateName))) + if (Adapters.quickapp === Adapter.type) { + let itemName = indexId!.name + let indexName = indexId!.name + if (itemName || indexName) { + let forExpr: string + if (itemName && !indexName) { + forExpr = `${itemName} in ${stateName}` + } else { + forExpr = `(${indexName}, ${itemName}) in ${stateName}` + } + setJSXAttr(component.node, Adapter.for, t.stringLiteral(`{{${forExpr}}}`)) + } + } else { + setJSXAttr(component.node, Adapter.for, t.jSXExpressionContainer(t.identifier(stateName))) + } const returnBody = this.renderPath.node.body.body const ifStem = callee.findParent(p => p.isIfStatement()) // @TEST