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'; 17import { RuntimeNames, PositionalIdTracker } from './utils'; 18import { AbstractVisitor, VisitorOptions } from '../common/abstract-visitor'; 19 20export interface InternalsTransformerOptions extends VisitorOptions { 21 positionalIdTracker: PositionalIdTracker; 22} 23 24export class InternalsTransformer extends AbstractVisitor { 25 private readonly positionalIdTracker: PositionalIdTracker; 26 27 constructor(options: InternalsTransformerOptions) { 28 super(options); 29 this.positionalIdTracker = options.positionalIdTracker; 30 } 31 32 visitor(beforeChildren: arkts.AstNode): arkts.AstNode { 33 const node = this.visitEachChild(beforeChildren); 34 if (arkts.isCallExpression(node)) { 35 if (arkts.isIdentifier(node.expression)) { 36 if (node.expression.name === RuntimeNames.__CONTEXT) { 37 return arkts.factory.createIdentifier(RuntimeNames.CONTEXT, undefined); 38 } 39 if (node.expression.name === RuntimeNames.__ID) { 40 return arkts.factory.createIdentifier(RuntimeNames.ID, undefined); 41 } 42 if (node.expression.name === RuntimeNames.__KEY) { 43 return this.positionalIdTracker.id(RuntimeNames.__KEY); 44 } 45 } 46 } 47 return node; 48 } 49}