From 5025c1c8542304b6c4ec3ec8e53e082851d40d92 Mon Sep 17 00:00:00 2001 From: sniperc96 Date: Thu, 4 Sep 2025 15:27:58 +0800 Subject: [PATCH] fix bug:Local in class Signed-off-by: sniperc96 Change-Id: Ie556a92199f93c512222d647bfd0308b53e184f1 --- .../rules/old-new-decorator-mix-use-check.ts | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/arkui-plugins/ui-syntax-plugins/rules/old-new-decorator-mix-use-check.ts b/arkui-plugins/ui-syntax-plugins/rules/old-new-decorator-mix-use-check.ts index 704eb038b..bfdb8a3a2 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/old-new-decorator-mix-use-check.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/old-new-decorator-mix-use-check.ts @@ -47,10 +47,16 @@ class OldNewDecoratorMixUseCheckRule extends AbstractUISyntaxRule { oldAndNewDecoratorsMixUse: `The '@{{decoratorName}}' annotation can only be used in a 'struct' decorated with '@{{component}}'.`, }; } - public parsed(node: arkts.StructDeclaration): void { - if (!arkts.isStructDeclaration(node)) { - return; + + public parsed(node: arkts.StructDeclaration | arkts.ClassDeclaration): void { + if (arkts.isStructDeclaration(node)) { + this.handleStructDeclaration(node); + } else if (arkts.isClassDeclaration(node)) { + this.handleClassDeclaration(node); } + } + + private handleStructDeclaration(node: arkts.StructDeclaration): void { // Gets the decorator version of a custom component const componentV2Decorator = getAnnotationUsage(node, PresetDecorators.COMPONENT_V2); const componentDecorator = getAnnotationUsage(node, PresetDecorators.COMPONENT_V1); @@ -74,6 +80,24 @@ class OldNewDecoratorMixUseCheckRule extends AbstractUISyntaxRule { }); } + private handleClassDeclaration(node: arkts.ClassDeclaration): void { + node.definition?.body.forEach((property) => { + if (!arkts.isClassProperty(property)) { + return; + } + const newDecorator = this.findPropertyDecorator(property, OldNewDecoratorMixUseCheckRule.newV2decorators); + const oldDecorator = this.findPropertyDecorator(property, OldNewDecoratorMixUseCheckRule.oldV1Decorators); + // Check that the new decorator is not used in struct + if (newDecorator) { + this.reportError(newDecorator, PresetDecorators.COMPONENT_V2); + } + // Check that the old decorator is not used in struct + if (oldDecorator) { + this.reportError(oldDecorator, PresetDecorators.COMPONENT_V1); + } + }); + } + private findPropertyDecorator( node: arkts.ClassProperty, decoratorList: string[] @@ -127,6 +151,18 @@ class OldNewDecoratorMixUseCheckRule extends AbstractUISyntaxRule { }, }); } + + private reportError(errorDecorator: arkts.AnnotationUsage, componentName: string): void { + const propertyDecoratorName = getAnnotationName(errorDecorator); + this.report({ + node: errorDecorator, + message: this.messages.oldAndNewDecoratorsMixUse, + data: { + decoratorName: propertyDecoratorName, + component: componentName + } + }); + } } export default OldNewDecoratorMixUseCheckRule; \ No newline at end of file -- Gitee