1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as arkts from '@koalaui/libarkts'; 17import { getClassPropertyAnnotationNames } from '../utils'; 18import { UISyntaxRule, UISyntaxRuleContext } from './ui-syntax-rule'; 19 20function checkInvalidStaticPropertyDecorations(context: UISyntaxRuleContext, node: arkts.StructDeclaration): void { 21 node.definition.body.forEach((member) => { 22 // Errors are reported when the node type is ClassProperty, 23 if (arkts.isClassProperty(member)) { 24 const propertyNameNode = member.key; 25 if ((member.isStatic && getClassPropertyAnnotationNames(member).length > 0) && propertyNameNode) { 26 context.report({ 27 node: propertyNameNode, 28 message: rule.messages.invalidStaticUsage 29 }); 30 } 31 } 32 }); 33} 34 35const rule: UISyntaxRule = { 36 name: 'struct-property-decorator', 37 messages: { 38 invalidStaticUsage: `Static variables in custom components cannot be decorated by built-in variable decorators.` 39 }, 40 setup(context) { 41 return { 42 parsed: (node): void => { 43 if (!arkts.isStructDeclaration(node)) { 44 return; 45 } 46 checkInvalidStaticPropertyDecorations(context, node); 47 }, 48 }; 49 }, 50}; 51 52export default rule;