17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ciimport {
177c804472Sopenharmony_ci  isClassDeclaration,
187c804472Sopenharmony_ci  isEnumDeclaration,
197c804472Sopenharmony_ci  isExportDeclaration,
207c804472Sopenharmony_ci  isFunctionDeclaration,
217c804472Sopenharmony_ci  isIdentifier,
227c804472Sopenharmony_ci  isImportEqualsDeclaration,
237c804472Sopenharmony_ci  isInterfaceDeclaration,
247c804472Sopenharmony_ci  isModuleBlock,
257c804472Sopenharmony_ci  isModuleDeclaration,
267c804472Sopenharmony_ci  isTypeAliasDeclaration,
277c804472Sopenharmony_ci  isVariableStatement
287c804472Sopenharmony_ci} from 'typescript';
297c804472Sopenharmony_ciimport type { ModuleDeclaration, Node, SourceFile, ModuleBody } from 'typescript';
307c804472Sopenharmony_ciimport { getExportKeyword } from '../common/commonUtils';
317c804472Sopenharmony_ciimport { getClassDeclaration } from './classDeclaration';
327c804472Sopenharmony_ciimport type { ClassEntity } from './classDeclaration';
337c804472Sopenharmony_ciimport { getEnumDeclaration } from './enumDeclaration';
347c804472Sopenharmony_ciimport type { EnumEntity } from './enumDeclaration';
357c804472Sopenharmony_ciimport { getFunctionDeclaration } from './functionDeclaration';
367c804472Sopenharmony_ciimport type { FunctionEntity } from './functionDeclaration';
377c804472Sopenharmony_ciimport { getExportDeclaration, getModuleImportEqual } from './importAndExportDeclaration';
387c804472Sopenharmony_ciimport type { ImportEuqalEntity } from './importAndExportDeclaration';
397c804472Sopenharmony_ciimport { getInterfaceDeclaration } from './interfaceDeclaration';
407c804472Sopenharmony_ciimport type { InterfaceEntity } from './interfaceDeclaration';
417c804472Sopenharmony_ciimport { getTypeAliasDeclaration } from './typeAliasDeclaration';
427c804472Sopenharmony_ciimport type { TypeAliasEntity } from './typeAliasDeclaration';
437c804472Sopenharmony_ciimport { getVariableStatementDeclaration } from './variableStatementResolve';
447c804472Sopenharmony_ciimport type { StatementEntity } from './variableStatementResolve';
457c804472Sopenharmony_ci
467c804472Sopenharmony_ciinterface SubstepGetModuleparam {
477c804472Sopenharmony_ci  typeAliasDeclarations: Array<TypeAliasEntity>;
487c804472Sopenharmony_ci  classDeclarations: Array<ClassEntity>;
497c804472Sopenharmony_ci  interfaceDeclarations: Array<InterfaceEntity>;
507c804472Sopenharmony_ci  functionDeclarations: Map<string, Array<FunctionEntity>>;
517c804472Sopenharmony_ci  enumDeclarations: Array<EnumEntity>;
527c804472Sopenharmony_ci  moduleDeclarations: Array<ModuleBlockEntity>;
537c804472Sopenharmony_ci  variableStatements: Array<Array<StatementEntity>>;
547c804472Sopenharmony_ci  moduleImportEquaqls: Array<ImportEuqalEntity>;
557c804472Sopenharmony_ci  exportDeclarations: Array<string>;
567c804472Sopenharmony_ci  sourceFile: SourceFile;
577c804472Sopenharmony_ci  fileName: string;
587c804472Sopenharmony_ci  moduleBody: ModuleBody;
597c804472Sopenharmony_ci}
607c804472Sopenharmony_ci
617c804472Sopenharmony_ciexport interface ModuleBlockEntity {
627c804472Sopenharmony_ci  moduleName: string;
637c804472Sopenharmony_ci  exportModifiers: Array<number>;
647c804472Sopenharmony_ci  typeAliasDeclarations: Array<TypeAliasEntity>;
657c804472Sopenharmony_ci  classDeclarations: Array<ClassEntity>;
667c804472Sopenharmony_ci  interfaceDeclarations: Array<InterfaceEntity>;
677c804472Sopenharmony_ci  functionDeclarations: Map<string, Array<FunctionEntity>>;
687c804472Sopenharmony_ci  enumDeclarations: Array<EnumEntity>;
697c804472Sopenharmony_ci  moduleDeclarations: Array<ModuleBlockEntity>;
707c804472Sopenharmony_ci  variableStatements: Array<Array<StatementEntity>>;
717c804472Sopenharmony_ci  moduleImportEquaqls: Array<ImportEuqalEntity>;
727c804472Sopenharmony_ci  exportDeclarations: Array<string>;
737c804472Sopenharmony_ci}
747c804472Sopenharmony_ci
757c804472Sopenharmony_ci/**
767c804472Sopenharmony_ci * get module info
777c804472Sopenharmony_ci * @param node
787c804472Sopenharmony_ci * @param sourceFile
797c804472Sopenharmony_ci * @param fileName
807c804472Sopenharmony_ci * @returns
817c804472Sopenharmony_ci */
827c804472Sopenharmony_ciexport function getModuleDeclaration(node: Node, sourceFile: SourceFile, fileName: string): ModuleBlockEntity {
837c804472Sopenharmony_ci  const moduleNode = node as ModuleDeclaration;
847c804472Sopenharmony_ci  const typeAliasDeclarations: Array<TypeAliasEntity> = [];
857c804472Sopenharmony_ci  const classDeclarations: Array<ClassEntity> = [];
867c804472Sopenharmony_ci  const interfaceDeclarations: Array<InterfaceEntity> = [];
877c804472Sopenharmony_ci  const functionDeclarations: Map<string, Array<FunctionEntity>> = new Map<string, Array<FunctionEntity>>();
887c804472Sopenharmony_ci  const enumDeclarations: Array<EnumEntity> = [];
897c804472Sopenharmony_ci  const moduleDeclarations: Array<ModuleBlockEntity> = [];
907c804472Sopenharmony_ci  const variableStatements: Array<Array<StatementEntity>> = [];
917c804472Sopenharmony_ci  const moduleImportEquaqls: Array<ImportEuqalEntity> = [];
927c804472Sopenharmony_ci  const exportDeclarations: Array<string> = [];
937c804472Sopenharmony_ci  const moduleBody = moduleNode.body;
947c804472Sopenharmony_ci  let moduleName = '';
957c804472Sopenharmony_ci  if (isIdentifier(moduleNode.name)) {
967c804472Sopenharmony_ci    moduleName = moduleNode.name.escapedText.toString();
977c804472Sopenharmony_ci  } else {
987c804472Sopenharmony_ci    moduleName = sourceFile.text.substring(moduleNode.name.pos, moduleNode.name.end).trim();
997c804472Sopenharmony_ci  }
1007c804472Sopenharmony_ci
1017c804472Sopenharmony_ci  let exportModifiers: Array<number> = [];
1027c804472Sopenharmony_ci  const modifiers = moduleNode.modifiers;
1037c804472Sopenharmony_ci  if (modifiers !== undefined) {
1047c804472Sopenharmony_ci    exportModifiers = getExportKeyword(modifiers);
1057c804472Sopenharmony_ci  }
1067c804472Sopenharmony_ci  const SubstepModuleBlockEntitys: SubstepGetModuleparam = substepModule({
1077c804472Sopenharmony_ci    typeAliasDeclarations,
1087c804472Sopenharmony_ci    classDeclarations,
1097c804472Sopenharmony_ci    interfaceDeclarations,
1107c804472Sopenharmony_ci    functionDeclarations,
1117c804472Sopenharmony_ci    enumDeclarations,
1127c804472Sopenharmony_ci    moduleDeclarations,
1137c804472Sopenharmony_ci    variableStatements,
1147c804472Sopenharmony_ci    moduleImportEquaqls,
1157c804472Sopenharmony_ci    exportDeclarations,
1167c804472Sopenharmony_ci    moduleBody,
1177c804472Sopenharmony_ci    sourceFile,
1187c804472Sopenharmony_ci    fileName
1197c804472Sopenharmony_ci  });
1207c804472Sopenharmony_ci  delete SubstepModuleBlockEntitys.moduleBody;
1217c804472Sopenharmony_ci  delete SubstepModuleBlockEntitys.sourceFile;
1227c804472Sopenharmony_ci  delete SubstepModuleBlockEntitys.fileName;
1237c804472Sopenharmony_ci  return {
1247c804472Sopenharmony_ci    ...SubstepModuleBlockEntitys,
1257c804472Sopenharmony_ci    exportModifiers,
1267c804472Sopenharmony_ci    moduleName
1277c804472Sopenharmony_ci  };
1287c804472Sopenharmony_ci}
1297c804472Sopenharmony_ci
1307c804472Sopenharmony_ci/**
1317c804472Sopenharmony_ci * get some module info
1327c804472Sopenharmony_ci * @param SubstepGetModuleparam
1337c804472Sopenharmony_ci * @returns
1347c804472Sopenharmony_ci */
1357c804472Sopenharmony_cifunction substepModule(props: SubstepGetModuleparam): SubstepGetModuleparam {
1367c804472Sopenharmony_ci  if (props.moduleBody !== undefined && isModuleBlock(props.moduleBody)) {
1377c804472Sopenharmony_ci    props.moduleBody.statements.forEach(value => {
1387c804472Sopenharmony_ci      if (isFunctionDeclaration(value)) {
1397c804472Sopenharmony_ci        const functionEntity = getFunctionDeclaration(value, props.sourceFile);
1407c804472Sopenharmony_ci        if (props.functionDeclarations.get(functionEntity.functionName) !== undefined) {
1417c804472Sopenharmony_ci          props.functionDeclarations.get(functionEntity.functionName)?.push(functionEntity);
1427c804472Sopenharmony_ci        } else {
1437c804472Sopenharmony_ci          const functionArray: Array<FunctionEntity> = [];
1447c804472Sopenharmony_ci          functionArray.push(functionEntity);
1457c804472Sopenharmony_ci          props.functionDeclarations.set(functionEntity.functionName, functionArray);
1467c804472Sopenharmony_ci        }
1477c804472Sopenharmony_ci      } else if (isTypeAliasDeclaration(value)) {
1487c804472Sopenharmony_ci        props.typeAliasDeclarations.push(getTypeAliasDeclaration(value, props.sourceFile));
1497c804472Sopenharmony_ci      } else if (isEnumDeclaration(value)) {
1507c804472Sopenharmony_ci        props.enumDeclarations.push(getEnumDeclaration(value, props.sourceFile));
1517c804472Sopenharmony_ci      } else if (isClassDeclaration(value)) {
1527c804472Sopenharmony_ci        props.classDeclarations.push(getClassDeclaration(value, props.sourceFile));
1537c804472Sopenharmony_ci      } else if (isInterfaceDeclaration(value)) {
1547c804472Sopenharmony_ci        props.interfaceDeclarations.push(getInterfaceDeclaration(value, props.sourceFile));
1557c804472Sopenharmony_ci      } else if (isModuleDeclaration(value)) {
1567c804472Sopenharmony_ci        props.moduleDeclarations.push(getModuleDeclaration(value, props.sourceFile, props.fileName));
1577c804472Sopenharmony_ci      } else if (isVariableStatement(value)) {
1587c804472Sopenharmony_ci        props.variableStatements.push(getVariableStatementDeclaration(value, props.sourceFile));
1597c804472Sopenharmony_ci      } else if (isImportEqualsDeclaration(value)) {
1607c804472Sopenharmony_ci        props.moduleImportEquaqls.push(getModuleImportEqual(value, props.sourceFile));
1617c804472Sopenharmony_ci      } else if (isExportDeclaration(value)) {
1627c804472Sopenharmony_ci        props.exportDeclarations.push(getExportDeclaration(value, props.sourceFile));
1637c804472Sopenharmony_ci      } else {
1647c804472Sopenharmony_ci        console.log('--------------------------- uncaught module type start -----------------------');
1657c804472Sopenharmony_ci        console.log('fileName: ' + props.fileName);
1667c804472Sopenharmony_ci        console.log(value);
1677c804472Sopenharmony_ci        console.log('--------------------------- uncaught module type end -----------------------');
1687c804472Sopenharmony_ci      }
1697c804472Sopenharmony_ci    });
1707c804472Sopenharmony_ci  }
1717c804472Sopenharmony_ci  return props;
1727c804472Sopenharmony_ci}
173