Skip to content

Commit

Permalink
feat(transformer): 支持多层 if 语句嵌套, close #1036
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Nov 23, 2018
1 parent 1a17207 commit 8931734
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,10 @@ export class RenderParser {
blockStatement.traverse(this.renameIfScopeVaribale(blockStatement))
}
if (t.isIdentifier(parentNode.left)) {
const name = parentNode.left.name
const bindingNode = this.renderScope.getOwnBinding(name)!.path.node
const block = this.templates.get(name) || buildBlockElement()
const assignmentName = parentNode.left.name
const bindingNode = this.renderScope.getOwnBinding(assignmentName)!.path.node
const block = this.templates.get(assignmentName) || buildBlockElement()
if (isEmptyDeclarator(bindingNode)) {
const ifStatement = parentPath.findParent(p => p.isIfStatement())
const blockStatement = parentPath.findParent(p =>
p.isBlockStatement()
)
Expand All @@ -572,7 +571,52 @@ export class RenderParser {
const parentIfStatement = ifStatement.findParent(p =>
p.isIfStatement()
) as NodePath<t.IfStatement>
if (parentIfStatement && parentIfStatement.get('alternate') === ifStatement) {
const assignments: t.AssignmentExpression[] = []
let isAssignedBefore = false
// @TODO: 重构这两种循环为通用模块

// 如果这个 JSX assigmnent 的作用域中有其他的 if block 曾经赋值过,它应该是 else-if
if (blockStatement && blockStatement.isBlockStatement()) {
for (const parentStatement of blockStatement.node.body) {
if (t.isIfStatement(parentStatement) && t.isBlockStatement(parentStatement.consequent)) {
const statements = parentStatement.consequent.body
for (const statement of statements) {
if (t.isExpressionStatement(statement) && t.isAssignmentExpression(statement.expression) && t.isIdentifier(statement.expression.left, { name: assignmentName })) {
isAssignedBefore = true
}
}
}
}
}

// 如果这个 JSX assigmnent 的的父级作用域中的 prev sibling 有相同的赋值,它应该是 else-if
if (parentIfStatement) {
const { consequent } = parentIfStatement.node
if (t.isBlockStatement(consequent)) {
const body = consequent.body
for (const parentStatement of body) {
if (t.isIfStatement(parentStatement) && t.isBlockStatement(parentStatement.consequent)) {
const statements = parentStatement.consequent.body
for (const statement of statements) {
if (t.isExpressionStatement(statement) && t.isAssignmentExpression(statement.expression) && t.isIdentifier(statement.expression.left, { name: assignmentName })) {
assignments.push(statement.expression)
}
}
}
}
}
}
if (
(
parentIfStatement &&
(
parentIfStatement.get('alternate') === ifStatement ||
assignments.findIndex(a => a === parentNode) > 0
)
)
||
isAssignedBefore
) {
setJSXAttr(
jsxElementPath.node,
Adapter.elseif,
Expand All @@ -588,7 +632,7 @@ export class RenderParser {
}
block.children.push(jsxElementPath.node)
// setTemplate(name, path, templates)
name && this.templates.set(name, block)
assignmentName && this.templates.set(assignmentName, block)
}
} else {
throw codeFrameError(
Expand Down

0 comments on commit 8931734

Please sign in to comment.