From 04f9bb62c99934accec4b1859ed3fc287b4a6866 Mon Sep 17 00:00:00 2001 From: yuxiaofei9 Date: Tue, 9 Sep 2025 16:31:55 +0800 Subject: [PATCH] add ui-syntax: validate static block Signed-off-by: yuxiaofei9 --- compiler/src/process_struct_componentV2.ts | 5 +- compiler/src/validate_ui_syntax.ts | 11 ++++ .../validateStaticBlock.ets | 66 +++++++++++++++++++ .../test/transform_ut/helpers/pathConfig.ts | 3 +- compiler/test/transform_ut_error.json | 14 ++++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/vaildate_ui_syntax/validateStaticBlock.ets diff --git a/compiler/src/process_struct_componentV2.ts b/compiler/src/process_struct_componentV2.ts index 4b4e4b9f2..29c920198 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 dbe4379fb..e8dfc95c8 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 000000000..0f5910c12 --- /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'; + } +} \ 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 98a015267..216156e3f 100644 --- a/compiler/test/transform_ut/helpers/pathConfig.ts +++ b/compiler/test/transform_ut/helpers/pathConfig.ts @@ -332,7 +332,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 91dec88b0..dab70fd90 100644 --- a/compiler/test/transform_ut_error.json +++ b/compiler/test/transform_ut_error.json @@ -963,6 +963,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).", -- Gitee