• 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 { SourceFile } from 'typescript';
17import { InterfaceEntity } from '../declaration-node/interfaceDeclaration';
18import { generateCommonMethodSignature } from './generateCommonMethodSignature';
19import { generateIndexSignature } from './generateIndexSignature';
20import { generatePropertySignatureDeclaration } from './generatePropertySignatureDeclaration';
21
22/**
23 * generate interface
24 * @param rootName
25 * @param interfaceEntity
26 * @param sourceFile
27 * @param isSourceFile
28 * @returns
29 */
30export function generateInterfaceDeclaration(rootName: string, interfaceEntity: InterfaceEntity, sourceFile: SourceFile, isSourceFile: boolean,
31  currentSourceInterfaceArray: InterfaceEntity[]): string {
32  const interfaceName = interfaceEntity.interfaceName;
33  let interfaceBody = '';
34  const interfaceElementSet = new Set<string>();
35  if (interfaceEntity.exportModifiers.length > 0 || isSourceFile) {
36    interfaceBody += `export const ${interfaceName} = { \n`;
37  } else {
38    interfaceBody += `const ${interfaceName} = { \n`;
39  }
40
41  if (interfaceEntity.interfacePropertySignatures.length > 0) {
42    interfaceEntity.interfacePropertySignatures.forEach(value => {
43      interfaceBody += generatePropertySignatureDeclaration(interfaceName, value, sourceFile) + '\n';
44      interfaceElementSet.add(value.propertyName);
45    });
46  }
47
48  if (interfaceEntity.interfaceMethodSignature.size > 0) {
49    interfaceEntity.interfaceMethodSignature.forEach(value => {
50      interfaceBody += generateCommonMethodSignature(interfaceName, value, sourceFile) + '\n';
51      interfaceElementSet.add(value[0].functionName);
52    });
53  }
54
55  if (interfaceEntity.indexSignature.length > 0) {
56    interfaceEntity.indexSignature.forEach(value => {
57      interfaceBody += generateIndexSignature(value) + '\n';
58      interfaceElementSet.add(value.indexSignatureKey);
59    });
60  }
61
62  if (interfaceEntity.heritageClauses.length > 0) {
63    interfaceEntity.heritageClauses.forEach(value => {
64      currentSourceInterfaceArray.forEach(currentInterface => {
65        if (value.types.includes(currentInterface.interfaceName)) {
66          interfaceBody += generateHeritageInterface(currentInterface, sourceFile, interfaceElementSet);
67        }
68      });
69    });
70  }
71
72  interfaceBody += '}';
73  return interfaceBody;
74}
75
76function generateHeritageInterface(interfaceEntity: InterfaceEntity, sourceFile: SourceFile, elements: Set<string>): string {
77  const interfaceName = interfaceEntity.interfaceName;
78  let interfaceBody = '';
79  if (interfaceEntity.interfacePropertySignatures.length > 0) {
80    interfaceEntity.interfacePropertySignatures.forEach(value => {
81      if (!elements.has(value.propertyName)) {
82        interfaceBody += generatePropertySignatureDeclaration(interfaceName, value, sourceFile) + '\n';
83      }
84    });
85  }
86
87  if (interfaceEntity.interfaceMethodSignature.size > 0) {
88    interfaceEntity.interfaceMethodSignature.forEach(value => {
89      if (!elements.has(value[0].functionName)) {
90        interfaceBody += generateCommonMethodSignature(interfaceName, value, sourceFile) + '\n';
91      }
92    });
93  }
94
95  if (interfaceEntity.indexSignature.length > 0) {
96    interfaceEntity.indexSignature.forEach(value => {
97      if (elements.has(value.indexSignatureKey)) {
98        interfaceBody += generateIndexSignature(value) + '\n';
99      }
100    });
101  }
102  return interfaceBody;
103}
104