diff --git a/compiler/src/process_struct_componentV2.ts b/compiler/src/process_struct_componentV2.ts index 4b4e4b9f29305d63c5b8ee4ee4e917022f71e2d6..29c920198e45ddfc329af79e45c611adf02d590d 100644 --- a/compiler/src/process_struct_componentV2.ts +++ b/compiler/src/process_struct_componentV2.ts @@ -54,7 +54,8 @@ import { judgeBuilderParamAssignedByBuilder } from './process_component_member'; import { componentCollection, builderParamObjectCollection, - validateStmgmtKeywords + validateStmgmtKeywords, + validateStaticBlock } from './validate_ui_syntax'; import logMessageCollection from './log_message_collection'; import { globalProgram } from '../main'; @@ -436,6 +437,8 @@ function parseComponentProperty(node: ts.StructDeclaration, structInfo: StructIn } else if (ts.isMethodDeclaration(member)) { const decorators: readonly ts.Decorator[] = ts.getAllDecorators(member); parseMethodDeclaration(member, decorators, structInfo); + } else if (ts.isClassStaticBlockDeclaration(member)) { + validateStaticBlock(member); } }); } diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index dbe4379fb769fe6103d7e6d22cfee340b9e872d0..e8dfc95c80a33d420eb22e59f81d7efef47567a9 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -1519,6 +1519,9 @@ function traversalComponentProps(node: ts.StructDeclaration, componentSet: IComp validateStateVariable(item); currentMethodCollection.add(item.name.getText()); } + if (ts.isClassStaticBlockDeclaration(item)) { + validateStaticBlock(item); + } }); collectCurrentClassMethod(node, currentMethodCollection); } @@ -1932,6 +1935,14 @@ function validateStateVariable(node: ts.MethodDeclaration): void { } } +export function validateStaticBlock(node: ts.ClassStaticBlockDeclaration): void { + transformLog.errors.push({ + type: LogType.WARN, + message: `Static code blocks not supported in structs.`, + pos: node.getStart(), + }); +} + export function getObservedPropertyCollection(className: string): Set { const observedProperthCollection: Set = new Set([ ...stateCollection.get(className), diff --git a/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/vaildate_ui_syntax/validateStaticBlock.ets b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/vaildate_ui_syntax/validateStaticBlock.ets new file mode 100644 index 0000000000000000000000000000000000000000..f8519e3289978301973a7e544131a503c51d8aee --- /dev/null +++ b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/vaildate_ui_syntax/validateStaticBlock.ets @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 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. + */ +/* + * This test case is used to verify whether a static block is used in a struct. + */ +@Entry +@Component +struct ComponentSample { + static a:string = 'str'; + static { + this.a = 'str2'; + } + + build() { + Column(){ + Text(ComponentSample.a) + Text(ComponentV2Sample.a) + Text(DialogSample.a) + Text(classA.a) + } + } +} + +@ComponentV2 +struct ComponentV2Sample { + static a:string = 'str'; + static { + this.a = 'str2'; + } + + build() { + + } +} + +@CustomDialog +struct DialogSample { + cont:CustomDialogController; + static a:string = 'str'; + static { + this.a = 'str2'; + } + + build() { + + } +} + +class classA{ + static a: string = 'str'; + static { + classA.a = 'str2'; + } +} diff --git a/compiler/test/transform_ut/helpers/pathConfig.ts b/compiler/test/transform_ut/helpers/pathConfig.ts index fe2c744d809b34c9ab979a83b05ba1d8ced6d551..d7901b99e9e435f7901b900d2d096c1bd710b6c4 100644 --- a/compiler/test/transform_ut/helpers/pathConfig.ts +++ b/compiler/test/transform_ut/helpers/pathConfig.ts @@ -331,7 +331,8 @@ export const UT_VALIDATE_PAGES: string[] = [ 'Decorators/vaildate_ui_syntax/validateDuplicateMethod', 'Decorators/vaildate_ui_syntax/validateForEachParser', 'Decorators/vaildate_ui_syntax/validateRepeatParser', - 'Decorators/vaildate_ui_syntax/validateLazyForEachParser' + 'Decorators/vaildate_ui_syntax/validateLazyForEachParser', + 'Decorators/vaildate_ui_syntax/validateStaticBlock' ] export const UT_VALIDATE_PAGES_JSBUNDLE: string[] = [ diff --git a/compiler/test/transform_ut_error.json b/compiler/test/transform_ut_error.json index 222e4ec13b7f589d0638058f3814f004854916ac..83381ce53e0c5a691a3e034774e23cc2c630c4ad 100644 --- a/compiler/test/transform_ut_error.json +++ b/compiler/test/transform_ut_error.json @@ -949,6 +949,20 @@ "type": "ERROR", "code": "10905305" }, + "validateStaticBlock": [ + { + "message": "Static code blocks not supported in structs.", + "type": "WARN" + }, + { + "message": "Static code blocks not supported in structs.", + "type": "WARN" + }, + { + "message": "Static code blocks not supported in structs.", + "type": "WARN" + } + ], "v2ToV1Link": [ { "message": "The property 'link_value' in the custom component 'V1' is missing (mandatory to specify).",