fix builder bug and builderParam

Signed-off-by: puyajun <puyajun@huawei.com>
Change-Id: I38b3829020cb9d8b26e736f038fda59e3f31e513
This commit is contained in:
puyajun
2022-01-20 14:16:49 +08:00
parent 801d86edad
commit 9cd12f20cb
5 changed files with 91 additions and 98 deletions
+35 -21
View File
@@ -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,
log: LogInfo[]): ts.block {
// @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.express.arguments;
if (arguments && arguments.length < 1) {
argumentsArray = [ts.factory.createObjectLiteralExpression([newPropertyAssignment], true)]
} else {
function processExpressionStatementChange(node: ts.ExpressionStatement, nextNode: ts.Block,
log: LogInfo[]): ts.ExpressionStatement {
// @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.express.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[],
+18 -6
View File
@@ -210,11 +210,9 @@ function processBuildMember(node: ts.MethodDeclaration, context: ts.Transformati
});
}
const buildNode: ts.MethodDeclaration = processComponentBuild(node, log);
return ts.visitNode(buildNode, visitBuild);
const firstParseBuildNode = ts.visitNode(buildNode, visitBuild);
return ts.visitNode(firstParseBuildNode, visitBuildSecond);
function visitBuild(node: ts.Node): ts.Node {
if (isCustomComponentNode(node)) {
return node;
}
if (isGeometryView(node)) {
node = processGeometryView(node as ts.ExpressionStatement, log);
}
@@ -229,11 +227,17 @@ function processBuildMember(node: ts.MethodDeclaration, context: ts.Transformati
ts.factory.createIdentifier(FOREACH_OBSERVED_OBJECT),
ts.factory.createIdentifier(FOREACH_GET_RAW_OBJECT)), undefined, [node]);
}
return ts.visitEachChild(node, visitBuild, context);
}
function visitBuildSecond(node: ts.Node): ts.Node {
if (isCustomComponentNode(node) || isCustomBuilderNode(node)) {
return node;
}
if ((ts.isIdentifier(node) || ts.isPropertyAccessExpression(node)) &&
validateBuilderFunctionNode(node)) {
return getParsedBuilderAttrArgument(node);
}
return ts.visitEachChild(node, visitBuild, context);
return ts.visitEachChild(node, visitBuildSecond, context);
}
}
@@ -298,11 +302,19 @@ function isCustomComponentNode(node:ts.NewExpression | ts.ExpressionStatement):
node.expression.expression.expression.escapedText.toString().startsWith(
CUSTOM_COMPONENT_EARLIER_CREATE_CHILD))) {
return true;
}else {
} else {
return false;
}
}
function isCustomBuilderNode(node: ts.ExpressionStatement): boolean {
return ts.isExpressionStatement(node) && node.expression &&
// @ts-ignore
node.expression.expression && node.expression.expression.escapedText &&
// @ts-ignore
CUSTOM_BUILDER_METHOD.has(node.expression.expression.escapedText.toString());
}
function isGeometryView(node: ts.Node): boolean {
if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression)) {
const call: ts.CallExpression = node.expression;
+2 -2
View File
@@ -419,7 +419,7 @@ function createUpdateParams(name: ts.Identifier, decorator: string): ts.Statemen
builderParamObjectCollection.get(componentCollection.currentClassName)
.add(name.escapedText.toString())
}
updateParamsNode = createUpdateParamsWithoutIf(name, 1);
updateParamsNode = createUpdateParamsWithoutIf(name, true);
break;
}
return updateParamsNode;
@@ -435,7 +435,7 @@ function createUpdateParamsWithIf(name: ts.Identifier): ts.IfStatement {
createUpdateParamsWithoutIf(name)], true), undefined);
}
function createUpdateParamsWithoutIf(name: ts.Identifier, isAdd: number = 0): ts.ExpressionStatement {
function createUpdateParamsWithoutIf(name: ts.Identifier, isAdd: boolean = false): ts.ExpressionStatement {
let textName: string;
if (isAdd) {
textName = `__${name.getText()}`;
+13 -14
View File
@@ -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, {}));
`
+23 -55
View File
@@ -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, {}));
`