From df166a8a444a9ed5c60ea37fac917c4ea67f8557 Mon Sep 17 00:00:00 2001 From: wangweiyuan Date: Thu, 12 Jun 2025 16:52:06 +0800 Subject: [PATCH] =?UTF-8?q?add=20ui-syntax-plugins=200608=201=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangweiyuan --- .../ui-syntax-plugins/rules/index.ts | 2 + .../rules/main-pages-entry-check.ts | 74 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 arkui-plugins/ui-syntax-plugins/rules/main-pages-entry-check.ts diff --git a/arkui-plugins/ui-syntax-plugins/rules/index.ts b/arkui-plugins/ui-syntax-plugins/rules/index.ts index 92b50232d..a35ed35d8 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/index.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/index.ts @@ -28,6 +28,7 @@ import ComponentV2StateUsageValidation from './componentV2-state-usage-validatio import CustomDialogMissingController from './custom-dialog-missing-controller'; import EntryLoacalStorageCheck from './entry-localstorage-check'; import EntryStructNoExport from './entry-struct-no-export'; +import MainPagesEntryCheckRule from './main-pages-entry-check'; import MonitorDecoratorCheck from './monitor-decorator-check'; import NestedRelationship from './nested-relationship'; import NestedReuseComponentCheck from './nested-reuse-component-check'; @@ -80,6 +81,7 @@ const rules: Array = [ CustomDialogMissingController, EntryLoacalStorageCheck, EntryStructNoExport, + [MainPagesEntryCheckRule, 'error'], MonitorDecoratorCheck, NestedRelationship, NestedReuseComponentCheck, diff --git a/arkui-plugins/ui-syntax-plugins/rules/main-pages-entry-check.ts b/arkui-plugins/ui-syntax-plugins/rules/main-pages-entry-check.ts new file mode 100644 index 000000000..198852e07 --- /dev/null +++ b/arkui-plugins/ui-syntax-plugins/rules/main-pages-entry-check.ts @@ -0,0 +1,74 @@ +/* + * 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. + */ + +import * as arkts from '@koalaui/libarkts'; +import { getAnnotationUsage, PresetDecorators } from '../utils'; +import { AbstractUISyntaxRule } from './ui-syntax-rule'; + +class MainPagesEntryCheckRule extends AbstractUISyntaxRule { + public setup(): Record { + return { + mainPagesEntryCheck: `A page configured in 'main_pages. json or build-profile. json5' must have one and only one '@Entry' decorator. ` + }; + } + public parsed(node: arkts.StructDeclaration): void { + if (!arkts.isEtsScript(node)) { + return; + } + const currentFilePath = this.getPath(); + if (!currentFilePath) { + return; + } + if (!this.context.getMainPages().includes(currentFilePath)) { + return; + } + let entryDecoratorCount = 0; + // Store the first StructDeclaration + let firstStructDeclaration: arkts.AstNode | undefined = undefined; + // Traverse all child nodes of the Program + for (const child of node.getChildren()) { + // Check if it's of type StructDeclaration + if (arkts.isStructDeclaration(child)) { + if (!firstStructDeclaration) { + firstStructDeclaration = child; + } + const entryDocoratorUsage = getAnnotationUsage( + child, + PresetDecorators.ENTRY, + ); + if (entryDocoratorUsage) { + ++entryDecoratorCount; + } + } + } + if (entryDecoratorCount === 0) { + this.report({ + node: firstStructDeclaration ? firstStructDeclaration : node, + message: this.messages.mainPagesEntryCheck, + }); + } + } + + private getPath(): string | undefined { + const contextPtr = arkts.arktsGlobal.compilerContext?.peer; + if (!!contextPtr) { + let program = arkts.getOrUpdateGlobalContext(contextPtr).program; + return program.globalAbsName; + } + return undefined; + } +} + +export default MainPagesEntryCheckRule; \ No newline at end of file -- Gitee