• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
43import { isArktsOne } from '../common/commonUtils';
44
45/**
46 * 解析所有 sourceFile 节点信息
47 * @param sourceFile sourceFile对象
48 * @param mockBuffer 当前文件mock信息
49 * @param members 文件内容成员对象
50 * @param parent 文件根节点
51 * @returns
52 */
53export function getSourceFileAssembly(sourceFile: SourceFile, mockBuffer: MockBuffer, members: Members, parent: KeyValue): void {
54  let defaultExportNode: ts.ExportAssignment;
55  sourceFile.forEachChild(node => {
56    if (!isArktsOne(node, sourceFile)) {
57      return;
58    }
59    if (isImportDeclaration(node)) {
60      handleImportDeclaration(node, mockBuffer, members, parent, KeyValueTypes.IMPORT);
61    } else if (isModuleDeclaration(node)) {
62      handleModuleDeclaration(sourceFile, node, mockBuffer, members, parent, KeyValueTypes.MODULE);
63    } else if (isTypeAliasDeclaration(node)) {
64      handleTypeAliasDeclaration(node, mockBuffer, members, parent, KeyValueTypes.VARIABLE);
65    } else if (isClassDeclaration(node)) {
66      handleClassDeclaration(sourceFile, node, mockBuffer, members, parent, KeyValueTypes.CLASS);
67    } else if (isInterfaceDeclaration(node)) {
68      handleInterfaceDeclaration(sourceFile, node, mockBuffer, members, parent, KeyValueTypes.INTERFACE);
69    } else if (isEnumDeclaration(node)) {
70      handleEnumDeclaration(sourceFile, node, mockBuffer, members, parent, KeyValueTypes.ENUM);
71    } else if (isFunctionDeclaration(node)) {
72      handleFunctionDeclaration(node, mockBuffer, members, parent, KeyValueTypes.FUNCTION);
73    } else if (isExportAssignment(node)) {
74      defaultExportNode = node;
75    } else if (isExportDeclaration(node)) {
76      handleExportDeclaration(node, mockBuffer, members, parent, KeyValueTypes.IMPORT);
77    } else if (isVariableStatement(node)) {
78      handleVariableStatement(node, mockBuffer, members, parent, KeyValueTypes.VARIABLE);
79    }
80  });
81  defaultExportNode && handleExportAssignment(defaultExportNode, mockBuffer, members, parent, KeyValueTypes.EXPORT);
82}
83