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 { getAnnotationUsage, PresetDecorators } from '../utils'; 18import { UISyntaxRule, UISyntaxRuleContext } from './ui-syntax-rule'; 19 20function checkNoPropLinkOrObjectLinkInEntry(context: UISyntaxRuleContext, node: arkts.StructDeclaration): void { 21 // Check if the struct has the @Entry decorator 22 const isEntryComponent = !!getAnnotationUsage(node, PresetDecorators.ENTRY); 23 if (!isEntryComponent) { 24 return; 25 } 26 node.definition.body.forEach(body => { 27 if (!arkts.isClassProperty(body)) { 28 return; 29 } 30 const invalidDecorators = [PresetDecorators.PROP, PresetDecorators.LINK, PresetDecorators.OBJECT_LINK]; 31 // Check if any invalid decorators are applied to the class property 32 body.annotations?.forEach(annotation => { 33 reportInvalidDecorator(context, annotation, invalidDecorators); 34 }); 35 }); 36} 37 38function reportInvalidDecorator(context: UISyntaxRuleContext, annotation: arkts.AnnotationUsage, 39 invalidDecorators: string[],): void { 40 if (annotation.expr && invalidDecorators.includes(annotation.expr.dumpSrc())) { 41 const decorator = annotation.expr.dumpSrc(); 42 context.report({ 43 node: annotation, 44 message: rule.messages.invalidUsage, 45 data: { decorator }, 46 fix: (annotation) => { 47 const startPosition = arkts.getStartPosition(annotation); 48 const endPosition = arkts.getEndPosition(annotation); 49 return { 50 range: [startPosition, endPosition], 51 code: '', 52 }; 53 }, 54 }); 55 } 56} 57 58const rule: UISyntaxRule = { 59 name: 'no-prop-link-objectlink-in-entry', 60 messages: { 61 invalidUsage: `@{{decorator}} decorator cannot be used for '@Entry' decorated components.`, 62 }, 63 setup(context) { 64 return { 65 parsed: (node): void => { 66 if (!arkts.isStructDeclaration(node)) { 67 return; 68 } 69 checkNoPropLinkOrObjectLinkInEntry(context, node); 70 }, 71 }; 72 }, 73}; 74 75export default rule; 76