• 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 { SourceFile } from 'typescript';
17import { SyntaxKind } from 'typescript';
18import type { MethodSignatureEntity } from '../declaration-node/methodSignatureDeclaration';
19import {
20  getCallbackStatement,
21  getReturnStatement,
22  getWarnConsole,
23  getReturnData,
24  getOverloadedFunctionCallbackStatement,
25  overloadedFunctionArr
26} from './generateCommonUtil';
27
28/**
29 * generate interface signature method
30 * @param rootName
31 * @param methodSignatureArray
32 * @param sourceFile
33 * @returns
34 */
35export function generateCommonMethodSignature(
36  rootName: string,
37  methodSignatureArray:
38  Array<MethodSignatureEntity>,
39  sourceFile: SourceFile,
40  mockApi: string
41): string {
42  let methodSignatureBody = '';
43  const methodEntity = methodSignatureArray[0];
44  methodSignatureBody += `${methodEntity.functionName}: function(...args) {`;
45  methodSignatureBody += getWarnConsole(rootName, methodEntity.functionName);
46  if (methodSignatureArray.length === 1) {
47    const args = methodEntity.args;
48    const len = args.length;
49    if (args.length > 0 && args[len - 1].paramName.toLowerCase().includes('callback')) {
50      methodSignatureBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString);
51    }
52    if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) {
53      if (rootName === 'Context' && methodEntity.returnType.returnKindName === 'Context') {
54        methodSignatureBody += 'return Context;';
55      } else {
56        methodSignatureBody += getReturnStatement(methodEntity.returnType, sourceFile);
57      }
58    }
59  } else {
60    const argSet: Set<string> = new Set<string>();
61    let argParamsSet: string = '';
62    const returnSet: Set<string> = new Set<string>();
63    let isCallBack = false;
64    let needOverloaded = false;
65    methodSignatureArray.forEach(value => {
66      returnSet.add(value.returnType.returnKindName);
67      value.args.forEach(arg => {
68        argSet.add(arg.paramName);
69        if (arg.paramName.toLowerCase().includes('callback')) {
70          isCallBack = true;
71          if (arg.paramTypeString) {
72            argParamsSet = arg.paramTypeString;
73          }
74        }
75        if (
76          arg.paramTypeString.startsWith("'") && arg.paramTypeString.endsWith("'") ||
77          arg.paramTypeString.startsWith('"') && arg.paramTypeString.endsWith('"')
78        ) {
79          needOverloaded = true;
80        }
81      });
82    });
83    if (isCallBack) {
84      if (overloadedFunctionArr.includes(methodEntity.functionName) && needOverloaded) {
85        methodSignatureBody += getOverloadedFunctionCallbackStatement(methodSignatureArray, sourceFile, mockApi);
86      } else {
87        methodSignatureBody += getCallbackStatement(mockApi, argParamsSet);
88      }
89    }
90    let isReturnPromise = false;
91    let promiseReturnValue = '';
92    let otherReturnValue = '';
93    returnSet.forEach(value => {
94      if (value.includes('Promise<')) {
95        isReturnPromise = true;
96        promiseReturnValue = value;
97      } else {
98        if (!otherReturnValue) {
99          otherReturnValue = value;
100        }
101      }
102    });
103    if (isReturnPromise) {
104      if (promiseReturnValue) {
105        let returnType = null;
106        methodSignatureArray.forEach(value => {
107          if (value.returnType.returnKindName === promiseReturnValue) {
108            returnType = value.returnType;
109          }
110        });
111        methodSignatureBody += getReturnData(isCallBack, isReturnPromise, returnType, sourceFile, mockApi);
112      } else {
113        methodSignatureBody += `
114            return new Promise((resolve, reject) => {
115              resolve('[PC Preview] unknow boolean');
116            })
117          `;
118      }
119    } else if (otherReturnValue) {
120      let returnType = null;
121      methodSignatureArray.forEach(value => {
122        if (value.returnType.returnKindName === otherReturnValue) {
123          returnType = value.returnType;
124        }
125      });
126      methodSignatureBody += getReturnData(isCallBack, isReturnPromise, returnType, sourceFile, mockApi);
127    }
128  }
129  methodSignatureBody += '},\n';
130  return methodSignatureBody;
131}
132