* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as arkts from '@koalaui/libarkts';
import { backingField, expectName, flatVisitMethodWithOverloads } from '../../common/arkts-utils';
import { DecoratorNames, GetSetTypes, NodeCacheNames, StateManagementTypes } from '../../common/predefines';
import {
createGetter,
generateThisBacking,
generateGetOrSetCall,
hasDecorator,
collectStateManagementTypeImport,
findCachedMemoMetadata,
checkIsNameStartWithBackingField,
checkIsPropertyCanBeNonNull,
} from './utils';
import {
BasePropertyTranslator,
InnerClassPropertyCachedTranslator,
InnerClassPropertyTranslator,
InnerClassPropertyTypes,
PropertyCachedTranslator,
PropertyCachedTranslatorOptions,
PropertyTranslator,
PropertyTranslatorOptions,
} from './base';
import { factory } from './factory';
import { PropertyCache } from './cache/propertyCache';
import { CustomComponentInnerClassPropertyInfo } from '../../collectors/ui-collectors/records';
import { PropertyValueCache } from '../memo-collect-cache';
function resetOnReuseWithParamProperty(
this: BasePropertyTranslator,
newName: string,
originalName: string,
metadata?: arkts.AstNodeCacheValueMetadata
): arkts.ExpressionStatement {
const propertyValue = this.property.value?.clone();
const propertyType = this.propertyType?.clone();
const arg = factory.generateInitializeValue(
propertyValue,
propertyType,
originalName,
this.initializeOptions,
this.isMemoCached,
metadata
);
if (this.isMemoShouldUpdate) {
if (!!propertyValue) {
const isFunctionValue = arkts.isArrowFunctionExpression(propertyValue);
PropertyValueCache.getInstance().collect({ value: propertyValue, shouldCache: this.isMemoCached && isFunctionValue, metadata });
}
if (!!propertyType) {
PropertyValueCache.getInstance().collect({ value: propertyType });
}
}
return factory.createResetOnReuseStmt(newName, arg);
}
* @deprecated
*/
export class ParamTranslator extends PropertyTranslator {
protected stateManagementType: StateManagementTypes = StateManagementTypes.PARAM_DECORATED;
protected makeType: StateManagementTypes = StateManagementTypes.MAKE_PARAM;
protected shouldWrapPropertyType: boolean = true;
protected hasInitializeStruct: boolean = true;
protected hasUpdateStruct: boolean = true;
protected hasToRecord: boolean = false;
protected hasField: boolean = true;
protected hasGetter: boolean = true;
protected hasSetter: boolean = false;
protected hasResetOnReuse: boolean = true;
constructor(options: PropertyTranslatorOptions) {
super(options);
const isRequired = hasDecorator(this.property, DecoratorNames.REQUIRE);
this.initializeOptions = {
isRequired,
shouldCheckNonNull: !isRequired
}
}
resetOnReuse(
newName: string,
originalName: string,
metadata?: arkts.AstNodeCacheValueMetadata
): arkts.ExpressionStatement {
return resetOnReuseWithParamProperty.bind(this)(newName, originalName, metadata);
}
}
export class ParamCachedTranslator extends PropertyCachedTranslator {
protected stateManagementType: StateManagementTypes = StateManagementTypes.PARAM_DECORATED;
protected makeType: StateManagementTypes = StateManagementTypes.MAKE_PARAM;
protected shouldWrapPropertyType: boolean = true;
protected hasInitializeStruct: boolean = true;
protected hasUpdateStruct: boolean = true;
protected hasToRecord: boolean = false;
protected hasField: boolean = true;
protected hasGetter: boolean = true;
protected hasSetter: boolean = false;
protected hasResetOnReuse: boolean = true;
constructor(options: PropertyCachedTranslatorOptions) {
super(options);
const isRequired = this.propertyInfo.annotationInfo?.hasRequire;
this.initializeOptions = {
isRequired,
shouldCheckNonNull: !isRequired
}
this.initializeOptions.canDefinitelyBeNonNull = checkIsPropertyCanBeNonNull(
this.property,
this.initializeOptions
);
}
resetOnReuse(
newName: string,
originalName: string,
metadata?: arkts.AstNodeCacheValueMetadata
): arkts.ExpressionStatement {
return resetOnReuseWithParamProperty.bind(this)(newName, originalName, metadata);
}
}
* @deprecated
*/
export class ParamInnerClassTranslator<T extends InnerClassPropertyTypes> extends InnerClassPropertyTranslator<T> {
protected decorator: DecoratorNames = DecoratorNames.PARAM;
* @deprecated
*/
static canBeTranslated(node: arkts.AstNode): node is InnerClassPropertyTypes {
if (arkts.isMethodDefinition(node)) {
return checkIsNameStartWithBackingField(node.id) && hasDecorator(node, DecoratorNames.PARAM);
} else if (arkts.isClassProperty(node)) {
return checkIsNameStartWithBackingField(node.key) && hasDecorator(node, DecoratorNames.PARAM);
}
return false;
}
}
export class ParamCachedInnerClassTranslator<
T extends InnerClassPropertyTypes,
> extends InnerClassPropertyCachedTranslator<T> {
protected decorator: DecoratorNames = DecoratorNames.PARAM;
static canBeTranslated(
node: arkts.AstNode,
metadata?: CustomComponentInnerClassPropertyInfo
): node is InnerClassPropertyTypes {
return !!metadata?.name?.startsWith(StateManagementTypes.BACKING) && !!metadata.annotationInfo?.hasParam;
}
}