diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index 94e46f8..aa97d9d 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -76,7 +76,10 @@ import { COMMON_ATTRS, CUSTOM_BUILDER_PROPERTIES } from './component_map'; -import { componentCollection } from './validate_ui_syntax'; +import { + componentCollection, + builderParamObjectCollection +} from './validate_ui_syntax'; import { processCustomComponent } from './process_custom_component'; import { LogType, @@ -84,7 +87,6 @@ import { componentInfo, createFunction } from './utils'; -import { builderParamObjectCollection } from './process_component_member'; import { projectConfig } from '../main'; import { transformLog, contextGlobal } from './process_ui_syntax'; import { props } from './compile_info'; diff --git a/compiler/src/process_component_member.ts b/compiler/src/process_component_member.ts index 26bab48..d86f5e5 100644 --- a/compiler/src/process_component_member.ts +++ b/compiler/src/process_component_member.ts @@ -115,8 +115,6 @@ export const decoratorParamSet: Set = new Set(); export const stateObjectCollection: Set = new Set(); -export const builderParamObjectCollection: Map> = new Map(); - export class UpdateResult { private itemUpdate: boolean = false; private ctorUpdate: boolean = false; @@ -420,21 +418,12 @@ function createUpdateParams(name: ts.Identifier, decorator: string): ts.Statemen updateParamsNode = createUpdateParamsWithIf(name); break; case COMPONENT_PROP_DECORATOR: + case COMPONENT_BUILDERPARAM_DECORATOR: updateParamsNode = createUpdateParamsWithoutIf(name); break; case COMPONENT_OBJECT_LINK_DECORATOR: updateParamsNode = createUpdateParamsWithSet(name); break; - case COMPONENT_BUILDERPARAM_DECORATOR: - if (decorator === COMPONENT_BUILDERPARAM_DECORATOR) { - if (!builderParamObjectCollection.get(componentCollection.currentClassName)) { - builderParamObjectCollection.set(componentCollection.currentClassName, new Set([])); - } - builderParamObjectCollection.get(componentCollection.currentClassName) - .add(name.escapedText.toString()); - } - updateParamsNode = createUpdateParamsWithoutIf(name); - break; } return updateParamsNode; } diff --git a/compiler/src/process_custom_component.ts b/compiler/src/process_custom_component.ts index 0c78a2f..039810e 100644 --- a/compiler/src/process_custom_component.ts +++ b/compiler/src/process_custom_component.ts @@ -43,7 +43,8 @@ import { provideCollection, consumeCollection, objectLinkCollection, - isStaticViewCollection + isStaticViewCollection, + builderParamObjectCollection } from './validate_ui_syntax'; import { propAndLinkDecorators, @@ -52,7 +53,6 @@ import { createViewCreate, createCustomComponentNewExpression } from './process_component_member'; -import { builderParamObjectCollection } from './process_component_member'; import { LogType, LogInfo, diff --git a/compiler/src/process_import.ts b/compiler/src/process_import.ts index 8d236e9..ded48df 100644 --- a/compiler/src/process_import.ts +++ b/compiler/src/process_import.ts @@ -41,7 +41,8 @@ import { observedClassCollection, enumCollection, getComponentSet, - IComponentSet + IComponentSet, + builderParamObjectCollection } from './validate_ui_syntax'; import { LogInfo, LogType } from './utils'; import { projectConfig } from '../main'; @@ -150,7 +151,8 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa setDependencies(defaultNameFromParent, linkCollection.get(node.expression.escapedText.toString()), propertyCollection.get(node.expression.escapedText.toString()), - propCollection.get(node.expression.escapedText.toString())); + propCollection.get(node.expression.escapedText.toString()), + builderParamObjectCollection.get(node.expression.escapedText.toString())); } addDefaultExport(node); } @@ -177,7 +179,8 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa } setDependencies(asExportName, linkCollection.get(asExportPropertyName), propertyCollection.get(asExportPropertyName), - propCollection.get(asExportPropertyName)); + propCollection.get(asExportPropertyName), + builderParamObjectCollection.get(asExportPropertyName)); } asExportCollection.set(item.propertyName.escapedText.toString(), item.name.escapedText.toString()); } @@ -198,7 +201,7 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa ts.isIdentifier(item.name) && asNameFromParent.has(item.name.escapedText.toString())) { asNameFromParent.set(item.propertyName.escapedText.toString(), asNameFromParent.get(item.name.escapedText.toString())); - defaultCollection.add(item.name.escapedText.toString()); + defaultCollection.add(item.name.escapedText.toString()); } }); } @@ -267,12 +270,13 @@ function addDependencies(node: ts.ClassDeclaration, defaultNameFromParent: strin node.modifiers[1] && node.modifiers[0].kind === ts.SyntaxKind.ExportKeyword && node.modifiers[1].kind === ts.SyntaxKind.DefaultKeyword) { setDependencies(defaultNameFromParent, ComponentSet.links, ComponentSet.properties, - ComponentSet.props); + ComponentSet.props, ComponentSet.builderParams); } else if (asNameFromParent.has(componentName)) { setDependencies(asNameFromParent.get(componentName), ComponentSet.links, ComponentSet.properties, - ComponentSet.props); + ComponentSet.props, ComponentSet.builderParams); } else { - setDependencies(componentName, ComponentSet.links, ComponentSet.properties, ComponentSet.props); + setDependencies(componentName, ComponentSet.links, ComponentSet.properties, ComponentSet.props, + ComponentSet.builderParams); } } @@ -288,18 +292,24 @@ function addDefaultExport(node: ts.ClassDeclaration | ts.ExportAssignment): void setDependencies(CUSTOM_COMPONENT_DEFAULT, linkCollection.has(CUSTOM_COMPONENT_DEFAULT) ? new Set([...linkCollection.get(CUSTOM_COMPONENT_DEFAULT), ...linkCollection.get(name)]) : - linkCollection.get(name), propertyCollection.has(CUSTOM_COMPONENT_DEFAULT) ? - new Set([...propertyCollection.get(CUSTOM_COMPONENT_DEFAULT), ...propertyCollection.get(name)]) : - propertyCollection.get(name), propCollection.has(CUSTOM_COMPONENT_DEFAULT) ? + linkCollection.get(name), + propertyCollection.has(CUSTOM_COMPONENT_DEFAULT) ? + new Set([...propertyCollection.get(CUSTOM_COMPONENT_DEFAULT), + ...propertyCollection.get(name)]) : propertyCollection.get(name), + propCollection.has(CUSTOM_COMPONENT_DEFAULT) ? new Set([...propCollection.get(CUSTOM_COMPONENT_DEFAULT), ...propCollection.get(name)]) : - propCollection.get(name)); + propCollection.get(name), + builderParamObjectCollection.has(CUSTOM_COMPONENT_DEFAULT) ? + new Set([...builderParamObjectCollection.get(CUSTOM_COMPONENT_DEFAULT), + ...builderParamObjectCollection.get(name)]) : builderParamObjectCollection.get(name)); } function setDependencies(component: string, linkArray: Set, propertyArray: Set, - propArray: Set): void { + propArray: Set, builderParamArray: Set): void { linkCollection.set(component, linkArray); propertyCollection.set(component, propertyArray); propCollection.set(component, propArray); + builderParamObjectCollection.set(component, builderParamArray); componentCollection.customComponents.add(component); } diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 6cf8848..1f40f8c 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -46,7 +46,8 @@ import { TTOGGLE_CHECKBOX, TOGGLE_SWITCH, COMPONENT_BUTTON, - COMPONENT_TOGGLE + COMPONENT_TOGGLE, + COMPONENT_BUILDERPARAM_DECORATOR } from './pre_define'; import { INNER_COMPONENT_NAMES, @@ -94,6 +95,7 @@ export interface IComponentSet { objectLinks: Set; localStorageLink: Map>; localStorageProp: Map>; + builderParams: Set; } export const componentCollection: ComponentCollection = { @@ -121,6 +123,7 @@ export const storageLinkCollection: Map> = new Map(); export const provideCollection: Map> = new Map(); export const consumeCollection: Map> = new Map(); export const objectLinkCollection: Map> = new Map(); +export const builderParamObjectCollection: Map> = new Map(); export const localStorageLinkCollection: Map>> = new Map(); export const localStoragePropCollection: Map>> = new Map(); @@ -683,6 +686,7 @@ function collectComponentProps(node: ts.StructDeclaration): void { objectLinkCollection.set(componentName, ComponentSet.objectLinks); localStorageLinkCollection.set(componentName, ComponentSet.localStorageLink); localStoragePropCollection.set(componentName, ComponentSet.localStorageProp); + builderParamObjectCollection.set(componentName, ComponentSet.builderParams); } export function getComponentSet(node: ts.StructDeclaration): IComponentSet { @@ -696,13 +700,14 @@ export function getComponentSet(node: ts.StructDeclaration): IComponentSet { const provides: Set = new Set(); const consumes: Set = new Set(); const objectLinks: Set = new Set(); + const builderParams: Set = new Set(); const localStorageLink: Map> = new Map(); const localStorageProp: Map> = new Map(); traversalComponentProps(node, properties, regulars, states, links, props, storageProps, - storageLinks, provides, consumes, objectLinks, localStorageLink, localStorageProp); + storageLinks, provides, consumes, objectLinks, localStorageLink, localStorageProp, builderParams); return { properties, regulars, states, links, props, storageProps, storageLinks, provides, - consumes, objectLinks, localStorageLink, localStorageProp + consumes, objectLinks, localStorageLink, localStorageProp, builderParams }; } @@ -710,7 +715,8 @@ function traversalComponentProps(node: ts.StructDeclaration, properties: Set, states: Set, links: Set, props: Set, storageProps: Set, storageLinks: Set, provides: Set, consumes: Set, objectLinks: Set, - localStorageLink: Map>, localStorageProp: Map>): void { + localStorageLink: Map>, localStorageProp: Map>, + builderParams: Set): void { let isStatic: boolean = true; if (node.members) { const currentMethodCollection: Set = new Set(); @@ -727,7 +733,8 @@ function traversalComponentProps(node: ts.StructDeclaration, properties: Set, links: Set, props: Set, storageProps: Set, storageLinks: Set, provides: Set, consumes: Set, objectLinks: Set, - localStorageLink: Map>, localStorageProp: Map>): void { + localStorageLink: Map>, localStorageProp: Map>, + builderParams: Set): void { switch (decorator) { case COMPONENT_STATE_DECORATOR: states.add(name); @@ -770,6 +778,9 @@ function collectionStates(node: ts.Decorator, decorator: string, name: string, case COMPONENT_OBJECT_LINK_DECORATOR: objectLinks.add(name); break; + case COMPONENT_BUILDERPARAM_DECORATOR: + builderParams.add(name); + break; case COMPONENT_LOCAL_STORAGE_LINK_DECORATOR : collectionlocalStorageParam(node, name, localStorageLink); break; diff --git a/compiler/test/pages/TestComponent.ets b/compiler/test/pages/TestComponent.ets index f958b30..49da1a9 100644 --- a/compiler/test/pages/TestComponent.ets +++ b/compiler/test/pages/TestComponent.ets @@ -23,4 +23,17 @@ export struct TestComponent { .fontSize(32) } } +} + +@Component +export struct CustomContainerExport { + header: string = ""; + @BuilderParam closer: () => void; + build() { + Column() { + Text(this.header) + .fontSize(50) + this.closer() + } + } } \ No newline at end of file diff --git a/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts b/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts index 85cbce8..edff8d5 100644 --- a/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts +++ b/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts @@ -14,6 +14,7 @@ */ exports.source = ` +import { CustomContainerExport } from './test/pages/TestComponent'; @Component struct CustomContainer { header: string = ""; @@ -60,6 +61,15 @@ struct CustomContainerUser { build() { Column() { + CustomContainerExport({ + header: this.text, + }){ + Column(){ + specificParam("111", "22") + }.onClick(()=>{ + this.text = "changeHeader" + }) + } Row(){ CustomContainer({ header: this.text, @@ -84,7 +94,10 @@ struct CustomContainerUser { } ` exports.expectResult = -`class CustomContainer extends View { +`"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const TestComponent_1 = require("./test/pages/TestComponent"); +class CustomContainer extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); this.header = ""; @@ -178,10 +191,38 @@ class CustomContainerUser extends View { } render() { Column.create(); - Row.create(); let earlierCreatedChild_2 = this.findChildById("2"); if (earlierCreatedChild_2 == undefined) { - View.create(new CustomContainer("2", this, { + View.create(new TestComponent_1.CustomContainerExport("2", this, { + header: this.text, + closer: () => { + Column.create(); + Column.onClick(() => { + this.text = "changeHeader"; + }); + specificParam("111", "22"); + Column.pop(); + } + })); + } + else { + earlierCreatedChild_2.updateWithValueParams({ + header: this.text, + closer: () => { + Column.create(); + Column.onClick(() => { + this.text = "changeHeader"; + }); + specificParam("111", "22"); + Column.pop(); + } + }); + View.create(earlierCreatedChild_2); + } + Row.create(); + let earlierCreatedChild_3 = this.findChildById("3"); + if (earlierCreatedChild_3 == undefined) { + View.create(new CustomContainer("3", this, { header: this.text, content: this.specificParam, callContent: this.callSpecificParam("callContent1", 'callContent2'), @@ -189,19 +230,19 @@ class CustomContainerUser extends View { })); } else { - earlierCreatedChild_2.updateWithValueParams({ + earlierCreatedChild_3.updateWithValueParams({ header: this.text, content: this.specificParam, callContent: this.callSpecificParam("callContent1", 'callContent2'), footer: "Footer" }); - View.create(earlierCreatedChild_2); + View.create(earlierCreatedChild_3); } Row.pop(); Row.create(); - let earlierCreatedChild_3 = this.findChildById("3"); - if (earlierCreatedChild_3 == undefined) { - View.create(new CustomContainer2("3", this, { + let earlierCreatedChild_4 = this.findChildById("4"); + if (earlierCreatedChild_4 == undefined) { + View.create(new CustomContainer2("4", this, { header: this.text, content: () => { Column.create(); @@ -214,7 +255,7 @@ class CustomContainerUser extends View { })); } else { - earlierCreatedChild_3.updateWithValueParams({ + earlierCreatedChild_4.updateWithValueParams({ header: this.text, content: () => { Column.create(); @@ -225,7 +266,7 @@ class CustomContainerUser extends View { Column.pop(); } }); - View.create(earlierCreatedChild_3); + View.create(earlierCreatedChild_4); } Row.pop(); Column.pop();