• 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, getCallbackStatement,
21  getReturnStatement, getWarnConsole
22} from './generateCommonUtil';
23
24/**
25 * generate class method
26 * @param rootName
27 * @param methodArray
28 * @param sourceFile
29 * @returns
30 */
31export function generateCommonMethod(rootName: string, methodArray: Array<MethodEntity>, sourceFile: SourceFile, mockApi: string): string {
32  let methodBody = '';
33  const methodEntity = methodArray[0];
34  if (methodEntity.functionName.name === 'Symbol.iterator') {
35    methodBody += `this[${methodEntity.functionName.name}] = function(...args) {`;
36    methodBody += getWarnConsole(rootName, methodEntity.functionName.name);
37    methodBody += generateSymbolIterator(methodEntity);
38    methodBody += '};\n';
39    return methodBody;
40  } else {
41    methodBody += `this.${methodEntity.functionName.name} = function(...args) {`;
42    methodBody += getWarnConsole(rootName, methodEntity.functionName.name);
43  }
44
45  if (methodArray.length === 1) {
46    const args = methodEntity.args;
47    const len = args.length;
48    if (args.length > 0 && args[len - 1].paramName.toLowerCase().includes('callback')) {
49      methodBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString);
50    }
51    if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) {
52      if (methodEntity.functionName.name === 'getApplicationContext') {
53        methodBody += 'return new Context();';
54      } else {
55        methodBody += getReturnStatement(methodEntity.returnType, sourceFile);
56      }
57    }
58  } else {
59    const argSet: Set<string> = new Set<string>();
60    let argParamsSet: string = '';
61    const returnSet: Set<string> = new Set<string>();
62    let isCallBack = false;
63    methodArray.forEach(value => {
64      returnSet.add(value.returnType.returnKindName);
65      value.args.forEach(arg => {
66        argSet.add(arg.paramName);
67        if (arg.paramName.toLowerCase().includes('callback')) {
68          isCallBack = true;
69          if (arg.paramTypeString) {
70            argParamsSet = arg.paramTypeString;
71          }
72        }
73      });
74    });
75    if (isCallBack) {
76      methodBody += getCallbackStatement(mockApi, argParamsSet);
77    }
78    let isReturnPromise = false;
79    returnSet.forEach(value => {
80      if (value.startsWith('Promise')) {
81        isReturnPromise = true;
82      }
83    });
84
85    if (isReturnPromise && isCallBack) {
86      methodBody += `else {
87        return new Promise((resolve, reject) => {
88          resolve('[PC Preview] unknow boolean');
89        })
90      }`;
91    } else {
92      methodBody += `return new Promise((resolve, reject) => {
93        resolve('[PC preview] unknown type');
94      });`;
95    }
96  }
97  methodBody += '};\n';
98  return methodBody;
99}
100