From ddf4cd781e6200b6b57447f10b3ccd4378b2a387 Mon Sep 17 00:00:00 2001 From: yuche Date: Tue, 30 Apr 2019 11:15:23 +0800 Subject: [PATCH] =?UTF-8?q?perf(transformer):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=20props=20=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-transformer-wx/src/class.ts | 33 +++++++++-------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/packages/taro-transformer-wx/src/class.ts b/packages/taro-transformer-wx/src/class.ts index 59ad4d91056c..a6982d76cfe0 100644 --- a/packages/taro-transformer-wx/src/class.ts +++ b/packages/taro-transformer-wx/src/class.ts @@ -304,19 +304,19 @@ class Transformer { } } }, - ClassMethod (path) { - const node = path.node + ClassMethod (classMethodPath) { + const node = classMethodPath.node if (t.isIdentifier(node.key)) { const methodName = node.key.name - self.methods.set(methodName, path) + self.methods.set(methodName, classMethodPath) if (methodName.startsWith('render')) { - if (!isContainJSXElement(path)) { - throw codeFrameError(path.node, '以 render 开头的类函数必须返回 JSX,否则会导致渲染失败。如果是为了渲染字符串,建议更名。') + if (!isContainJSXElement(classMethodPath)) { + throw codeFrameError(classMethodPath.node, '以 render 开头的类函数必须返回 JSX,否则会导致渲染失败。如果是为了渲染字符串,建议更名。') } hasRender = true - self.renderJSX.set(methodName, path) - self.refIdMap.set(path, new Set([])) - path.traverse({ + self.renderJSX.set(methodName, classMethodPath) + self.refIdMap.set(classMethodPath, new Set([])) + classMethodPath.traverse({ ReturnStatement (returnPath) { const arg = returnPath.node.argument const ifStem = returnPath.findParent(p => p.isIfStatement()) @@ -378,17 +378,17 @@ class Transformer { if (!t.isJSXIdentifier(attrName, { name: 'data' })) { return } - generateAnonymousState(callPath.scope, callPath, self.refIdMap.get(path)!) + generateAnonymousState(callPath.scope, callPath, self.refIdMap.get(classMethodPath)!) } } }) } if (methodName.startsWith('render')) { - self.renderJSX.set(methodName, path) - self.refIdMap.set(path, new Set([])) + self.renderJSX.set(methodName, classMethodPath) + self.refIdMap.set(classMethodPath, new Set([])) } if (methodName === 'constructor') { - path.traverse({ + classMethodPath.traverse({ AssignmentExpression (p) { if ( t.isMemberExpression(p.node.left) && @@ -740,17 +740,10 @@ class Transformer { buildPropsAnonymousFunc = (attr: NodePath, expr: t.CallExpression, isBind = false) => { const { code } = generate(expr) - let renderScope: Scope | null = null - this.renderJSX.forEach(m => { - const method = attr.findParent(a => a === m) - if (method && method.scope) { - renderScope = method.get('body').scope - } - }) const id = t.isMemberExpression(expr.callee) ? findFirstIdentifierFromMemberExpression(expr.callee) : null if ( code.startsWith('this.props') || - (id && renderScope && isDerivedFromProps(renderScope, id.name)) + (id && isDerivedFromProps(attr.scope, id.name)) ) { const methodName = findMethodName(expr) const uniqueMethodName = `${methodName}${String(isBind)}`