Skip to content

Commit

Permalink
fix(transformer): render 函数顺序靠前导致找不到当前类其它的 JSX 函数
Browse files Browse the repository at this point in the history
close #3966
  • Loading branch information
yuche committed Jul 26, 2019
1 parent affe845 commit cd84e67
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/taro-transformer-wx/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Transformer {
components: [],
componentProperies: []
}
private methods: ClassMethodsMap = new Map()
private methods: ClassMethodsMap
private renderJSX: Map<string, NodePath<t.ClassMethod>> = new Map()
private refIdMap: Map<NodePath<t.ClassMethod>, Set<t.Identifier>> = new Map()
private initState: Set<string> = new Set()
Expand All @@ -128,13 +128,15 @@ class Transformer {
path: NodePath<t.ClassDeclaration>,
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()
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
24 changes: 21 additions & 3 deletions packages/taro-transformer-wx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export default function transform (options: Options): TransformResult {
const componentSourceMap = new Map<string, string[]>()
const imageSource = new Set<string>()
const importSources = new Set<string>()
const classMethods = new Map<string, NodePath<t.ClassMethod | t.ClassProperty>>()
let componentProperies: string[] = []
let mainClass!: NodePath<t.ClassDeclaration>
let storeName!: string
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit cd84e67

Please sign in to comment.