• 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 { SyntaxKind } from 'typescript';
17import type { SourceFile } from 'typescript';
18import type { PropertySignatureEntity } from '../declaration-node/propertySignatureDeclaration';
19import {
20  checkIsGenericSymbol, getCallbackStatement, getTheRealReferenceFromImport,
21  getWarnConsole, propertyTypeWhiteList
22} from './generateCommonUtil';
23
24/**
25 * generate interface signature property
26 * @param rootName
27 * @param propertySignature
28 * @param sourceFile
29 * @returns
30 */
31export function generatePropertySignatureDeclaration(rootName: string, propertySignature: PropertySignatureEntity, sourceFile: SourceFile, mockApi: string): string {
32  let propertySignatureBody = '';
33  if (propertySignature.kind === SyntaxKind.FunctionType) {
34    propertySignatureBody += `${propertySignature.propertyName}: function(...args) {`;
35    propertySignatureBody += getWarnConsole(rootName, propertySignature.propertyName);
36    propertySignatureBody += getCallbackStatement(mockApi);
37    propertySignatureBody += '},\n';
38  } else {
39    if (
40      (propertySignature.propertyTypeName.startsWith('{') ||
41      propertySignature.propertyTypeName.startsWith('Record<') ||
42      propertySignature.propertyTypeName.startsWith('Object') ||
43      propertySignature.propertyTypeName.startsWith('object')) &&
44      !propertySignature.propertyTypeName.endsWith(']')
45    ) {
46      propertySignatureBody = `${propertySignature.propertyName}: {},`;
47    } else if (propertySignature.kind === SyntaxKind.TypeReference) {
48      if (propertySignature.propertyTypeName.startsWith('Array')) {
49        propertySignatureBody = `${propertySignature.propertyName}: [],`;
50      } else if (propertySignature.propertyTypeName.startsWith('Map')) {
51        propertySignatureBody = `${propertySignature.propertyName}: {key: {}},`;
52      } else if (propertySignature.propertyTypeName === 'string' || checkIsGenericSymbol(propertySignature.propertyTypeName) ||
53        propertySignature.propertyTypeName === 'bool' || propertySignature.propertyTypeName === 'Data') {
54        propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unknown ${propertySignature.propertyName}',`;
55      } else {
56        if (propertySignature.propertyTypeName.includes('<')) {
57          if (propertySignature.propertyTypeName.startsWith('AsyncCallback')) {
58            propertySignatureBody = `${propertySignature.propertyName}: ()=>{},`;
59          } else {
60            const preSplit = propertySignature.propertyTypeName.split('<');
61            const genericArg = preSplit[preSplit.length - 1].split('>')[0];
62            propertySignatureBody = `${propertySignature.propertyName}: ${genericArg},`;
63          }
64        } else {
65          if (propertyTypeWhiteList(propertySignature.propertyTypeName) === propertySignature.propertyTypeName) {
66            propertySignatureBody = `${propertySignature.propertyName}: ${getTheRealReferenceFromImport(sourceFile, propertySignature.propertyTypeName)},`;
67          } else {
68            propertySignatureBody = `${propertySignature.propertyName}: ${propertyTypeWhiteList(propertySignature.propertyTypeName)},`;
69          }
70        }
71      }
72    } else if (propertySignature.kind === SyntaxKind.NumberKeyword) {
73      propertySignatureBody = `${propertySignature.propertyName}: 0,`;
74    } else if (propertySignature.kind === SyntaxKind.StringKeyword) {
75      propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unknown ${propertySignature.propertyName}',`;
76    } else if (propertySignature.kind === SyntaxKind.BooleanKeyword) {
77      propertySignatureBody = `${propertySignature.propertyName}: true,`;
78    } else if (propertySignature.kind === SyntaxKind.UnionType) {
79      let unionFirstElement = propertySignature.propertyTypeName.split('|')[0].trimStart().trimEnd();
80      if (unionFirstElement.includes('[]')) {
81        unionFirstElement = '[]';
82      }
83      if (unionFirstElement.startsWith('"') || unionFirstElement.startsWith("'")) {
84        propertySignatureBody = `${propertySignature.propertyName}: ${unionFirstElement},`;
85      } else if (unionFirstElement === 'string') {
86        propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unknown ${propertySignature.propertyName}',`;
87      } else if (unionFirstElement === 'number') {
88        propertySignatureBody = `${propertySignature.propertyName}: 0,`;
89      } else if (unionFirstElement === 'boolean') {
90        propertySignatureBody = `${propertySignature.propertyName}: true,`;
91      } else if (unionFirstElement === 'Uint8Array') {
92        propertySignatureBody = `${propertySignature.propertyName}: new ${unionFirstElement}(),`;
93      } else {
94        let element = unionFirstElement;
95        if (element === 'HTMLCanvasElement') {
96          element = `'[PC Preview] unknown ${propertySignature.propertyName}'`;
97        } else if (element === 'WebGLActiveInfo') {
98          element = '{size: \'[PC Preview] unknown GLint\', type: 0, name: \'[PC Preview] unknown name\'}';
99        } else if (element.startsWith('Array')) {
100          element = '[]';
101        } else if (propertyTypeWhiteList(unionFirstElement) === unionFirstElement) {
102          element = getTheRealReferenceFromImport(sourceFile, unionFirstElement);
103        }
104        propertySignatureBody = `${propertySignature.propertyName}: ${element},`;
105      }
106    } else if (propertySignature.kind === SyntaxKind.ArrayType) {
107      propertySignatureBody = `${propertySignature.propertyName}: [],`;
108    } else {
109      propertySignatureBody = `${propertySignature.propertyName}: '[PC Preview] unknown ${propertySignature.propertyName}',`;
110    }
111  }
112  return propertySignatureBody;
113}
114