diff --git a/packages/taro-transformer-wx/src/class.ts b/packages/taro-transformer-wx/src/class.ts index 7e82839f071b..1e3f25f63f9e 100644 --- a/packages/taro-transformer-wx/src/class.ts +++ b/packages/taro-transformer-wx/src/class.ts @@ -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' @@ -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 diff --git a/packages/taro-transformer-wx/src/constant.ts b/packages/taro-transformer-wx/src/constant.ts index b81e3e1f58b5..99c7e4ae8593 100644 --- a/packages/taro-transformer-wx/src/constant.ts +++ b/packages/taro-transformer-wx/src/constant.ts @@ -107,6 +107,8 @@ export const ALIPAY_BUBBLE_EVENTS = new Set([ 'onLongTap' ]) +export const ANONYMOUS_FUNC = 'anonymousFunc' + export const TRANSFORM_COMPONENT_PROPS = new Map() TRANSFORM_COMPONENT_PROPS.set(Adapters.alipay, { diff --git a/packages/taro-transformer-wx/src/render.ts b/packages/taro-transformer-wx/src/render.ts index 2314de237b8f..eddac4bfbc88 100644 --- a/packages/taro-transformer-wx/src/render.ts +++ b/packages/taro-transformer-wx/src/render.ts @@ -534,10 +534,10 @@ export class RenderParser { private renameIfScopeVaribale = (blockStatement: NodePath): 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)) { @@ -545,13 +545,13 @@ export class RenderParser { 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') }) ) } diff --git a/packages/taro-transformer-wx/src/utils.ts b/packages/taro-transformer-wx/src/utils.ts index 11c12b239f35..3b34926ef0e8 100644 --- a/packages/taro-transformer-wx/src/utils.ts +++ b/packages/taro-transformer-wx/src/utils.ts @@ -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) => { @@ -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 }) ) }