1/* 2 * Copyright (c) 2023 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 { SyntaxKind } from 'typescript'; 17import { getClassNameSet } from '../common/commonUtils'; 18import type { StatementEntity } from '../declaration-node/variableStatementResolve'; 19 20/** 21 * generate const variable statement 22 * @param statementEntity 23 * @returns 24 */ 25export function generateVariableStatementDelcatation(statementEntity: StatementEntity, isInnerModule: boolean): string { 26 let statementBody = ''; 27 if (isInnerModule) { 28 statementBody = `const ${statementEntity.statementName} = `; 29 } else { 30 statementBody = `${statementEntity.statementName}: `; 31 } 32 let statementValue: string; 33 if (statementEntity.typeKind === SyntaxKind.StringKeyword) { 34 statementValue = '\'\''; 35 } else if (statementEntity.typeKind === SyntaxKind.LiteralType || statementEntity.typeKind === SyntaxKind.StringLiteral || 36 statementEntity.typeKind === SyntaxKind.NumericLiteral) { 37 statementValue = judgmentStatementEntity(statementEntity, statementValue); 38 } else if (statementEntity.typeKind === SyntaxKind.NumberKeyword) { 39 statementValue = '0'; 40 } else if (statementEntity.typeKind === SyntaxKind.UnionType) { 41 statementValue = statementEntity.typeName.split('|')[0]; 42 } else if (statementEntity.typeKind === SyntaxKind.TypeReference) { 43 statementValue = judgmentStatementEntityTypeName(statementEntity, statementValue); 44 } else if (statementEntity.typeKind === SyntaxKind.BooleanKeyword) { 45 statementValue = 'true'; 46 } else if (statementEntity.initializer !== '') { 47 statementValue = statementEntity.initializer.endsWith('n').toString(); 48 } else { 49 statementValue = `'[PC Preivew] unknown ${statementEntity.statementName}'`; 50 } 51 statementBody += statementValue; 52 if (isInnerModule) { 53 statementBody += ';'; 54 } else { 55 statementBody += ','; 56 } 57 return statementBody; 58} 59 60function judgmentStatementEntity(statementEntity: StatementEntity, statementValue: string): string { 61 if (statementEntity.initializer === '') { 62 if (statementEntity.typeName.endsWith('n')) { 63 statementValue = statementEntity.typeName.replace('n', ''); 64 } else { 65 statementValue = statementEntity.typeName; 66 } 67 } else { 68 statementValue = statementEntity.initializer; 69 } 70 return statementValue; 71} 72function judgmentStatementEntityTypeName(statementEntity: StatementEntity, statementValue: string): string { 73 if (statementEntity.typeName.includes('<')) { 74 const tmpTypeName = statementEntity.typeName.split('<')[0]; 75 if (getClassNameSet().has(tmpTypeName)) { 76 statementValue = `new ${tmpTypeName}()`; 77 } else { 78 statementValue = `${tmpTypeName}`; 79 } 80 } else { 81 statementValue = statementEntity.typeName; 82 } 83 return statementValue; 84} 85