• 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';
27import { methodArrayItemForEach } from './generateCommonMethod';
28
29interface MethodSignatureArrayProps {
30  methodSignatureArray: Array<MethodSignatureEntity>,
31  methodEntity: MethodSignatureEntity,
32  methodSignatureBody: string,
33  sourceFile: SourceFile,
34  mockApi: string
35}
36
37interface MethodSignatureArrayBack {
38  methodSignatureArray: Array<MethodSignatureEntity>,
39  returnSet: Set<string>,
40  isCallBack: boolean,
41  methodSignatureBody: string
42}
43
44/**
45 * generate interface signature method
46 * @param rootName
47 * @param methodSignatureArray
48 * @param sourceFile
49 * @returns
50 */
51export function generateCommonMethodSignature(
52  rootName: string,
53  methodSignatureArray:
54    Array<MethodSignatureEntity>,
55  sourceFile: SourceFile,
56  mockApi: string
57): string {
58  let methodSignatureBody = '';
59  const methodEntity = methodSignatureArray[0];
60  methodSignatureBody += `${methodEntity.functionName}: function(...args) {`;
61  methodSignatureBody += getWarnConsole(rootName, methodEntity.functionName);
62  if (methodSignatureArray.length === 1) {
63    const args = methodEntity.args;
64    const len = args.length;
65    if (args.length > 0 && args[len - 1].paramName.toLowerCase().includes('callback')) {
66      methodSignatureBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString);
67    }
68    if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) {
69      if (rootName === 'Context' && methodEntity.returnType.returnKindName === 'Context') {
70        methodSignatureBody += 'return Context;';
71      } else {
72        methodSignatureBody += getReturnStatement(methodEntity.returnType, sourceFile);
73      }
74    }
75  } else {
76    const methodSignatureArrayBack = methodSignatureArrayForEach({
77      methodSignatureArray,
78      methodEntity,
79      methodSignatureBody,
80      sourceFile,
81      mockApi
82    });
83    methodSignatureBody = returnSetForEach(methodSignatureArrayBack, sourceFile, mockApi);
84  }
85  methodSignatureBody += '},\n';
86  return methodSignatureBody;
87}
88
89/**
90 * returnSet ForEach
91 * @param porps
92 * @param sourceFile
93 * @param mockApi
94 * @returns
95 */
96function returnSetForEach(porps: MethodSignatureArrayBack, sourceFile: SourceFile, mockApi: string): string {
97  let isReturnPromise = false;
98  let promiseReturnValue = '';
99  let methodSignatureOtherReturnValue = '';
100  porps.returnSet.forEach(value => {
101    if (value.includes('Promise<')) {
102      isReturnPromise = true;
103      promiseReturnValue = value;
104    } else {
105      if (!methodSignatureOtherReturnValue) {
106        methodSignatureOtherReturnValue = value;
107      }
108    }
109  });
110  if (isReturnPromise) {
111    if (promiseReturnValue) {
112      let returnType = null;
113      porps.methodSignatureArray.forEach(value => {
114        if (value.returnType.returnKindName === promiseReturnValue) {
115          returnType = value.returnType;
116        }
117      });
118      porps.methodSignatureBody += getReturnData(porps.isCallBack, isReturnPromise, returnType, sourceFile, mockApi);
119    } else {
120      porps.methodSignatureBody += `
121          return new Promise((resolve, reject) => {
122            resolve('[PC Preview] unknow boolean');
123          })
124        `;
125    }
126  } else if (methodSignatureOtherReturnValue) {
127    let returnType = null;
128    porps.methodSignatureArray.forEach(value => {
129      if (value.returnType.returnKindName === methodSignatureOtherReturnValue) {
130        returnType = value.returnType;
131      }
132    });
133    porps.methodSignatureBody += getReturnData(porps.isCallBack, isReturnPromise, returnType, sourceFile, mockApi);
134  }
135  return porps.methodSignatureBody;
136}
137
138/**
139 * methodSignatureArray ForEach
140 * @param porps
141 * @returns
142 */
143function methodSignatureArrayForEach(porps: MethodSignatureArrayProps): MethodSignatureArrayBack {
144  let argSet: Set<string> = new Set<string>();
145  let argParamsSet: string = '';
146  let returnSet: Set<string> = new Set<string>();
147  let isCallBack = false;
148  let needOverloaded = false;
149  const methodSignatureArray = porps.methodSignatureArray;
150  const sourceFile = porps.sourceFile;
151  const mockApi = porps.mockApi;
152  porps.methodSignatureArray.forEach(value => {
153    ({ returnSet, argSet, isCallBack, argParamsSet, needOverloaded} =
154      methodArrayItemForEach({returnSet, value, argSet, isCallBack, argParamsSet, needOverloaded}));
155  });
156  if (isCallBack) {
157    if (overloadedFunctionArr.includes(porps.methodEntity.functionName) && needOverloaded) {
158      porps.methodSignatureBody += getOverloadedFunctionCallbackStatement(methodSignatureArray, sourceFile, mockApi);
159    } else {
160      porps.methodSignatureBody += getCallbackStatement(porps.mockApi, argParamsSet);
161    }
162  }
163  return {
164    returnSet,
165    isCallBack,
166    methodSignatureBody: porps.methodSignatureBody,
167    methodSignatureArray: porps.methodSignatureArray
168  };
169}
170