diff --git a/compiler/src/process_component_class.ts b/compiler/src/process_component_class.ts index e39e30174a1c234ef8eb04e95fec81881914908d..160a4d911f73f1f39efa324771524550b2f8bf27 100644 --- a/compiler/src/process_component_class.ts +++ b/compiler/src/process_component_class.ts @@ -153,7 +153,8 @@ import { import { builderTypeParameter, initializeMYIDS, - globalBuilderParamAssignment + globalBuilderParamAssignment, + parseStylesNode } from './process_ui_syntax'; import constantDefine from './constant_define'; import processStructComponentV2, { StructInfo } from './process_struct_componentV2'; @@ -626,6 +627,7 @@ export function processComponentMethod(node: ts.MethodDeclaration, context: ts.T storedFileInfo.processBuilder = false; storedFileInfo.processLocalBuilder = false; } else if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) { + parseStylesNode(node, log); if (node.parameters && node.parameters.length === 0) { if (ts.isBlock(node.body) && node.body.statements && node.body.statements.length) { INNER_STYLE_FUNCTION.set(name, node.body); diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index d4d323d9d3d5acd57521d8a363647844c722d34b..7c9da6ef8dcba4ecdf835335d0af6643adc6f392 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -356,6 +356,7 @@ export function processUISyntax(program: ts.Program, ut = false, storedFileInfo.processBuilder = false; storedFileInfo.processGlobalBuilder = false; } else if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) { + parseStylesNode(node, transformLog.errors); if (node.parameters.length === 0) { node = undefined; } else { @@ -1304,19 +1305,20 @@ 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]); + if (node.body && ts.isBlock(node.body) && node.body.statements && + node.body.statements.length > 1) { + validateUIFunctionFormat(log, node.body.statements[0]); return; } - if (node.body && node.body.statements && node.body.statements.length === 1 && - ts.isExpressionStatement(node.body.statements[0])) { + if (node.body && ts.isBlock(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]); + validateUIFunctionFormat(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]); + if (node.body && ts.isBlock(node.body) && node.body.statements && + node.body.statements.length === 1 && !ts.isExpressionStatement(node.body.statements[0])) { + validateUIFunctionFormat(log, node.body.statements[0]); return; } } @@ -1344,7 +1346,7 @@ function findInstanceIdentifier(node: ts.CallExpression): string { return instanceName; } -function validateExtendFunctionFormat(log: LogInfo[], block: ts.Statement): void { +function validateUIFunctionFormat(log: LogInfo[], block: ts.Statement): void { log.push({ message: `Only UI component syntax can be written here.`, type: LogType.WARN, @@ -1352,6 +1354,18 @@ function validateExtendFunctionFormat(log: LogInfo[], block: ts.Statement): void }); } +export function parseStylesNode(node: ts.FunctionDeclaration | ts.MethodDeclaration, log: LogInfo[]): void { + const commonInstance: string = 'CommonInstance'; + if (node.body && ts.isBlock(node.body) && node.body.statements && node.body.statements.length) { + if (node.body.statements.length === 1 && ts.isExpressionStatement(node.body.statements[0])) { + !validateComponentInstance(node.body.statements[0], commonInstance) && + validateUIFunctionFormat(log, node.body.statements[0]); + return; + } + validateUIFunctionFormat(log, node.body.statements[0]); + } +} + 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/StylesUISyntax.ets b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_ui_syntax/StylesUISyntax.ets new file mode 100644 index 0000000000000000000000000000000000000000..8c18821527516e1a13b8f1f7b79a8873393206ee --- /dev/null +++ b/compiler/test/transform_ut/application/entry/src/main/ets/pages/utForValidate/Decorators/process_ui_syntax/StylesUISyntax.ets @@ -0,0 +1,55 @@ +/* + * 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. + */ +@Styles +function stylesOne() { + .width(100) + console.log(`this is a function`); +} + +@Styles +function stylesTwo() { + .height(200) +} + +@Entry +@ComponentV2 +struct testStylesV2 { + @Styles + innerStylesOne() { + .width(100) + console.log(`method`); + } + + build() { + Column() { + } + } +} + +@Component +struct testStyles { + @Styles + innerStyles(){ + if(true){ + .width(100) + } + } + + build() { + Row(){ + + } + } +} \ 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 fe2c744d809b34c9ab979a83b05ba1d8ced6d551..98a0152679d5d5694c24abbfbc1f0ce957ca661f 100644 --- a/compiler/test/transform_ut/helpers/pathConfig.ts +++ b/compiler/test/transform_ut/helpers/pathConfig.ts @@ -285,6 +285,7 @@ export const UT_VALIDATE_PAGES: string[] = [ 'Decorators/process_ui_syntax/NotSupportResrcParam', 'Decorators/process_ui_syntax/NotSupportResrcType', 'Decorators/process_ui_syntax/StylesNoParam', + 'Decorators/process_ui_syntax/StylesUISyntax', 'Decorators/process_ui_syntax/UnknownSrc', 'Decorators/process_ui_syntax/UnknownSrcName', 'Decorators/process_ui_syntax/UnknownSrcType', diff --git a/compiler/test/transform_ut_error.json b/compiler/test/transform_ut_error.json index 222e4ec13b7f589d0638058f3814f004854916ac..91dec88b07c07e0b8a0bed12de5d97957d8a63e5 100644 --- a/compiler/test/transform_ut_error.json +++ b/compiler/test/transform_ut_error.json @@ -364,6 +364,20 @@ "type": "ERROR", "code": "10905105" }, + "StylesUISyntax": [ + { + "message": "Only UI component syntax can be written here.", + "type": "WARN" + }, + { + "message": "Only UI component syntax can be written here.", + "type": "WARN" + }, + { + "message": "Only UI component syntax can be written here.", + "type": "WARN" + } + ], "ExceededPreview": { "message": "A page can contain at most 10 '@Preview' decorators.", "type": "ERROR",