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