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