1/* 2 * Copyright (c) 2024-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 { FullPosition } from '../base/Position'; 17 18export enum ArkMetadataKind { 19 LEADING_COMMENTS, 20 TRAILING_COMMENTS, 21} 22 23export interface ArkMetadataType {} 24 25/** 26 * ArkMetadata 27 * @example 28 * // get leading comments 29 * let stmt: Stmt = xxx; 30 * let comments = stmt.getMetadata(ArkMetadataKind.LEADING_COMMENTS) || []; 31 * comments.forEach((comment) => { 32 * logger.info(comment); 33 * }); 34 */ 35export class ArkMetadata { 36 protected metadata?: Map<ArkMetadataKind, ArkMetadataType>; 37 38 public getMetadata(kind: ArkMetadataKind): ArkMetadataType | undefined { 39 return this.metadata?.get(kind); 40 } 41 42 public setMetadata(kind: ArkMetadataKind, value: ArkMetadataType): void { 43 if (!this.metadata) { 44 this.metadata = new Map(); 45 } 46 this.metadata.set(kind, value); 47 } 48} 49 50export type CommentItem = { 51 content: string; 52 position: FullPosition; 53}; 54 55export class CommentsMetadata implements ArkMetadataType { 56 private comments: CommentItem[] = []; 57 58 constructor(comments: CommentItem[]) { 59 this.comments = comments; 60 } 61 62 public getComments(): CommentItem[] { 63 return this.comments; 64 } 65} 66