From 9270c580577a27ef4701aca38bbe8aded9d21240 Mon Sep 17 00:00:00 2001 From: Yenan Date: Tue, 10 Jun 2025 21:13:43 +0800 Subject: [PATCH] =?UTF-8?q?Consume=E8=A3=85=E9=A5=B0=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yenan --- compiler/src/process_component_member.ts | 15 +- .../@consume_with_defaultValue.js.sample | 201 ++++++++++++++++++ .../@consume_with_defaultValue.ets | 58 +++++ .../validateForbiddenSpecifyDefaultValue.ets | 54 +++++ .../test/transform_ut/helpers/pathConfig.ts | 2 + compiler/test/transform_ut_error.json | 14 ++ 6 files changed, 340 insertions(+), 4 deletions(-) create mode 100644 compiler/test/transform_ut/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.js.sample create mode 100644 compiler/test/transform_ut/application/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.ets create mode 100644 compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_component_member/validateForbiddenSpecifyDefaultValue.ets diff --git a/compiler/src/process_component_member.ts b/compiler/src/process_component_member.ts index dc65a1c1c..c41bcda12 100644 --- a/compiler/src/process_component_member.ts +++ b/compiler/src/process_component_member.ts @@ -147,7 +147,7 @@ export const requireCanReleaseMandatoryDecorators: Set = ...observedPropertyDecorators]); export const forbiddenSpecifyDefaultValueDecorators: Set = - new Set([COMPONENT_LINK_DECORATOR, COMPONENT_CONSUME_DECORATOR, COMPONENT_OBJECT_LINK_DECORATOR]); + new Set([COMPONENT_LINK_DECORATOR, COMPONENT_OBJECT_LINK_DECORATOR]); export const mandatoryToInitViaParamDecorators: Set = new Set([...propAndLinkDecorators, COMPONENT_OBJECT_LINK_DECORATOR]); @@ -872,12 +872,19 @@ function updateConsumeProperty(node: ts.PropertyDeclaration, } else { propertyOrAliasName = name; } + // '4' means that the property has aliasName and '2' represents the argument node + const callExpressionArgs: readonly ts.Expression[] | undefined = + node.initializer ? [ + propertyAndStringKey.length === 0 ? ts.factory.createStringLiteral(propertyOrAliasName) : + propertyAndStringKey.length === 4 && propertyAndStringKey[2] as ts.Expression, ts.factory.createStringLiteral(name), + node.initializer] : + [ + propertyAndStringKey.length === 0 ? ts.factory.createStringLiteral(propertyOrAliasName) : + propertyAndStringKey.length === 4 && propertyAndStringKey[2] as ts.Expression, ts.factory.createStringLiteral(name)]; return ts.factory.createExpressionStatement(ts.factory.createBinaryExpression( createPropertyAccessExpressionWithThis(`__${name}`), ts.factory.createToken(ts.SyntaxKind.EqualsToken), ts.factory.createCallExpression( - createPropertyAccessExpressionWithThis(INITIALIZE_CONSUME_FUNCTION), undefined, [ - propertyAndStringKey.length === 0 ? ts.factory.createStringLiteral(propertyOrAliasName) : - propertyAndStringKey.length === 4 && propertyAndStringKey[2] as ts.Expression, ts.factory.createStringLiteral(name)]))); + createPropertyAccessExpressionWithThis(INITIALIZE_CONSUME_FUNCTION), undefined, callExpressionArgs))); } function updateBuilderParamProperty(node: ts.PropertyDeclaration, diff --git a/compiler/test/transform_ut/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.js.sample b/compiler/test/transform_ut/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.js.sample new file mode 100644 index 000000000..9a71526f6 --- /dev/null +++ b/compiler/test/transform_ut/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.js.sample @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +"use strict"; +if (!("finalizeConstruction" in ViewPU.prototype)) { + Reflect.set(ViewPU.prototype, "finalizeConstruction", () => { }); +} +class CompA extends ViewPU { + constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) { + super(parent, __localStorage, elmtId, extraInfo); + if (typeof paramsLambda === "function") { + this.paramsGenerator_ = paramsLambda; + } + this.__reviewVotes = new ObservedPropertySimplePU(0, this, "reviewVotes"); + this.addProvidedVar("reviewVote", this.__reviewVotes, false); + this.addProvidedVar("reviewVotes", this.__reviewVotes, false); + this.__message = new ObservedPropertySimplePU('1111', this, "message"); + this.addProvidedVar('aliasName', this.__message, true); + this.addProvidedVar("message", this.__message, true); + this.setInitiallyProvidedValue(params); + this.finalizeConstruction(); + } + setInitiallyProvidedValue(params) { + if (params.reviewVotes !== undefined) { + this.reviewVotes = params.reviewVotes; + } + if (params.message !== undefined) { + this.message = params.message; + } + } + updateStateVars(params) { + } + purgeVariableDependenciesOnElmtId(rmElmtId) { + this.__reviewVotes.purgeDependencyOnElmtId(rmElmtId); + this.__message.purgeDependencyOnElmtId(rmElmtId); + } + aboutToBeDeleted() { + this.__reviewVotes.aboutToBeDeleted(); + this.__message.aboutToBeDeleted(); + SubscriberManager.Get().delete(this.id__()); + this.aboutToBeDeletedInternal(); + } + get reviewVotes() { + return this.__reviewVotes.get(); + } + set reviewVotes(newValue) { + this.__reviewVotes.set(newValue); + } + get message() { + return this.__message.get(); + } + set message(newValue) { + this.__message.set(newValue); + } + initialRender() { + this.observeComponentCreation2((elmtId, isInitialRender) => { + Column.create(); + }, Column); + { + this.observeComponentCreation2((elmtId, isInitialRender) => { + if (isInitialRender) { + let componentCall = new CompB(this, {}, undefined, elmtId, () => { }, { page: "test/transform_ut/application/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.ets", line: 23, col: 7 }); + ViewPU.create(componentCall); + let paramsLambda = () => { + return {}; + }; + componentCall.paramsGenerator_ = paramsLambda; + } + else { + this.updateStateVarsOfChildByElmtId(elmtId, {}); + } + }, { name: "CompB" }); + } + this.observeComponentCreation2((elmtId, isInitialRender) => { + Button.createWithChild(); + Button.onClick(() => { + this.reviewVotes += 1; + }); + }, Button); + this.observeComponentCreation2((elmtId, isInitialRender) => { + Text.create('' + this.reviewVotes); + Text.fontSize(30); + }, Text); + Text.pop(); + Button.pop(); + Column.pop(); + } + rerender() { + this.updateDirtyElements(); + } + static getEntryName() { + return "CompA"; + } +} +class CompB extends ViewPU { + constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) { + super(parent, __localStorage, elmtId, extraInfo); + if (typeof paramsLambda === "function") { + this.paramsGenerator_ = paramsLambda; + } + this.setInitiallyProvidedValue(params); + this.finalizeConstruction(); + } + setInitiallyProvidedValue(params) { + } + updateStateVars(params) { + } + purgeVariableDependenciesOnElmtId(rmElmtId) { + } + aboutToBeDeleted() { + SubscriberManager.Get().delete(this.id__()); + this.aboutToBeDeletedInternal(); + } + initialRender() { + this.observeComponentCreation2((elmtId, isInitialRender) => { + Column.create(); + }, Column); + { + this.observeComponentCreation2((elmtId, isInitialRender) => { + if (isInitialRender) { + let componentCall = new CompC(this, {}, undefined, elmtId, () => { }, { page: "test/transform_ut/application/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.ets", line: 39, col: 7 }); + ViewPU.create(componentCall); + let paramsLambda = () => { + return {}; + }; + componentCall.paramsGenerator_ = paramsLambda; + } + else { + this.updateStateVarsOfChildByElmtId(elmtId, {}); + } + }, { name: "CompC" }); + } + Column.pop(); + } + rerender() { + this.updateDirtyElements(); + } +} +class CompC extends ViewPU { + constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) { + super(parent, __localStorage, elmtId, extraInfo); + if (typeof paramsLambda === "function") { + this.paramsGenerator_ = paramsLambda; + } + this.__reviewVotes = this.initializeConsume("reviewVote", "reviewVotes"); + this.setInitiallyProvidedValue(params); + this.finalizeConstruction(); + } + setInitiallyProvidedValue(params) { + } + updateStateVars(params) { + } + purgeVariableDependenciesOnElmtId(rmElmtId) { + this.__reviewVotes.purgeDependencyOnElmtId(rmElmtId); + } + aboutToBeDeleted() { + this.__reviewVotes.aboutToBeDeleted(); + SubscriberManager.Get().delete(this.id__()); + this.aboutToBeDeletedInternal(); + } + get reviewVotes() { + return this.__reviewVotes.get(); + } + set reviewVotes(newValue) { + this.__reviewVotes.set(newValue); + } + initialRender() { + this.observeComponentCreation2((elmtId, isInitialRender) => { + Column.create(); + }, Column); + this.observeComponentCreation2((elmtId, isInitialRender) => { + Button.createWithChild(); + Button.onClick(() => { + this.reviewVotes += 1; + }); + }, Button); + this.observeComponentCreation2((elmtId, isInitialRender) => { + Text.create('' + this.reviewVotes); + Text.fontSize(30); + }, Text); + Text.pop(); + Button.pop(); + Column.pop(); + } + rerender() { + this.updateDirtyElements(); + } +} +registerNamedRoute(() => new CompA(undefined, {}), "", { bundleName: "com.example.application", moduleName: "application", pagePath: "pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue", pageFullPath: "application/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue", integratedHsp: "false", moduleType: "followWithHap" }); +//# sourceMappingURL=@consume_with_defaultValue.js.map \ No newline at end of file diff --git a/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.ets b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.ets new file mode 100644 index 000000000..5c5da8217 --- /dev/null +++ b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForPartialUpdate/ui_state_management/others/@consume_@provide/@consume_with_defaultValue.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@Entry +@Component +struct CompA { + @Provide("reviewVote") reviewVotes : number = 0; + @Provide({allowOverride: 'aliasName'}) message: string = '1111' + + build() { + Column() { + CompB() + Button() { + Text('' + this.reviewVotes) + .fontSize(30) + } + .onClick(() => { + this.reviewVotes += 1; + }) + } + } +} + +@Component +struct CompB { + build() { + Column() { + CompC() + } + } +} + +@Component +struct CompC { + @Consume("reviewVote") reviewVotes : number = 15; + build() { + Column() { + Button() { + Text('' + this.reviewVotes) + .fontSize(30) + } + .onClick(() => { + this.reviewVotes += 1; + }) + } + } +} \ No newline at end of file diff --git a/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_component_member/validateForbiddenSpecifyDefaultValue.ets b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_component_member/validateForbiddenSpecifyDefaultValue.ets new file mode 100644 index 000000000..65664138a --- /dev/null +++ b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_component_member/validateForbiddenSpecifyDefaultValue.ets @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // To validate the rule that variables decorated by specified decorators can not have default value + @Observed + class Info { + public info: number =0; + + constructor(info: number) { + this.info = info; + } + } + + @Component + struct child { + @Consume('trial') bulubulu: string = 'bulubulu'; + @Link linkProperty : number = 4; + @ObjectLink objectProperty: Info = new Info(32); + + build(){ + Row(){ + Text(this.bulubulu) + } + } + } + + @Entry + @Component + struct parent { + @State stateProperty: number = 6; + @Provide provideProperty: Info = new Info(33); + + build(){ + Row(){ + child({ + linkProperty:this.stateProperty, + objectProperty:this.provideProperty + }) + Blank() + Text(this.stateProperty) + } + } + } \ No newline at end of file diff --git a/compiler/test/transform_ut/helpers/pathConfig.ts b/compiler/test/transform_ut/helpers/pathConfig.ts index 9ed688e8a..936ccfdc3 100644 --- a/compiler/test/transform_ut/helpers/pathConfig.ts +++ b/compiler/test/transform_ut/helpers/pathConfig.ts @@ -178,6 +178,7 @@ export const UT_PARTIAL_UPFATE_PAGES: string[] = [ 'ui_state_management/inner_struct_state_management/@state/@state', 'ui_state_management/inner_struct_state_management/@state/@state1', 'ui_state_management/others/@consume_@provide/@consume_@provide', + 'ui_state_management/others/@consume_@provide/@consume_with_defaultValue', 'ui_state_management/others/@observed_@objectLink/@observed_@objectLink', 'ui_state_management/others/@observed_@objectLink/@observed_@objectLink1', 'ui_state_management/others/@watch/@watch', @@ -244,6 +245,7 @@ export const UT_VALIDATE_PAGES: string[] = [ 'Decorators/process_component_member/updateBuilderParamProperty', 'Decorators/process_component_member/validateCustomDecorator', 'Decorators/process_component_member/validateDuplicateDecorator', + 'Decorators/process_component_member/validateForbiddenSpecifyDefaultValue', 'Decorators/process_component_member/validateForbiddenUseStateType', 'Decorators/process_component_member/validateHasIllegalDecoratorInEntry', 'Decorators/process_component_member/validateHasIllegalQuestionToken', diff --git a/compiler/test/transform_ut_error.json b/compiler/test/transform_ut_error.json index a6a0320c0..c799dbb29 100644 --- a/compiler/test/transform_ut_error.json +++ b/compiler/test/transform_ut_error.json @@ -157,6 +157,20 @@ "type": "ERROR", "code": "10905311" }, + "validateForbiddenSpecifyDefaultValue": [ + { + "message": "The '@Link' property cannot be specified a default value.", + "type": "ERROR", + "code": "10905304", + "solutions": ["Please initialize the rules according to the decorator."] + }, + { + "message": "The '@ObjectLink' property cannot be specified a default value.", + "type": "ERROR", + "code": "10905304", + "solutions": ["Please initialize the rules according to the decorator."] + } + ], "validateCustomDecorator": { "message": "The inner decorator '@State' cannot be used together with custom decorator.", "type": "ERROR", -- Gitee