1/* 2 * Copyright (c) 2022 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 { SourceFile, SyntaxKind } from 'typescript'; 17import { firstCharacterToUppercase } from '../common/commonUtils'; 18import { ClassEntity } from '../declaration-node/classDeclaration'; 19import { generateCommonMethod } from './generateCommonMethod'; 20import { getWarnConsole } from './generateCommonUtil'; 21import { generatePropertyDeclaration } from './generatePropertyDeclaration'; 22import { generateStaticFunction } from './generateStaticFunction'; 23 24/** 25 * generate class 26 * @param rootName 27 * @param classEntity 28 * @param isSystem 29 * @param globalName 30 * @param filename 31 * @param sourceFile 32 * @param isInnerMockFunction 33 * @returns 34 */ 35export function generateClassDeclaration(rootName: string, classEntity: ClassEntity, isSystem: boolean, globalName: string, 36 filename: string, sourceFile: SourceFile, isInnerMockFunction: boolean): string { 37 if (isSystem) { 38 return ''; 39 } 40 41 const className = firstCharacterToUppercase(classEntity.className); 42 let classBody = ''; 43 if (classEntity.exportModifiers.includes(SyntaxKind.ExportKeyword) && !isInnerMockFunction) { 44 classBody += `export const ${className} = class ${className} `; 45 } else { 46 classBody += `const ${className} = class ${className} `; 47 } 48 49 let isExtend = false; 50 51 if (classEntity.heritageClauses.length > 0) { 52 classEntity.heritageClauses.forEach(value => { 53 if (value.clauseToken === 'extends') { 54 isExtend = true; 55 classBody += `${value.clauseToken} `; 56 value.types.forEach((val, index) => { 57 if (index !== value.types.length - 1) { 58 classBody += `${val},`; 59 } else { 60 classBody += `${val}`; 61 } 62 }); 63 } 64 }); 65 } 66 67 if (!isSystem) { 68 classBody += '{'; 69 if (classEntity.classConstructor.length > 1) { 70 classBody += `constructor(...arg) { `; 71 } else { 72 classBody += `constructor() { `; 73 } 74 if (isExtend) { 75 classBody += `super();`; 76 } 77 classBody += getWarnConsole(className, 'constructor'); 78 } 79 if (classEntity.classProperty.length > 0) { 80 classEntity.classProperty.forEach(value => { 81 classBody += generatePropertyDeclaration(className, value, sourceFile) + '\n'; 82 }); 83 } 84 85 if (classEntity.classMethod.size > 0) { 86 classEntity.classMethod.forEach(value => { 87 classBody += generateCommonMethod(className, value, sourceFile); 88 }); 89 } 90 91 classBody += '}\n};'; 92 if (!filename.startsWith('system_')) { 93 if (classEntity.staticMethods.length > 0) { 94 let staticMethodBody = ''; 95 classEntity.staticMethods.forEach(value => { 96 staticMethodBody += generateStaticFunction(value, false, sourceFile) + '\n'; 97 }); 98 classBody += staticMethodBody; 99 } 100 } 101 return classBody; 102} 103