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 { isIdentifier, SourceFile, VariableStatement } from 'typescript'; 17 18/** 19 * get const declaration variable 20 * @param variableStatement 21 * @param sourceFile 22 * @returns 23 */ 24export function getVariableStatementDeclaration(variableStatement: VariableStatement, sourceFile: SourceFile): Array<StatementEntity> { 25 const statementsArray: Array<StatementEntity> = []; 26 variableStatement.declarationList.declarations.forEach(value => { 27 let statementName = ''; 28 let initializer = ''; 29 let typeName = ''; 30 let typeKind = -1; 31 32 if (isIdentifier(value.name)) { 33 statementName = value.name.escapedText.toString(); 34 } else { 35 statementName = sourceFile.text.substring(value.pos, value.end).trimStart().trimEnd(); 36 } 37 if (value.initializer !== undefined) { 38 initializer = sourceFile.text.substring(value.initializer.pos, value.initializer.end); 39 } 40 if (value.type !== undefined) { 41 typeName = sourceFile.text.substring(value.type.pos, value.type.end); 42 typeKind = value.type.kind; 43 } 44 statementsArray.push({ statementName: statementName, typeName: typeName, typeKind: typeKind, initializer: initializer }); 45 }); 46 return statementsArray; 47} 48 49export interface StatementEntity { 50 statementName: string, 51 typeName: string, 52 typeKind: number, 53 initializer: string 54} 55