Skip to content

Commit

Permalink
feat(transformer): 支持在快应用循环中使用匿名函数,#3495
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Jun 24, 2019
1 parent 3deb1a7 commit 0a66dbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/taro-transformer-wx/src/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export function setJSXAttr (
value?: t.StringLiteral | t.JSXExpressionContainer | t.JSXElement,
path?: NodePath<t.JSXElement>
) {
if ((name === Adapter.forIndex || name === Adapter.forItem) && Adapter.type === Adapters.quickapp) {
return
}
const element = jsx.openingElement
if (!t.isJSXIdentifier(element.name)) {
return
Expand Down
24 changes: 21 additions & 3 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0a66dbd

Please sign in to comment.