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 type { ConstructorDeclaration, SourceFile } from 'typescript'; 17import { isIdentifier } from 'typescript'; 18 19/** 20 * get constructors info 21 * @param node 22 * @param sourceFile 23 * @returns 24 */ 25export function getConstructorDeclaration( 26 constructorNode: ConstructorDeclaration, 27 sourceFile: SourceFile 28): Array<ConstructorEntity> { 29 const constructors: Array<ConstructorEntity> = []; 30 const fileText = sourceFile.getFullText(); 31 constructorNode.parameters.forEach(value => { 32 const paramElement = value.name; 33 let name = ''; 34 let typeName = ''; 35 let typeKind = -1; 36 if (isIdentifier(paramElement)) { 37 name = paramElement.escapedText.toString(); 38 } else { 39 name = fileText.slice(paramElement.pos, paramElement.end).trim(); 40 } 41 42 const paramTypeElement = value.type; 43 if (paramTypeElement !== undefined) { 44 typeName = fileText.slice(paramTypeElement.pos, paramTypeElement.end).trim(); 45 typeKind = paramTypeElement.kind; 46 } 47 48 constructors.push({ 49 name: name, 50 typeName: typeName, 51 typeKind: typeKind 52 }); 53 }); 54 return constructors; 55} 56 57export interface ConstructorEntity { 58 name: string; 59 typeName: string; 60 typeKind: number; 61} 62