diff --git a/arkui-plugins/ui-syntax-plugins/rules/computed-decorator-check.ts b/arkui-plugins/ui-syntax-plugins/rules/computed-decorator-check.ts index 72a8024891eadd78b2f504c9cbccd1534ae8b589..683ba3d3c697b72afa12bf58d09b40d97fe43b04 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/computed-decorator-check.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/computed-decorator-check.ts @@ -14,7 +14,7 @@ */ import * as arkts from '@koalaui/libarkts'; -import { getIdentifierName, PresetDecorators, getAnnotationName, getAnnotationUsage, BUILD_NAME } from '../utils'; +import { getClassAnnotationUsage, getIdentifierName, PresetDecorators, getAnnotationName, getAnnotationUsage, BUILD_NAME, findDecorator } from '../utils'; import { AbstractUISyntaxRule } from './ui-syntax-rule'; class ComputedDecoratorCheckRule extends AbstractUISyntaxRule { @@ -57,10 +57,7 @@ class ComputedDecoratorCheckRule extends AbstractUISyntaxRule { if (arkts.isMethodDefinition(member)) { const methodName = getIdentifierName(member.name); - computedDecorator = member.scriptFunction.annotations?.find(annotation => - annotation.expr && arkts.isIdentifier(annotation.expr) && - annotation.expr.name === PresetDecorators.COMPUTED - ); + computedDecorator = findDecorator(member.scriptFunction, PresetDecorators.COMPUTED); this.validateComputedMethodKind(member, computedDecorator, methodName); if (member.kind === arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_SET) { @@ -77,10 +74,7 @@ class ComputedDecoratorCheckRule extends AbstractUISyntaxRule { } private validateComputedOnClassProperty(member: arkts.ClassProperty): void { - const computedDecorator = member.annotations?.find(annotation => - annotation.expr && arkts.isIdentifier(annotation.expr) && - annotation.expr.name === PresetDecorators.COMPUTED - ); + const computedDecorator = findDecorator(member, PresetDecorators.COMPUTED); if (computedDecorator) { this.report({ node: computedDecorator, @@ -211,18 +205,14 @@ class ComputedDecoratorCheckRule extends AbstractUISyntaxRule { } private validateClassBody(node: arkts.ClassDeclaration): void { - const observedV2Decorator = node.definition?.annotations.find(annotation => - getAnnotationName(annotation) === PresetDecorators.OBSERVED_V2 - ); + const observedV2Decorator = getClassAnnotationUsage(node, PresetDecorators.OBSERVED_V2); + const observedDecorator = getClassAnnotationUsage(node, PresetDecorators.OBSERVED_V1); node.definition?.body.forEach((member) => { if (arkts.isMethodDefinition(member)) { - this.validateComputedInClass(node, member, observedV2Decorator); - const computedDecorator = member.scriptFunction.annotations?.find(annotation => - annotation.expr && arkts.isIdentifier(annotation.expr) && - annotation.expr.name === PresetDecorators.COMPUTED - ); + this.validateComputedInClass(node, member, observedV2Decorator, observedDecorator); + const computedDecorator = findDecorator(member.scriptFunction, PresetDecorators.COMPUTED); if (!arkts.isIdentifier(member.name)) { return; } @@ -250,10 +240,7 @@ class ComputedDecoratorCheckRule extends AbstractUISyntaxRule { componentV2Decorator: arkts.AnnotationUsage | undefined, componentDecorator: arkts.AnnotationUsage | undefined ): void { - const computedDecorator = member.scriptFunction.annotations?.find(annotation => - annotation.expr && arkts.isIdentifier(annotation.expr) && - annotation.expr.name === PresetDecorators.COMPUTED - ); + const computedDecorator = findDecorator(member.scriptFunction, PresetDecorators.COMPUTED); if (computedDecorator && !componentV2Decorator && !componentDecorator) { this.report({ node: computedDecorator, @@ -290,12 +277,10 @@ class ComputedDecoratorCheckRule extends AbstractUISyntaxRule { node: arkts.AstNode, member: arkts.MethodDefinition, observedV2Decorator: arkts.AnnotationUsage | undefined, + observedDecorator: arkts.AnnotationUsage | undefined ): void { - const computedDecorator = member.scriptFunction.annotations?.find(annotation => - annotation.expr && arkts.isIdentifier(annotation.expr) && - annotation.expr.name === PresetDecorators.COMPUTED - ); - if (computedDecorator && !observedV2Decorator && + const computedDecorator = findDecorator(member.scriptFunction, PresetDecorators.COMPUTED); + if (computedDecorator && !observedV2Decorator && !observedDecorator && arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_GET === member.kind) { this.report({ node: computedDecorator, @@ -309,6 +294,22 @@ class ComputedDecoratorCheckRule extends AbstractUISyntaxRule { }, }); } + if (computedDecorator && !observedV2Decorator && observedDecorator && + arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_GET === member.kind) { + this.report({ + node: computedDecorator, + message: this.messages.onlyInObservedV2, + fix: () => { + let startPosition = observedDecorator.startPosition; + startPosition = arkts.SourcePosition.create(startPosition.index() - 1, startPosition.line()); + const endPosition = observedDecorator.endPosition; + return { + range: [startPosition, endPosition], + code: `@${PresetDecorators.OBSERVED_V2}`, + }; + }, + }); + } } } diff --git a/arkui-plugins/ui-syntax-plugins/rules/entry-localstorage-check.ts b/arkui-plugins/ui-syntax-plugins/rules/entry-localstorage-check.ts index 366cbc1da4df40f6250bb9f168fb22717d205891..7fb2892535ddafccfbf5fbb8d8cbc08c187d4586 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/entry-localstorage-check.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/entry-localstorage-check.ts @@ -34,7 +34,7 @@ class EntryLocalStorageCheckRule extends AbstractUISyntaxRule { private checkLocalStorageLink(node: arkts.StructDeclaration): void { // Check if @Entry decorator exists with parameter const entryDecorator = getAnnotationUsage(node, PresetDecorators.ENTRY); - const isStorageUsed = entryDecorator && node.definition.annotations[0].properties[0]; + const isStorageUsed = entryDecorator && entryDecorator.properties[0]; // Check if @LocalStorageLink exists let localStorageLinkUsed = false; node.definition.body.forEach(body => {