From b4fd53f334dfce1eaaee2ee560804fccde753e6c Mon Sep 17 00:00:00 2001 From: wangweiyuan2 Date: Thu, 26 Jun 2025 11:32:10 +0800 Subject: [PATCH] ui-syntax-plugins-0626-bug Signed-off-by: wangweiyuan2 --- .../component-componentV2-mix-use-check.ts | 31 ++----------------- .../rules/monitor-decorator-check.ts | 30 ++++++++++++------ .../rules/nested-relationship.ts | 15 ++------- .../ui-syntax-plugins/utils/index.ts | 2 +- 4 files changed, 27 insertions(+), 51 deletions(-) diff --git a/arkui-plugins/ui-syntax-plugins/rules/component-componentV2-mix-use-check.ts b/arkui-plugins/ui-syntax-plugins/rules/component-componentV2-mix-use-check.ts index 58140c91e..72c7bbc76 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/component-componentV2-mix-use-check.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/component-componentV2-mix-use-check.ts @@ -29,21 +29,11 @@ const v1ComponentDecorators: string[] = [ PresetDecorators.LOCAL_STORAGE_PROP, ]; -const v2ComponentDecorators: string[] = [ - PresetDecorators.LOCAL, - PresetDecorators.PARAM, - PresetDecorators.EVENT, - PresetDecorators.PROVIDER, - PresetDecorators.CONSUMER, -]; - class ComponentComponentV2MixUseCheckRule extends AbstractUISyntaxRule { - private observedV1Names: Set = new Set(); private observedV2Names: Set = new Set(); public setup(): Record { return { - observedv1_v2: `The type of the @{{annotation}} property cannot be a class decorated with '@Observed'.`, observedv2_v1: `The type of the @{{annotation}} property cannot be a class decorated with '@ObservedV2'.` }; } @@ -67,13 +57,9 @@ class ComponentComponentV2MixUseCheckRule extends AbstractUISyntaxRule { } const annotationName = getIdentifierName(anno.expr); - if (annotationName === PresetDecorators.OBSERVED_V1) { - const componentV1Name = node?.definition?.ident?.name; - componentV1Name ? this.observedV1Names.add(componentV1Name) : null; - } if (annotationName === PresetDecorators.OBSERVED_V2) { - const componentV2Name = node?.definition?.ident?.name; + const componentV2Name = node.definition?.ident?.name; componentV2Name ? this.observedV2Names.add(componentV2Name) : null; } }); @@ -108,9 +94,7 @@ class ComponentComponentV2MixUseCheckRule extends AbstractUISyntaxRule { ): void { if (arkts.isIdentifier(node)) { const name = getIdentifierName(node); - if (this.observedV1Names.has(name)) { - this.observedV1Names.add(typeName); - } + if (this.observedV2Names.has(name)) { this.observedV2Names.add(typeName); } @@ -160,13 +144,6 @@ class ComponentComponentV2MixUseCheckRule extends AbstractUISyntaxRule { const currentNode: arkts.AstNode = queue.shift() as arkts.AstNode; if (arkts.isIdentifier(currentNode)) { const name = getIdentifierName(currentNode); - if ( - annotationName === PresetDecorators.COMPONENT_V2 && - this.observedV1Names.has(name) - ) { - this.checkObservedConflict(node, v2ComponentDecorators); - break; - } if ( annotationName === PresetDecorators.COMPONENT_V1 && @@ -196,9 +173,7 @@ class ComponentComponentV2MixUseCheckRule extends AbstractUISyntaxRule { if (annotationName && componentDecorators.includes(annotationName)) { this.report({ node: anno, - message: this.messages[ - v1ComponentDecorators.includes(annotationName) ? 'observedv2_v1' : 'observedv1_v2' - ], + message: this.messages.observedv2_v1, data: { annotation: annotationName, }, diff --git a/arkui-plugins/ui-syntax-plugins/rules/monitor-decorator-check.ts b/arkui-plugins/ui-syntax-plugins/rules/monitor-decorator-check.ts index d1d95c461..a096d26da 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/monitor-decorator-check.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/monitor-decorator-check.ts @@ -48,14 +48,14 @@ class MonitorDecoratorCheckRule extends AbstractUISyntaxRule { // Check for errors related to @Monitor usage if (monitorUsed && !isObservedV2 && arkts.isClassDeclaration(node)) { - this.reportInvalidUsage( + this.checkInvalidUsage( node, this.messages.monitorUsedInObservedV2Class, `@${PresetDecorators.OBSERVED_V2}` ); } if (monitorUsed && !isComponentV2 && arkts.isStructDeclaration(node)) { - this.reportInvalidUsage( + this.checkInvalidUsage( node, this.messages.monitorUsedInComponentV2Struct, `@${PresetDecorators.COMPONENT_V2}\n` @@ -168,14 +168,24 @@ class MonitorDecoratorCheckRule extends AbstractUISyntaxRule { }); } - private reportInvalidUsage(node: arkts.AstNode, message: string, fixCode: string): void { - this.report({ - node, - message, - fix: () => ({ - range: [node.startPosition, node.startPosition], - code: fixCode - }) + private checkInvalidUsage(node: arkts.ClassDeclaration | arkts.StructDeclaration, message: string, fixCode: string): void { + let monitor: arkts.AnnotationUsage | undefined; + node.definition?.body.forEach((body) => { + if (!arkts.isMethodDefinition(body)) { + return; + } + monitor = this.getLocalMonitorUsed(body); + if (!monitor) { + return; + } + this.report({ + node: monitor, + message, + fix: () => ({ + range: [node.startPosition, node.startPosition], + code: fixCode + }) + }); }); } diff --git a/arkui-plugins/ui-syntax-plugins/rules/nested-relationship.ts b/arkui-plugins/ui-syntax-plugins/rules/nested-relationship.ts index 69027a616..fc07a421c 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/nested-relationship.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/nested-relationship.ts @@ -27,11 +27,10 @@ import { AbstractUISyntaxRule } from './ui-syntax-rule'; class NestedRelationshipRule extends AbstractUISyntaxRule { public setup(): Record { return { - noChildComponent: `No child component is allowed in the '{{componentName}}' component.`, + noChildComponent: `The component '{{componentName}}' can't have any child.`, singleChildComponent: `The '{{componentName}}' component can have only one child component.`, - delegateChildrenComponentChildren: `The '{{childComponentName}}' component cannot be a child component of the '{{parentComponentName}}' component.`, - delegateChildrenComponentParent: `The component '{{parentComponentName}}' can only have the child component {{childComponentList}}.`, - delegateParentComponent: `The '{{componentName}}' component can only be nested in the {{parentComponentList}} parent component.`, + delegateChildrenComponentParent: `The component '{{parentComponentName}}' can only have the child component '{{childComponentList}}'.`, + delegateParentComponent: `The '{{componentName}}' component can only be nested in the '{{parentComponentList}}' parent component.`, }; } public parsed(node: arkts.StructDeclaration): void { @@ -61,14 +60,6 @@ class NestedRelationshipRule extends AbstractUISyntaxRule { // If the parent component of the current component is not within the valid range, an error is reported const parentComponentName = curNode.expression.name; if (!this.context.componentsInfo.validParentComponent.get(componentName)!.includes(parentComponentName)) { - this.context.report({ - node: node, - message: this.messages.delegateChildrenComponentChildren, - data: { - childComponentName: componentName, - parentComponentName: parentComponentName, - }, - }); const parentComponentListArray: string[] = this.context.componentsInfo.validParentComponent.get(componentName)!; const parentComponentList: string = listToString(parentComponentListArray); this.report({ diff --git a/arkui-plugins/ui-syntax-plugins/utils/index.ts b/arkui-plugins/ui-syntax-plugins/utils/index.ts index 5dffe68af..c15264a79 100644 --- a/arkui-plugins/ui-syntax-plugins/utils/index.ts +++ b/arkui-plugins/ui-syntax-plugins/utils/index.ts @@ -211,7 +211,7 @@ export function isProtectedlassProperty(property: arkts.ClassProperty): boolean } export function listToString(strList: string[]): string { - return strList.length > 1 ? `${strList.slice(0, -1).join(', ')} and ${strList.slice(-1)}` : strList.join(''); + return strList.length > 1 ? `${strList.join(',')}` : strList.join(''); } export class MultiMap { -- Gitee