Skip to content

Commit

Permalink
fix(transformer): 多层循环中使用箭头函数给事件传参错误,close #2551
Browse files Browse the repository at this point in the history
close #2514, close #2112
  • Loading branch information
yuche committed Apr 15, 2019
1 parent 9dbc512 commit f880c82
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 286 deletions.
44 changes: 39 additions & 5 deletions packages/taro-transformer-wx/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class Transformer {
ReturnStatement (returnPath) {
const arg = returnPath.node.argument
const ifStem = returnPath.findParent(p => p.isIfStatement())
// tslint:disable-next-line: strict-type-predicates
if (ifStem && ifStem.isIfStatement() && arg === null) {
const consequent = ifStem.get('consequent')
if (consequent.isBlockStatement() && consequent.node.body.includes(returnPath.node)) {
Expand Down Expand Up @@ -441,14 +442,47 @@ class Transformer {
index = t.identifier('__index' + counter)
safeSet(loopCallExpr, 'node.arguments[0].params[1]', index)
}
classBody.push(t.classProperty(t.identifier(anonymousFuncName + 'Array'), t.arrayExpression([])))
classBody.push(t.classProperty(t.identifier(anonymousFuncName + 'Map'), t.objectExpression([])))
const indexKey = stemParent.scope.generateUid('indexKey')
// tslint:disable-next-line: no-inner-declarations
function findParentLoopCallExprIndices (callExpr: NodePath<t.CallExpression>) {
const indices: Set<t.Identifier> = new Set([])
// tslint:disable-next-line: no-conditional-assignment
while (callExpr = callExpr.findParent(p => isArrayMapCallExpression(p) && p !== callExpr) as NodePath<t.CallExpression>) {
let index = safeGet(callExpr, 'node.arguments[0].params[1]')
if (!t.isIdentifier(index)) {
index = t.identifier('__index' + counter)
safeSet(callExpr, 'node.arguments[0].params[1]', index)
}
indices.add(index)
}
return indices
}
const indices = [...findParentLoopCallExprIndices(loopCallExpr)].reverse()
const indexKeyDecl = t.variableDeclaration('const', [t.variableDeclarator(
t.identifier(indexKey),
indices.length === 0
? t.binaryExpression('+', t.stringLiteral(createRandomLetters(5)), index)
: t.templateLiteral(
[
t.templateElement({ raw: createRandomLetters(5) }),
...indices.map(() => t.templateElement({ raw: '-' })),
t.templateElement({ raw: '' })
],
[
...indices.map(i => t.identifier(i.name)),
index
]
)
)])
stemParent.insertBefore(indexKeyDecl)
const arrayFunc = t.memberExpression(
t.memberExpression(t.thisExpression(), t.identifier(anonymousFuncName + 'Array')),
t.identifier(index.name),
t.memberExpression(t.thisExpression(), t.identifier(anonymousFuncName + 'Map')),
t.identifier(indexKey),
true
)
classBody.push(
t.classMethod('method', t.identifier(anonymousFuncName), [t.identifier(index.name), t.identifier('e')], t.blockStatement([
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')])))
]))
Expand All @@ -458,7 +492,7 @@ class Transformer {
t.memberExpression(t.thisExpression(), t.identifier(anonymousFuncName)),
t.identifier('bind')
),
[t.thisExpression(), t.identifier(index.name)]
[t.thisExpression(), t.identifier(indexKey)]
))
stemParent.insertBefore(
t.expressionStatement(t.assignmentExpression(
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-transformer-wx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ export default function transform (options: Options): TransformResult {
if (index !== cases.length - 1 && t.isNullLiteral(Case.test)) {
throw codeFrameError(Case, '含有 JSX 的 switch case 语句只有最后一个 case 才能是 default')
}
// tslint:disable-next-line: strict-type-predicates
const test = Case.test === null ? t.nullLiteral() : t.binaryExpression('===', discriminant, Case.test)
return { block, test }
}).reduceRight((ifStatement, item) => {
Expand Down Expand Up @@ -463,6 +464,7 @@ export default function transform (options: Options): TransformResult {
}
}

// tslint:disable-next-line: strict-type-predicates
if (!t.isJSXIdentifier(name) || value === null || t.isStringLiteral(value) || t.isJSXElement(value)) {
return
}
Expand Down
274 changes: 0 additions & 274 deletions packages/taro-transformer-wx/src/loop-component.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
ALIPAY_BUBBLE_EVENTS
} from './constant'
import { Adapter, Adapters } from './adapter'
import { transformOptions, buildBabelTransformOptions } from './options'
import { buildBabelTransformOptions } from './options'
import generate from 'babel-generator'
import { LoopRef } from './interface'
const template = require('babel-template')
Expand Down Expand Up @@ -918,6 +918,7 @@ export class RenderParser {
&& name.name.indexOf('render') !== 0
&& !t.isJSXElement(value)
) {
// tslint:disable-next-line: strict-type-predicates
const v: t.StringLiteral | t.Expression | t.BooleanLiteral = value === null
? t.booleanLiteral(true)
: (t.isJSXExpressionContainer(value)
Expand Down
Loading

0 comments on commit f880c82

Please sign in to comment.