• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { ExportAssignment, ExportDeclaration, ImportDeclaration,
17   ImportEqualsDeclaration, isImportDeclaration, Node, SourceFile } from 'typescript';
18
19/**
20 * get current sourceFile all imports
21 * @param sourceFile
22 * @returns
23 */
24export function getImportDeclarationArray(sourceFile: SourceFile): Array<ImportElementEntity> {
25  const importDeclarations: Array<ImportElementEntity> = [];
26  sourceFile.forEachChild(node => {
27    if (isImportDeclaration(node)) {
28      importDeclarations.push(getImportDeclaration(node, sourceFile));
29    }
30  });
31  return importDeclarations;
32}
33
34/**
35 * get module inner import info
36 * @param importEqualNode
37 * @param sourceFile
38 * @returns
39 */
40export function getModuleImportEqual(importEqualNode: ImportEqualsDeclaration, sourceFile: SourceFile): ImportEuqalEntity {
41  return {
42    importEqualName: importEqualNode.name.escapedText.toString(),
43    importEqualTypeName: sourceFile.text.substring(importEqualNode.moduleReference.pos, importEqualNode.moduleReference.end).trimStart().trimEnd(),
44    importEqualTypeKind: importEqualNode.moduleReference.kind
45  };
46}
47
48/**
49 * get export info
50 * @param exportNode
51 * @param sourceFile
52 * @returns
53 */
54export function getExportDeclaration(exportNode: ExportDeclaration, sourceFile: SourceFile): string {
55  return sourceFile.text.substring(exportNode.pos, exportNode.end).trimStart().trimEnd();
56}
57
58/**
59 * get import info
60 * @param node
61 * @param sourceFile
62 * @returns
63 */
64export function getImportDeclaration(node: Node, sourceFile: SourceFile): ImportElementEntity {
65  let importElements = '';
66  const importNode = node as ImportDeclaration;
67  const importPath = sourceFile.text.substring(importNode.moduleSpecifier.pos, importNode.moduleSpecifier.end).trimStart().trimEnd();
68  const importClause = importNode.importClause;
69  if (importClause !== undefined) {
70    importElements = sourceFile.text.substring(importClause.pos, importClause.end).trimStart().trimEnd();
71    if (importElements.startsWith('type ')) {
72      importElements = importElements.replace('type ', '');
73    }
74  }
75
76  return {
77    importPath: importPath,
78    importElements: importElements
79  };
80}
81
82/**
83 * get export info
84 * @param exportAssigment
85 * @param sourceFile
86 * @returns
87 */
88export function getExportAssignment(exportAssigment: ExportAssignment, sourceFile: SourceFile): Array<string> {
89  const exportAssignments: Array<string> = [];
90  if (exportAssigment.expression !== undefined) {
91    exportAssignments.push(sourceFile.text.substring(exportAssigment.expression.pos, exportAssigment.expression.end).trimStart().trimEnd());
92  }
93  return exportAssignments;
94}
95
96export interface ImportElementEntity {
97  importPath: string,
98  importElements: string
99}
100
101export interface ExportElementEntity {
102  exportName: string
103}
104
105export interface ImportEuqalEntity {
106  importEqualName: string,
107  importEqualTypeName: string,
108  importEqualTypeKind: number
109}
110