diff --git a/arkui-plugins/ui-syntax-plugins/rules/builderparam-decorator-check.ts b/arkui-plugins/ui-syntax-plugins/rules/builderparam-decorator-check.ts index ac532a3e906f50277ca0328d564a391b1fbc946a..2b97c5808d27b57214db3474174e2cc301dd112c 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/builderparam-decorator-check.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/builderparam-decorator-check.ts @@ -19,6 +19,7 @@ import { AbstractUISyntaxRule } from './ui-syntax-rule'; class BuilderParamDecoratorCheckRule extends AbstractUISyntaxRule { private structNameWithMultiplyBuilderParam: string[] = []; + private structNameWithoutBuilderParam: string[] = []; public setup(): Record { return { @@ -60,6 +61,9 @@ class BuilderParamDecoratorCheckRule extends AbstractUISyntaxRule { if (count > 1) { this.structNameWithMultiplyBuilderParam.push(structName); } + if (count === 0) { + this.structNameWithoutBuilderParam.push(structName); + } }); } @@ -105,13 +109,14 @@ class BuilderParamDecoratorCheckRule extends AbstractUISyntaxRule { if (!arkts.isCallExpression(parentNode)) { return; } - if (!arkts.isIdentifier(node) || !this.structNameWithMultiplyBuilderParam.includes(getIdentifierName(node))) { + let structName: string = getIdentifierName(node); + if (!arkts.isIdentifier(node) || !(this.structNameWithMultiplyBuilderParam.includes(structName) || + this.structNameWithoutBuilderParam.includes(structName))) { return; } if (!this.hasBlockStatement(node)) { return; } - let structName: string = getIdentifierName(node); let structNode = node.parent; while (!arkts.isStructDeclaration(structNode)) { if (!structNode.parent) { diff --git a/arkui-plugins/ui-syntax-plugins/rules/struct-property-decorator.ts b/arkui-plugins/ui-syntax-plugins/rules/struct-property-decorator.ts index fd6f98af874cc05d974dafbd22dc0acab2a9b985..81228846b8fd8c0f49abb19b890c95ffe39529c6 100644 --- a/arkui-plugins/ui-syntax-plugins/rules/struct-property-decorator.ts +++ b/arkui-plugins/ui-syntax-plugins/rules/struct-property-decorator.ts @@ -17,7 +17,7 @@ import * as arkts from '@koalaui/libarkts'; import { getClassPropertyAnnotationNames, hasAnnotation, PresetDecorators } from '../utils'; import { AbstractUISyntaxRule } from './ui-syntax-rule'; -const decorators: string[] = [ +const v1Decorators: string[] = [ PresetDecorators.BUILDER_PARAM, PresetDecorators.STATE, PresetDecorators.PROP_REF, @@ -30,6 +30,16 @@ const decorators: string[] = [ PresetDecorators.REQUIRE, ]; +const v2Decorators: string[] = [ + PresetDecorators.PARAM, + PresetDecorators.ONCE, + PresetDecorators.EVENT, + PresetDecorators.PROVIDER, + PresetDecorators.CONSUMER, + PresetDecorators.MONITOR, + PresetDecorators.REQUIRE, +]; + class StructPropertyDecoratorRule extends AbstractUISyntaxRule { public setup(): Record { return { @@ -38,15 +48,22 @@ class StructPropertyDecoratorRule extends AbstractUISyntaxRule { } public parsed(node: arkts.AstNode): void { - if (!arkts.isStructDeclaration(node)) { - return; + if (arkts.isStructDeclaration(node)) { + const hasComponentV1 = hasAnnotation(node.definition.annotations, PresetDecorators.COMPONENT_V1); + const hasComponentV2 = hasAnnotation(node.definition.annotations, PresetDecorators.COMPONENT_V2); + this.checkInvalidStaticPropertyDecorations(node, hasComponentV1, hasComponentV2); + if (hasComponentV2) { + this.checkInvalidStaticMethodDecorations(node); + } + } + if (arkts.isClassDeclaration(node)) { + this.checkInvalidStaticMethodDecorations(node); } - const hasComponentV1 = hasAnnotation(node.definition.annotations, PresetDecorators.COMPONENT_V1); - this.checkInvalidStaticPropertyDecorations(node, hasComponentV1); } private hasPropertyDecorator( member: arkts.ClassProperty, + decorators: String[] ): boolean { const annotationName = getClassPropertyAnnotationNames(member); return decorators.some(decorator => @@ -54,16 +71,43 @@ class StructPropertyDecoratorRule extends AbstractUISyntaxRule { ); } - private checkInvalidStaticPropertyDecorations(node: arkts.StructDeclaration, hasComponentV1: boolean): void { + private checkInvalidStaticPropertyDecorations( + node: arkts.StructDeclaration, + hasComponentV1: boolean, + hasComponentV2: boolean + ): void { node.definition.body.forEach((member) => { // Errors are reported when the node type is static ClassProperty, - if (!arkts.isClassProperty(member) || !member.key) { + if (!arkts.isClassProperty(member) || !member.key || !member.isStatic) { + return; + } + if (hasComponentV1 && this.hasPropertyDecorator(member, v1Decorators) || + hasComponentV2 && this.hasPropertyDecorator(member, v2Decorators)) { + const propertyNameNode = member.key; + this.report({ + node: propertyNameNode, + message: this.messages.invalidStaticUsage + }); + } + }); + } + + private checkInvalidStaticMethodDecorations(node: arkts.ClassDeclaration | arkts.StructDeclaration): void { + node.definition?.body.forEach((member) => { + // Errors are reported when the node type is static Method, + if (!arkts.isMethodDefinition(member) || !member.name || !member.isStatic) { return; } - if (!hasComponentV1 || !member.isStatic || !this.hasPropertyDecorator(member)) { + const hasMonitor = member.funcExpr.scriptFunction.annotations.some(annotation => { + if (!annotation.expr || !arkts.isIdentifier(annotation.expr)) { + return false; + } + return annotation.expr.name === PresetDecorators.MONITOR; + }); + if (!hasMonitor) { return; } - const propertyNameNode = member.key; + const propertyNameNode = member.name; this.report({ node: propertyNameNode, message: this.messages.invalidStaticUsage