Skip to content

Commit

Permalink
fix(transformer): JSX props 的匿名函数变量不需要重命名
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Apr 4, 2019
1 parent 0a2f8d8 commit 8056981
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/taro-transformer-wx/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
incrementId,
isContainStopPropagation
} from './utils'
import { DEFAULT_Component_SET } from './constant'
import { DEFAULT_Component_SET, ANONYMOUS_FUNC } from './constant'
import { kebabCase, uniqueId, get as safeGet, set as safeSet } from 'lodash'
import { RenderParser } from './render'
import { findJSXAttrByName } from './jsx'
Expand Down Expand Up @@ -400,7 +400,7 @@ class Transformer {
const exprPath = attr.get('value.expression')
const stemParent = path.getStatementParent()
const counter = self.anonymousFuncCounter()
const anonymousFuncName = `anonymousFunc${counter}`
const anonymousFuncName = `${ANONYMOUS_FUNC}${counter}`
const isCatch = isContainStopPropagation(exprPath)
const classBody = self.classPath.node.body.body
const loopCallExpr = path.findParent(p => isArrayMapCallExpression(p)) as NodePath<t.CallExpression>
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-transformer-wx/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export const ALIPAY_BUBBLE_EVENTS = new Set<string>([
'onLongTap'
])

export const ANONYMOUS_FUNC = 'anonymousFunc'

export const TRANSFORM_COMPONENT_PROPS = new Map<Adapters, { [key: string]: { [key: string]: string } }>()

TRANSFORM_COMPONENT_PROPS.set(Adapters.alipay, {
Expand Down
12 changes: 6 additions & 6 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,24 +534,24 @@ export class RenderParser {

private renameIfScopeVaribale = (blockStatement: NodePath<t.BlockStatement>): Visitor => {
return {
VariableDeclarator: (p) => {
const { id, init } = p.node
const ifStem = p.parentPath.parentPath.parentPath
if (!ifStem.isIfStatement() || isContainJSXElement(p)) {
VariableDeclarator: (path) => {
const { id, init } = path.node
const ifStem = path.parentPath.parentPath.parentPath
if (!ifStem.isIfStatement() || isContainJSXElement(path)) {
return
}
if (t.isIdentifier(id)) {
if (id.name.startsWith('loopArray') || id.name.startsWith(LOOP_CALLEE)) {
this.renderPath.node.body.body.unshift(
t.variableDeclaration('let', [t.variableDeclarator(t.identifier(id.name))])
)
p.parentPath.replaceWith(
path.parentPath.replaceWith(
template('ID = INIT;')({ ID: t.identifier(id.name), INIT: init })
)
} else {
const newId = this.renderScope.generateDeclaredUidIdentifier('$' + id.name)
blockStatement.scope.rename(id.name, newId.name)
p.parentPath.replaceWith(
path.parentPath.replaceWith(
template('ID = INIT;')({ ID: newId, INIT: init || t.identifier('undefined') })
)
}
Expand Down
15 changes: 12 additions & 3 deletions packages/taro-transformer-wx/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,17 @@ export function generateAnonymousState (
)
if (blockStatement && blockStatement.isBlockStatement()) {
blockStatement.traverse({
VariableDeclarator: (p) => {
const { id, init } = p.node
VariableDeclarator: (path) => {
const { id, init } = path.node
const isArrowFunctionInJSX = path.findParent(p => p.isJSXAttribute() ||
(
p.isAssignmentExpression() && t.isMemberExpression(p.node.left) && t.isThisExpression(p.node.left.object)
&& t.isIdentifier(p.node.left.property) && p.node.left.property.name.startsWith('')
)
)
if (isArrowFunctionInJSX) {
return
}
if (t.isIdentifier(id) && !id.name.startsWith(LOOP_STATE)) {
const newId = scope.generateDeclaredUidIdentifier('$' + id.name)
refIds.forEach((refId) => {
Expand All @@ -204,7 +213,7 @@ export function generateAnonymousState (
variableName = newId.name
refIds.add(t.identifier(variableName))
blockStatement.scope.rename(id.name, newId.name)
p.parentPath.replaceWith(
path.parentPath.replaceWith(
template('ID = INIT;')({ ID: newId, INIT: init })
)
}
Expand Down

0 comments on commit 8056981

Please sign in to comment.