1/* 2 * Copyright (c) 2022-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'; 17 18export interface VisitorOptions { 19 isExternal?: boolean; 20 externalSourceName?: string; 21 program?: arkts.Program; 22} 23 24export abstract class AbstractVisitor implements VisitorOptions { 25 public isExternal: boolean; 26 public externalSourceName?: string; 27 public program?: arkts.Program; 28 29 constructor(options?: VisitorOptions) { 30 this.isExternal = options?.isExternal ?? false; 31 this.externalSourceName = options?.externalSourceName; 32 this.program = options?.program; 33 } 34 35 indentation = 0; 36 37 withIndentation<T>(exec: () => T) { 38 this.indentation++; 39 const result = exec(); 40 this.indentation--; 41 return result; 42 } 43 44 abstract visitor(node: arkts.AstNode): arkts.AstNode; 45 46 reset(): void { 47 this.indentation = 0; 48 } 49 50 visitEachChild(node: arkts.AstNode): arkts.AstNode { 51 return this.withIndentation(() => arkts.visitEachChild(node, (it) => this.visitor(it))); 52 } 53} 54