From 66424fdf117564b101b36003ff4438cb652c94d6 Mon Sep 17 00:00:00 2001 From: yuche Date: Thu, 6 Dec 2018 11:28:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(taroize):=20=E6=94=AF=E6=8C=81=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E7=94=A8=20this.foo=20=E8=A1=A8=E7=A4=BA=20class=20?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=B1=9E=E6=80=A7=20`foo`,=20close=20#1381?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-transformer-wx/src/render.ts | 40 +++++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/taro-transformer-wx/src/render.ts b/packages/taro-transformer-wx/src/render.ts index feed96169c0a..eb00c9ca207f 100644 --- a/packages/taro-transformer-wx/src/render.ts +++ b/packages/taro-transformer-wx/src/render.ts @@ -114,6 +114,7 @@ export class RenderParser { private loopCalleeId = new Set() private usedThisProperties = new Set() private incrementCalleeId = incrementId() + private classComputedState = new Set() private renderPath: NodePath private methods: ClassMethodsMap @@ -1394,12 +1395,22 @@ export class RenderParser { } setUsedState () { + for (const [ key, method ] of this.methods) { + if (method) { + if (method.isClassMethod()) { + const kind = method.node.kind + if (kind === 'get') { + this.classComputedState.add(key) + } + } + } + } Array.from(this.reserveStateWords).forEach(this.setReserveWord) const usedState = Array.from( new Set( Array.from(this.referencedIdentifiers) .map(i => i.name) - .concat([...this.initState, ...this.usedThisState, ...this.componentProperies]) + .concat([...this.initState, ...this.usedThisState, ...this.componentProperies, ...this.classComputedState]) ) ) .concat(...this.usedState) @@ -1425,7 +1436,7 @@ export class RenderParser { } setPendingState () { - let properties = Array.from( + const propertyKeys = Array.from( new Set(Array.from(this.referencedIdentifiers) .map(i => i.name)) ) @@ -1449,16 +1460,27 @@ export class RenderParser { .filter(i => i !== MAP_CALL_ITERATOR && !this.reserveStateWords.has(i)) .filter(i => !i.startsWith('$$')) .filter(i => !this.loopRefIdentifiers.has(i)) - .map(i => t.objectProperty(t.identifier(i), t.identifier(i))) + let properties = propertyKeys.map(i => t.objectProperty(t.identifier(i), t.identifier(i))) if (this.customComponentData.length > 0) { properties = properties.concat(this.customComponentData) } - const pendingState = t.objectExpression(properties.concat( - Adapter.type === Adapters.swan && transformOptions.isRoot ? t.objectProperty( - t.identifier('_triggerObserer'), - t.booleanLiteral(false) - ) : [] - )) + const pendingState = t.objectExpression( + properties.concat( + Adapter.type === Adapters.swan && transformOptions.isRoot ? t.objectProperty( + t.identifier('_triggerObserer'), + t.booleanLiteral(false) + ) : [] + ).concat( + Array.from(this.classComputedState).filter(i => { + return !propertyKeys.includes(i) + }).map(i => { + return t.objectProperty( + t.identifier(i), + t.memberExpression(t.thisExpression(), t.identifier(i)) + ) + }) + ) + ) this.renderPath.node.body.body = this.renderPath.node.body.body.concat( buildAssignState(pendingState), t.returnStatement(