From fd617cce22808f04d9bd73191b6261c95ae28459 Mon Sep 17 00:00:00 2001 From: wangweiyuan Date: Thu, 17 Jul 2025 10:59:07 +0800 Subject: [PATCH] ui-syntax-plugins 0717 Signed-off-by: wangweiyuan --- .../ui-syntax-plugins/rules/index.ts | 2 + .../rules/main-pages-entry-check.ts | 87 +++++++++---------- .../rules/no-duplicate-id.ts | 8 +- .../ui-syntax-plugins/utils/index.ts | 5 ++ 4 files changed, 48 insertions(+), 54 deletions(-) diff --git a/arkui-plugins/ui-syntax-plugins/rules/index.ts b/arkui-plugins/ui-syntax-plugins/rules/index.ts index 2a2aacc9f..5548fbaaa 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/index.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/index.ts @@ -29,6 +29,7 @@ import ComponentV2StateUsageValidationRule from './componentV2-state-usage-valid import CustomDialogMissingControllerRule from './custom-dialog-missing-controller'; import EntryLocalStorageCheckRule from './entry-localstorage-check'; import EntryStructNoExportRule from './entry-struct-no-export'; +import MainPagesEntryCheckRule from './main-pages-entry-check'; import MonitorDecoratorCheckRule from './monitor-decorator-check'; import NestedRelationshipRule from './nested-relationship'; import NestedReuseComponentCheckRule from './nested-reuse-component-check'; @@ -80,6 +81,7 @@ const rules: Array = [ [CustomDialogMissingControllerRule, 'error'], [EntryLocalStorageCheckRule, 'warn'], [EntryStructNoExportRule, 'warn'], + [MainPagesEntryCheckRule, 'error'], [MonitorDecoratorCheckRule, 'error'], [NestedRelationshipRule, 'error'], [NestedReuseComponentCheckRule, 'error'], 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 index d055b748f..e4ddae7dc 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/main-pages-entry-check.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/main-pages-entry-check.ts @@ -14,61 +14,52 @@ */ import * as arkts from '@koalaui/libarkts'; -import { getAnnotationUsage, PresetDecorators } from '../utils'; +import { getAnnotationUsage, getCurrentFilePath, 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' annotation. ` - }; - } - public parsed(node: arkts.StructDeclaration): void { - if (!arkts.isEtsScript(node)) { - return; + public setup(): Record { + return { + mainPagesEntryCheck: `A page configured in 'main_pages. json or build-profile. json5' must have one and only one '@Entry' annotation. ` + }; } - 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; + public parsed(node: arkts.StructDeclaration): void { + if (!arkts.isEtsScript(node)) { + return; } - const entryDocoratorUsage = getAnnotationUsage( - child, - PresetDecorators.ENTRY, - ); - if (entryDocoratorUsage) { - ++entryDecoratorCount; + const currentFilePath = getCurrentFilePath(node); + 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, + }); } - } - } - 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 diff --git a/arkui-plugins/ui-syntax-plugins/rules/no-duplicate-id.ts b/arkui-plugins/ui-syntax-plugins/rules/no-duplicate-id.ts index b33790e75..32f2b369d 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/no-duplicate-id.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/no-duplicate-id.ts @@ -15,6 +15,7 @@ import * as arkts from '@koalaui/libarkts'; import { AbstractUISyntaxRule } from './ui-syntax-rule'; +import { getCurrentFilePath } from '../utils'; interface IdInfo { value: string; @@ -62,7 +63,7 @@ class NoDuplicateIdRule extends AbstractUISyntaxRule { message: this.messages.duplicateId, data: { id: idInfo.value, - path: this.getPath(node) ?? '', + path: getCurrentFilePath(node) ?? '', line: idInfo.node.startPosition.line().toString(), index: idInfo.node.startPosition.index().toString() } @@ -73,11 +74,6 @@ class NoDuplicateIdRule extends AbstractUISyntaxRule { } } - private getPath(node: arkts.AstNode): string | undefined { - const program = arkts.getProgramFromAstNode(node); - return program.absName; - } - private getIdInfo(node: arkts.CallExpression): IdInfo | undefined { const callee = node.expression; diff --git a/arkui-plugins/ui-syntax-plugins/utils/index.ts b/arkui-plugins/ui-syntax-plugins/utils/index.ts index 34866868f..0e3f34341 100644 --- a/arkui-plugins/ui-syntax-plugins/utils/index.ts +++ b/arkui-plugins/ui-syntax-plugins/utils/index.ts @@ -550,3 +550,8 @@ export const TypeFlags = { Literal: 'literal', Union: 'union', }; + +export function getCurrentFilePath(node: arkts.AstNode): string | undefined { + const program = arkts.getProgramFromAstNode(node); + return program.absName; +} \ No newline at end of file -- Gitee