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 ts, { 17 isClassDeclaration, 18 isEnumDeclaration, 19 isExportAssignment, 20 isExportDeclaration, 21 isFunctionDeclaration, 22 isImportDeclaration, 23 isInterfaceDeclaration, 24 isModuleDeclaration, 25 isTypeAliasDeclaration, 26 isVariableStatement, 27 SourceFile 28} from 'typescript'; 29import { KeyValue, Members, MockBuffer } from '../types'; 30import { KeyValueTypes } from '../common/constants'; 31import { 32 handleClassDeclaration, 33 handleEnumDeclaration, 34 handleExportAssignment, 35 handleExportDeclaration, 36 handleFunctionDeclaration, 37 handleImportDeclaration, 38 handleInterfaceDeclaration, 39 handleModuleDeclaration, 40 handleTypeAliasDeclaration, 41 handleVariableStatement 42} from '../common/tsNodeUtils'; 43 44/** 45 * 解析所有 sourceFile 节点信息 46 * @param sourceFile sourceFile对象 47 * @param mockBuffer 当前文件mock信息 48 * @param members 文件内容成员对象 49 * @param parent 文件根节点 50 * @returns 51 */ 52export function getSourceFileAssembly(sourceFile: SourceFile, mockBuffer: MockBuffer, members: Members, parent: KeyValue): void { 53 let defaultExportNode: ts.ExportAssignment; 54 sourceFile.forEachChild(node => { 55 if (isImportDeclaration(node)) { 56 handleImportDeclaration(node, mockBuffer, members, parent, KeyValueTypes.IMPORT); 57 } else if (isModuleDeclaration(node)) { 58 handleModuleDeclaration(node, mockBuffer, members, parent, KeyValueTypes.MODULE); 59 } else if (isTypeAliasDeclaration(node)) { 60 handleTypeAliasDeclaration(node, mockBuffer, members, parent, KeyValueTypes.VARIABLE); 61 } else if (isClassDeclaration(node)) { 62 handleClassDeclaration(node, mockBuffer, members, parent, KeyValueTypes.CLASS); 63 } else if (isInterfaceDeclaration(node)) { 64 handleInterfaceDeclaration(node, mockBuffer, members, parent, KeyValueTypes.INTERFACE); 65 } else if (isEnumDeclaration(node)) { 66 handleEnumDeclaration(node, mockBuffer, members, parent, KeyValueTypes.ENUM); 67 } else if (isFunctionDeclaration(node)) { 68 handleFunctionDeclaration(node, mockBuffer, members, parent, KeyValueTypes.FUNCTION); 69 } else if (isExportAssignment(node)) { 70 defaultExportNode = node; 71 } else if (isExportDeclaration(node)) { 72 handleExportDeclaration(node, mockBuffer, members, parent, KeyValueTypes.IMPORT); 73 } else if (isVariableStatement(node)) { 74 handleVariableStatement(node, mockBuffer, members, parent, KeyValueTypes.VARIABLE); 75 } 76 }); 77 defaultExportNode && handleExportAssignment(defaultExportNode, mockBuffer, members, parent, KeyValueTypes.EXPORT); 78} 79