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'; 17 18import { 19 createGetter, 20 createSetter, 21 generateThisBackingValue, 22 generateThisBacking, 23 getValueInAnnotation, 24 DecoratorNames, 25} from './utils'; 26import { PropertyTranslator } from './base'; 27import { GetterSetter, InitializerConstructor } from './types'; 28import { backingField, expectName } from '../../common/arkts-utils'; 29import { createOptionalClassProperty } from '../utils'; 30import { factory } from './factory'; 31 32export class BuilderParamTranslator extends PropertyTranslator implements InitializerConstructor, GetterSetter { 33 translateMember(): arkts.AstNode[] { 34 const originalName: string = expectName(this.property.key); 35 const newName: string = backingField(originalName); 36 this.cacheTranslatedInitializer(newName, originalName); // TODO: need to release cache after some point... 37 return this.translateWithoutInitializer(newName, originalName); 38 } 39 40 cacheTranslatedInitializer(newName: string, originalName: string): void { 41 const currentStructInfo: arkts.StructInfo = arkts.GlobalInfo.getInfoInstance().getStructInfo(this.structName); 42 const mutableThis: arkts.Expression = generateThisBacking(newName); 43 const initializeStruct: arkts.AstNode = this.generateInitializeStruct(mutableThis, originalName); 44 currentStructInfo.initializeBody.push(initializeStruct); 45 arkts.GlobalInfo.getInfoInstance().setStructInfo(this.structName, currentStructInfo); 46 } 47 48 translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[] { 49 const field: arkts.ClassProperty = createOptionalClassProperty(newName, this.property, '', 50 arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE, true); 51 const thisGetValue: arkts.Expression = generateThisBacking(newName, false, true); 52 const thisSetValue: arkts.Expression = generateThisBacking(newName, false, false); 53 const getter: arkts.MethodDefinition = this.translateGetter( 54 originalName, 55 this.property.typeAnnotation, 56 thisGetValue 57 ); 58 const setter: arkts.MethodDefinition = this.translateSetter( 59 originalName, 60 this.property.typeAnnotation, 61 thisSetValue 62 ); 63 64 return [field, getter, setter]; 65 } 66 67 translateGetter( 68 originalName: string, 69 typeAnnotation: arkts.TypeNode | undefined, 70 returnValue: arkts.Expression 71 ): arkts.MethodDefinition { 72 return createGetter(originalName, typeAnnotation, returnValue, true); 73 } 74 75 translateSetter( 76 originalName: string, 77 typeAnnotation: arkts.TypeNode | undefined, 78 left: arkts.Expression 79 ): arkts.MethodDefinition { 80 const right: arkts.Identifier = arkts.factory.createIdentifier('value'); 81 return createSetter(originalName, typeAnnotation, left, right, true); 82 } 83 84 generateInitializeStruct(mutableThis: arkts.Expression, originalName: string): arkts.AstNode { 85 return arkts.factory.createAssignmentExpression( 86 mutableThis, 87 arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, 88 arkts.factory.createBinaryExpression( 89 arkts.factory.createBinaryExpression( 90 factory.createBlockStatementForOptionalExpression( 91 arkts.factory.createIdentifier('initializers'), 92 originalName 93 ), 94 arkts.factory.createIdentifier('content'), 95 arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NULLISH_COALESCING 96 ), 97 this.property.value ?? arkts.factory.createUndefinedLiteral(), 98 arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NULLISH_COALESCING 99 ) 100 ); 101 } 102} 103