From e55797f1f5e5da9e5c33eea66a41a8b5bf94ab4d Mon Sep 17 00:00:00 2001 From: Yenan Date: Wed, 3 Sep 2025 11:47:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dextend=E4=BC=A0if=E6=88=96swi?= =?UTF-8?q?tch=E8=AF=AD=E5=8F=A5=E6=B2=A1=E6=9C=89=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E6=88=96=E8=80=85=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yenan --- compiler/src/process_ui_syntax.ts | 52 ++++++++++++++++++ .../process_ui_syntax/ExtendUISyntax.ets | 53 +++++++++++++++++++ .../test/transform_ut/helpers/pathConfig.ts | 1 + compiler/test/transform_ut_error.json | 10 ++++ 4 files changed, 116 insertions(+) create mode 100644 compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_ui_syntax/ExtendUISyntax.ets diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 6a5607db1..d4d323d9d 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -1031,6 +1031,7 @@ export function processAnimateToOrImmediately(node: ts.CallExpression): ts.CallE function processExtend(node: ts.FunctionDeclaration, log: LogInfo[], decoratorName: string): ts.FunctionDeclaration { const componentName: string = isExtendFunction(node, { decoratorName: '', componentName: '' }, true); + checkExtendNode(node, componentName, log); if (componentName && node.body && !node.body.statements.length && decoratorName === COMPONENT_EXTEND_DECORATOR) { const statementArray: ts.Statement[] = []; const bodynode: ts.Block = ts.visitEachChild(node.body, traverseExtendExpression, contextGlobal); @@ -1300,6 +1301,57 @@ function parseExtendNode(node: ts.CallExpression, extendResult: ExtendResult, ch } } +function checkExtendNode(node: ts.FunctionDeclaration, componentName: string, + log: LogInfo[]): void { + const componentInstance: string = `${componentName}Instance`; + if (node.body && node.body.statements && node.body.statements.length > 1) { + validateExtendFunctionFormat(log, node.body.statements[0]); + return; + } + if (node.body && node.body.statements && node.body.statements.length === 1 && + ts.isExpressionStatement(node.body.statements[0])) { + !validateComponentInstance(node.body.statements[0], componentInstance) && + validateExtendFunctionFormat(log, node.body.statements[0]); + return; + } + if (node.body && node.body.statements && node.body.statements.length === 1 && + !ts.isExpressionStatement(node.body.statements[0])) { + validateExtendFunctionFormat(log, node.body.statements[0]); + return; + } +} + +function validateComponentInstance(node: ts.ExpressionStatement, targetInstanceName: string): boolean { + if (!ts.isCallExpression(node.expression)) { + return false; + } + const instanceName: string = findInstanceIdentifier(node.expression); + return instanceName === targetInstanceName; +} + +function findInstanceIdentifier(node: ts.CallExpression): string { + let instanceName: string = ''; + if (!ts.isPropertyAccessExpression(node.expression)) { + return instanceName; + } + const newNode: ts.PropertyAccessExpression = node.expression; + if (ts.isIdentifier(newNode.expression)) { + instanceName = newNode.expression.escapedText.toString(); + return instanceName; + } else if (ts.isCallExpression(newNode.expression)) { + instanceName = findInstanceIdentifier(newNode.expression); + } + return instanceName; +} + +function validateExtendFunctionFormat(log: LogInfo[], block: ts.Statement): void { + log.push({ + message: `Only UI component syntax can be written here.`, + type: LogType.WARN, + pos: block.getStart() + }); +} + function createEntryNode(node: ts.SourceFile, context: ts.TransformationContext, entryNodeKey: ts.Expression, id: number): ts.SourceFile { let cardRelativePath: string; diff --git a/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_ui_syntax/ExtendUISyntax.ets b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_ui_syntax/ExtendUISyntax.ets new file mode 100644 index 000000000..b9ffae0f8 --- /dev/null +++ b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_ui_syntax/ExtendUISyntax.ets @@ -0,0 +1,53 @@ +/* + * 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. + */ +@Extend(Text) +function fancyText(weightValue: number, color: Color) { + .fontStyle(FontStyle.Italic) + .fontWeight(weightValue) + .backgroundColor(color) +} + +@Extend(Button) +function extendButton() { + if (true) { + } else { + } +} + +@Extend(Button) +function extendButtonOne() { + switch (true){ + case true: + break; + } +} + +@Component +struct testExtend { + @State stringOne: string = 'stringOne'; + + build() { + Column() { + Text(this.stringOne) + } + } +} + +@Entry +@Component +struct ExtendInnerComponentsSummerpockets { + build(){ + } +} \ 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 49b950ba5..fe2c744d8 100644 --- a/compiler/test/transform_ut/helpers/pathConfig.ts +++ b/compiler/test/transform_ut/helpers/pathConfig.ts @@ -280,6 +280,7 @@ export const UT_VALIDATE_PAGES: string[] = [ 'Decorators/process_ui_syntax/EntryDecoParam', 'Decorators/process_ui_syntax/ExtendOneChild', 'Decorators/process_ui_syntax/ExtendInnerComponents', + 'Decorators/process_ui_syntax/ExtendUISyntax', 'Decorators/process_ui_syntax/NoSrc', 'Decorators/process_ui_syntax/NotSupportResrcParam', 'Decorators/process_ui_syntax/NotSupportResrcType', diff --git a/compiler/test/transform_ut_error.json b/compiler/test/transform_ut_error.json index 09c16a5f3..222e4ec13 100644 --- a/compiler/test/transform_ut_error.json +++ b/compiler/test/transform_ut_error.json @@ -320,6 +320,16 @@ "message": "'testExtend' parameter cannot be passed in the '@Extend' decorator.", "type": "WARN" }, + "ExtendUISyntax": [ + { + "message": "Only UI component syntax can be written here.", + "type": "WARN" + }, + { + "message": "Only UI component syntax can be written here.", + "type": "WARN" + } + ], "UnknownSrc": { "message": "Unknown resource source 'hap'.", "type": "ERROR", -- Gitee