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, getClassPropertyAnnotationNames, PresetDecorators } from '../utils'; 18import { UISyntaxRule, UISyntaxRuleContext } from './ui-syntax-rule'; 19 20function checkLocalStorageLink(node: arkts.StructDeclaration, context: UISyntaxRuleContext): void { 21 // Check if @Entry decorator exists with parameter 22 const entryDecorator = getAnnotationUsage(node, PresetDecorators.ENTRY); 23 const isStorageUsed = entryDecorator && node.definition?.annotations[0].properties[0]; 24 // Check if @LocalStorageLink exists 25 let localStorageLinkUsed = false; 26 node.definition.body.forEach(body => { 27 if (!arkts.isClassProperty(body)) { 28 return; 29 } 30 const propertyDecorators = getClassPropertyAnnotationNames(body); 31 localStorageLinkUsed = propertyDecorators.some( 32 decorator => decorator === PresetDecorators.LOCAL_STORAGE_LINK); 33 }); 34 35 // If @LocalStorageLink is used but @Entry(storage) is missing, report error 36 if (entryDecorator && localStorageLinkUsed && !isStorageUsed) { 37 context.report({ 38 node: entryDecorator, 39 message: rule.messages.invalidUsage 40 }); 41 } 42} 43 44const rule: UISyntaxRule = { 45 name: 'entry-localstorage-check', 46 messages: { 47 invalidUsage: `@LocalStorageLink requires @Entry(storage) on the struct.`, 48 }, 49 setup(context) { 50 return { 51 parsed: (node): void => { 52 if (!arkts.isStructDeclaration(node)) { 53 return; 54 } 55 checkLocalStorageLink(node, context); 56 }, 57 }; 58 }, 59}; 60 61export default rule; 62