From 806e8257b18b73003df8313bbd884ad799bb85f7 Mon Sep 17 00:00:00 2001 From: puyajun Date: Thu, 20 Jan 2022 13:20:03 +0800 Subject: [PATCH 1/3] puyajun@huawei.com fix custom builderParam bug Signed-off-by: puyajun Change-Id: I9df9e94e0648aa92dd7064da48268518e97f73a2 --- compiler/src/process_component_build.ts | 54 ++++++++++------ compiler/test/ut/builder/builderLambda.ts | 27 ++++---- compiler/test/ut/builder/builderParam.ts | 78 +++++++---------------- 3 files changed, 70 insertions(+), 89 deletions(-) diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index 766971ace..505000c0d 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -169,7 +169,7 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme break; case ComponentType.customComponent: if (index + 1 < array.length && ts.isBlock(array[index + 1])) { - item = processBlockChange(item, + item = processExpressionStatementChange(item, array[index + 1] as ts.Block, log) } processCustomComponent(item, newStatements, log); @@ -195,29 +195,43 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme } } -function processBlockChange(node: ts.ExpressionStatement, nextNode: ts.Block, +function processExpressionStatementChange(node: ts.ExpressionStatement, nextNode: ts.Block, log: LogInfo[]): ts.ExpressionStatement { - // @ts-ignore - const newBlock: ts.Block = processComponentBlock(nextNode, false, log); - const arrowNode: ts.ArrowFunction = ts.factory.createArrowFunction(undefined, undefined, - [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), newBlock); - const newPropertyAssignment:ts.PropertyAssignment = ts.factory.createPropertyAssignment( - ts.factory.createIdentifier(CHILD), arrowNode); - // @ts-ignore - let argumentsArray: ts.ObjectLiteralExpression[] = node.expression.arguments; - if (argumentsArray && argumentsArray.length < 1) { - argumentsArray = [ts.factory.createObjectLiteralExpression([newPropertyAssignment], true)] - } else { // @ts-ignore - argumentsArray = [ts.factory.createObjectLiteralExpression( + let name = node.expression.expression.escapedText.toString() + let childParam: string; + if (builderParamObjectCollection.get(name) && builderParamObjectCollection.get(name).size > 0) { + builderParamObjectCollection.get(name).forEach((item) => { + childParam = item + }) + // @ts-ignore + const newBlock: ts.Block = processComponentBlock(nextNode, false, log); + const arrowNode: ts.ArrowFunction = ts.factory.createArrowFunction(undefined, undefined, + [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), newBlock); + const newPropertyAssignment:ts.PropertyAssignment = ts.factory.createPropertyAssignment( + ts.factory.createIdentifier(childParam), arrowNode); + // @ts-ignore + let argumentsArray: ts.ObjectLiteralExpression[] = node.expression.arguments; + if (argumentsArray && argumentsArray.length < 1) { + argumentsArray = [ts.factory.createObjectLiteralExpression([newPropertyAssignment], true)] + } else { // @ts-ignore - node.expression.arguments[0].properties.concat([newPropertyAssignment]), true)] - } - // @ts-ignore - node = ts.factory.updateExpressionStatement(node, ts.factory.updateCallExpression(node.expression, + argumentsArray = [ts.factory.createObjectLiteralExpression( + // @ts-ignore + node.expression.arguments[0].properties.concat([newPropertyAssignment]), true)] + } // @ts-ignore - node.expression.expression, node.expression.expression.typeArguments, argumentsArray)) - return node; + node = ts.factory.updateExpressionStatement(node, ts.factory.updateCallExpression(node.expression, + // @ts-ignore + node.expression.expression, node.expression.expression.typeArguments, argumentsArray)) + return node; + } else { + log.push({ + type: LogType.ERROR, + message: `The attribute in '${name}' should be decorated with '@BuilderParam' to receive .`, + pos: node.getStart() + }); + } } function processInnerComponent(node: ts.ExpressionStatement, index: number, arr: ts.Statement[], diff --git a/compiler/test/ut/builder/builderLambda.ts b/compiler/test/ut/builder/builderLambda.ts index c19735078..0bfa5adac 100644 --- a/compiler/test/ut/builder/builderLambda.ts +++ b/compiler/test/ut/builder/builderLambda.ts @@ -57,8 +57,8 @@ exports.expectResult = `class CustomContainer extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); - this.header = "" - this.footer = "" + this.header = ""; + this.footer = ""; this.updateWithValueParams(params); } updateWithValueParams(params) { @@ -74,17 +74,16 @@ exports.expectResult = this.__child.aboutToBeDeleted(); SubscriberManager.Get().delete(this.id()); } - get child(){ + get child() { return this.__child.get(); } set child(newValue) { - this.__child.set(newValue) + this.__child.set(newValue); } render() { Column.create(); Text.create(this.header); Text.pop(); - this.child(); Text.create(this.footer); Text.pop(); Column.pop(); @@ -94,11 +93,11 @@ function specificParam(label1, label2) { Column.create(); Text.create(label1); Text.pop(); - Text.create(label1); + Text.create(label2); Text.pop(); Column.pop(); } -class CustomContainerUser { +class CustomContainerUser extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); this.updateWithValueParams(params); @@ -117,12 +116,12 @@ class CustomContainerUser { child: () => { Column.create(); Text.create("content1"); - Text.width(50) + Text.width(50); Text.pop(); Text.create("content2"); Text.pop(); Column.pop(); - specificParam("content3", "content4) + specificParam("content3", "content4"); } })); } @@ -132,18 +131,18 @@ class CustomContainerUser { child: () => { Column.create(); Text.create("content1"); - Text.width(50) + Text.width(50); Text.pop(); Text.create("content2"); Text.pop(); Column.pop(); - specificParam("content3", "content4) - }} + specificParam("content3", "content4"); + } }); View.create(earlierCreatedChild_2); } - Column.pop(); + Column.pop(); } } -loadDocument(new MyComponent("1", undefined, {})); +loadDocument(new CustomContainerUser("1", undefined, {})); ` diff --git a/compiler/test/ut/builder/builderParam.ts b/compiler/test/ut/builder/builderParam.ts index 49a3916e4..bf41f3dd5 100644 --- a/compiler/test/ut/builder/builderParam.ts +++ b/compiler/test/ut/builder/builderParam.ts @@ -18,31 +18,24 @@ exports.source = ` struct CustomContainer { header: string = ""; footer: string = ""; - @BuilderParam child: () => any; + @BuilderParam child1: () => any; build() { Column() { + this.child1() Text(this.header) - this.child() Text(this.footer) } } } -@Builder function specificParam(label1: string, label2: string) { - Column() { - Text(label1) - Text(label2) - } -} - @Entry @Component struct CustomContainerUser { @Builder specificChild() { Column() { Text("My content1") - Text("My content1") + Text("My content2") } } @@ -51,13 +44,9 @@ struct CustomContainerUser { CustomContainer({ header: "Header", footer: "Footer", - child: this.specificChild - }) - CustomContainer({ - header: "Header", - footer: "Footer", - child: specificParam("content3", "content4") - }) + }){ + this.specificChild() + } } } } @@ -66,8 +55,8 @@ exports.expectResult = `class CustomContainer extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); - this.header = "" - this.footer = "" + this.header = ""; + this.footer = ""; this.updateWithValueParams(params); } updateWithValueParams(params) { @@ -77,37 +66,28 @@ exports.expectResult = if (params.footer !== undefined) { this.footer = params.footer; } - this.__child = params.child; + this.__child1 = params.child1; } aboutToBeDeleted() { - this.__child.aboutToBeDeleted(); + this.__child1.aboutToBeDeleted(); SubscriberManager.Get().delete(this.id()); } - get child(){ - return this.__child.get(); + get child1() { + return this.__child1.get(); } - set child(newValue) { - this.__child.set(newValue) + set child1(newValue) { + this.__child1.set(newValue); } render() { Column.create(); Text.create(this.header); Text.pop(); - this.child(); Text.create(this.footer); Text.pop(); Column.pop(); } } -function specificParam(label1, label2) { - Column.create(); - Text.create(label1); - Text.pop(); - Text.create(label1); - Text.pop(); - Column.pop(); -} -class CustomContainerUser { +class CustomContainerUser extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); this.updateWithValueParams(params); @@ -132,35 +112,23 @@ class CustomContainerUser { View.create(new CustomContainer("2", this, { header: "Header", footer: "Footer", - child: this.specificChild + child1: () => { + this.specificChild(); + } })); } else { earlierCreatedChild_2.updateWithValueParams({ header: "Header", footer: "Footer", - child: this.specificChild + child1: () => { + this.specificChild(); + } }); View.create(earlierCreatedChild_2); } - let earlierCreatedChild_3 = this.findChildById("3"); - if (earlierCreatedChild_3 == undefined) { - View.create(new CustomContainer("3", this, { - header: "Header", - footer: "Footer", - child: specificParam("content3", "content4") - })); - } - else { - earlierCreatedChild_3.updateWithValueParams({ - header: "Header", - footer: "Footer", - child: specificParam("content3", "content4") - }); - View.create(earlierCreatedChild_3); - } - Column.pop(); + Column.pop(); } } -loadDocument(new MyComponent("1", undefined, {})); +loadDocument(new CustomContainerUser("1", undefined, {})); ` -- Gitee From c57b39b0bf48f7540ac0069e748e35e510160da9 Mon Sep 17 00:00:00 2001 From: puyajun Date: Thu, 20 Jan 2022 14:35:34 +0800 Subject: [PATCH 2/3] puyajun@huawei.com fix builder bug and builderParam Signed-off-by: puyajun Change-Id: If2e656e8ce5f618a96c4085c729f9c77a108d988 --- compiler/src/process_component_build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index 505000c0d..efabe5568 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -228,7 +228,7 @@ function processExpressionStatementChange(node: ts.ExpressionStatement, nextNode } else { log.push({ type: LogType.ERROR, - message: `The attribute in '${name}' should be decorated with '@BuilderParam' to receive .`, + message: `'${name}' should have a property decorated with @ builderparamThe .`, pos: node.getStart() }); } -- Gitee From 6cfd5867082890a922dbb70d0bab039d917a520e Mon Sep 17 00:00:00 2001 From: puyajun Date: Thu, 20 Jan 2022 15:18:35 +0800 Subject: [PATCH 3/3] puyajun@huawei.com fix bug builder and builderParam Signed-off-by: puyajun Change-Id: I7e74a1350bc92b3d250236e3deeaa33625a52b22 --- compiler/src/process_component_build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index efabe5568..59d7756d7 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -228,7 +228,7 @@ function processExpressionStatementChange(node: ts.ExpressionStatement, nextNode } else { log.push({ type: LogType.ERROR, - message: `'${name}' should have a property decorated with @ builderparamThe .`, + message: `'${name}' should have a property decorated with @ builderparam .`, pos: node.getStart() }); } -- Gitee