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 { 17 InterfaceDeclaration, isCallSignatureDeclaration, isConstructSignatureDeclaration, 18 isHeritageClause, isIndexSignatureDeclaration, isMethodSignature, isPropertySignature, 19 isTypeParameterDeclaration, SourceFile 20} from 'typescript'; 21import { getExportKeyword } from '../common/commonUtils'; 22import { CallSignatureEntity, getCallSignatureDeclaration } from './callSignatureDeclaration'; 23import { ConstructorEntity, getConstructorDeclaration } from './constructorDeclaration'; 24import { getHeritageClauseDeclaration, HeritageClauseEntity } from './heritageClauseDeclaration'; 25import { getIndexSignatureDeclaration, IndexSignatureEntity } from './indexSignatureDeclaration'; 26import { getMethodSignatureDeclaration, MethodSignatureEntity } from './methodSignatureDeclaration'; 27import { getPropertySignatureDeclaration, PropertySignatureEntity } from './propertySignatureDeclaration'; 28import { getTypeParameterDeclaration, TypeParameterEntity } from './typeParameterDeclaration'; 29 30/** 31 * get interface info 32 * @param interfaceNode 33 * @param sourceFile 34 * @returns 35 */ 36export function getInterfaceDeclaration(interfaceNode: InterfaceDeclaration, sourceFile: SourceFile): InterfaceEntity { 37 let exportModifiers: Array<number> = []; 38 if (interfaceNode.modifiers !== undefined) { 39 exportModifiers = getExportKeyword(interfaceNode.modifiers); 40 } 41 42 const interfaceName = interfaceNode.name.escapedText.toString(); 43 const heritageClauses: Array<HeritageClauseEntity> = []; 44 const interfaceConstructors: Array<Array<ConstructorEntity>> = []; 45 const interfaceMethodSignature: Map<string, Array<MethodSignatureEntity>> = new Map<string, Array<MethodSignatureEntity>>(); 46 const interfacePropertySignatures: Array<PropertySignatureEntity> = []; 47 const callSignature: Array<CallSignatureEntity> = []; 48 const typeParameters: Array<TypeParameterEntity> = []; 49 const indexSignature: Array<IndexSignatureEntity> = []; 50 51 if (interfaceNode.heritageClauses !== undefined) { 52 interfaceNode.heritageClauses.forEach(value => { 53 heritageClauses.push(getHeritageClauseDeclaration(value, sourceFile)); 54 }); 55 } 56 57 interfaceNode.members.forEach(value => { 58 if (isPropertySignature(value)) { 59 interfacePropertySignatures.push(getPropertySignatureDeclaration(value, sourceFile)); 60 } else if (isMethodSignature(value)) { 61 const methodSignature = getMethodSignatureDeclaration(value, sourceFile); 62 if (interfaceMethodSignature.get(methodSignature.functionName) !== undefined) { 63 interfaceMethodSignature.get(methodSignature.functionName)?.push(methodSignature); 64 } else { 65 const methodSignatureArray: Array<MethodSignatureEntity> = []; 66 methodSignatureArray.push(methodSignature); 67 interfaceMethodSignature.set(methodSignature.functionName, methodSignatureArray); 68 } 69 } else if (isHeritageClause(value)) { 70 heritageClauses.push(getHeritageClauseDeclaration(value, sourceFile)); 71 } else if (isConstructSignatureDeclaration(value)) { 72 interfaceConstructors.push(getConstructorDeclaration(value, sourceFile)); 73 } else if (isCallSignatureDeclaration(value)) { 74 callSignature.push(getCallSignatureDeclaration(value, sourceFile)); 75 } else if (isTypeParameterDeclaration(value)) { 76 typeParameters.push(getTypeParameterDeclaration(value, sourceFile)); 77 } else if (isIndexSignatureDeclaration(value)) { 78 indexSignature.push(getIndexSignatureDeclaration(value, sourceFile)); 79 } else { 80 console.log('--------------------------- uncaught interface type start -----------------------'); 81 console.log('interfaceName: ' + interfaceName); 82 console.log(value); 83 console.log('--------------------------- uncaught interface type end -----------------------'); 84 } 85 }); 86 87 return { 88 interfaceName: interfaceName, 89 typeParameters: typeParameters, 90 heritageClauses: heritageClauses, 91 interfaceConstructors: interfaceConstructors, 92 interfaceMethodSignature: interfaceMethodSignature, 93 interfacePropertySignatures: interfacePropertySignatures, 94 callSignatures: callSignature, 95 exportModifiers: exportModifiers, 96 indexSignature: indexSignature 97 }; 98} 99 100export interface InterfaceEntity { 101 interfaceName: string, 102 typeParameters: Array<TypeParameterEntity>, 103 heritageClauses: Array<HeritageClauseEntity>, 104 interfaceConstructors: Array<Array<ConstructorEntity>>, 105 interfaceMethodSignature: Map<string, Array<MethodSignatureEntity>>, 106 interfacePropertySignatures: Array<PropertySignatureEntity>, 107 callSignatures: Array<CallSignatureEntity>, 108 exportModifiers: Array<number>, 109 indexSignature: Array<IndexSignatureEntity> 110} 111