diff --git a/arkui-plugins/ui-syntax-plugins/rules/index.ts b/arkui-plugins/ui-syntax-plugins/rules/index.ts index 2a2aacc9f48de2637df26a63268b97c684776816..5548fbaaa2f05be10003e468b245eb2bba267cc6 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 d055b748fd80130234d418d63545361d6d1596ac..e4ddae7dc0366146f2e7590576124140c1f7a3e4 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 b33790e7542d3012faf921ea8f266ed81c79879b..32f2b369d6b65d7ba729d0a9b9cfdc9d5945d6df 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 34866868fb6a7fdcd7a59809038816d487fa7e5b..0e3f34341be809724e5b4355f51a60b3c9707c88 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