diff --git a/packages/taro-transformer-wx/src/class.ts b/packages/taro-transformer-wx/src/class.ts index e1b4d4792b4c..a871bff4788d 100644 --- a/packages/taro-transformer-wx/src/class.ts +++ b/packages/taro-transformer-wx/src/class.ts @@ -106,7 +106,7 @@ class Transformer { components: [], componentProperies: [] } - private methods: ClassMethodsMap = new Map() + private methods: ClassMethodsMap private renderJSX: Map> = new Map() private refIdMap: Map, Set> = new Map() private initState: Set = new Set() @@ -128,13 +128,15 @@ class Transformer { path: NodePath, sourcePath: string, componentProperies: string[], - sourceDir: string + sourceDir: string, + methods: ClassMethodsMap ) { this.classPath = path this.sourcePath = sourcePath this.sourceDir = sourceDir this.moduleNames = Object.keys(path.scope.getAllBindings('module')) this.componentProperies = new Set(componentProperies) + this.methods = methods this.compile() } @@ -924,7 +926,7 @@ class Transformer { ])) this.classPath.node.body.body = this.classPath.node.body.body.concat(method) } else if (t.isMemberExpression(expr) && !t.isThisExpression(expr.object)) { - // @TODO: 新旧 props 系统在事件处理上耦合太深,快应用应用新 props 把旧 props 系统逻辑全部清除 + // @TODO: 新旧 props 系统在事件处理上耦合太深,快应用应用新 props 把旧 props 系统逻辑全部清楚 this.buildAnonyMousFunc(path, attr, expr) } } diff --git a/packages/taro-transformer-wx/src/index.ts b/packages/taro-transformer-wx/src/index.ts index a6e912fe9fa1..732331b37aad 100644 --- a/packages/taro-transformer-wx/src/index.ts +++ b/packages/taro-transformer-wx/src/index.ts @@ -244,6 +244,7 @@ export default function transform (options: Options): TransformResult { const componentSourceMap = new Map() const imageSource = new Set() const importSources = new Set() + const classMethods = new Map>() let componentProperies: string[] = [] let mainClass!: NodePath let storeName!: string @@ -330,8 +331,11 @@ export default function transform (options: Options): TransformResult { mainClass = path as any }, ClassMethod (path) { - if (t.isIdentifier(path.node.key) && path.node.key.name === 'render') { - renderMethod = path + if (t.isIdentifier(path.node.key)) { + if (path.node.key.name === 'render') { + renderMethod = path + } + classMethods.set(path.node.key.name, path) } }, IfStatement (path) { @@ -613,6 +617,20 @@ export default function transform (options: Options): TransformResult { } }, ClassProperty (path) { + const { key: { name }, value } = path.node + if (t.isArrowFunctionExpression(value) || t.isFunctionExpression(value)) { + classMethods.set(name, path) + if (name.startsWith('render')) { + path.replaceWith(t.classMethod( + 'method', + t.identifier(name), + value.params, + t.isBlockStatement(value.body) ? value.body : t.blockStatement([ + t.returnStatement(value.body) + ]) + )) + } + } if (Adapter.type !== Adapters.quickapp) { return } @@ -798,7 +816,7 @@ export default function transform (options: Options): TransformResult { ) return { ast } as TransformResult } - result = new Transformer(mainClass, options.sourcePath, componentProperies, options.sourceDir!).result + result = new Transformer(mainClass, options.sourcePath, componentProperies, options.sourceDir!, classMethods).result result.code = generate(ast).code result.ast = ast const lessThanSignReg = new RegExp(lessThanSignPlacehold, 'g')