• 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 { FunctionEntity } from '../declaration-node/functionDeclaration';
19import { getCallbackStatement, getReturnStatement, getWarnConsole } from './generateCommonUtil';
20
21/**
22 * generate function
23 * @param rootName
24 * @param functionArray
25 * @param sourceFile
26 * @returns
27 */
28export function generateCommonFunction(rootName: string, functionArray: Array<FunctionEntity>, sourceFile: SourceFile, mockApi: string): string {
29  let functionBody = '';
30  const functionEntity = functionArray[0];
31  functionBody = `${functionEntity.functionName}: function(...args) {`;
32  functionBody += getWarnConsole(rootName, functionEntity.functionName);
33
34  if (functionArray.length === 1) {
35    const args = functionEntity.args;
36    const len = args.length;
37    if (args.length > 0 && args[len - 1].paramName.toLowerCase().includes('callback')) {
38      functionBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString);
39    }
40    if (functionEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) {
41      if (rootName === 'featureAbility' && functionEntity.returnType.returnKindName === 'Context') {
42        functionBody += 'return _Context;';
43      } else if (rootName === 'inputMethod' && functionEntity.returnType.returnKindName === 'InputMethodSubtype') {
44        functionBody += 'return mockInputMethodSubtype().InputMethodSubtype;';
45      } else {
46        functionBody += getReturnStatement(functionEntity.returnType, sourceFile);
47      }
48    }
49  } else {
50    const argSet: Set<string> = new Set<string>();
51    let argParamsSet: string = '';
52    const returnSet: Set<string> = new Set<string>();
53    let isCallBack = false;
54    functionArray.forEach(value => {
55      returnSet.add(value.returnType.returnKindName);
56      value.args.forEach(arg => {
57        argSet.add(arg.paramName);
58        if (arg.paramName.toLowerCase().includes('callback')) {
59          isCallBack = true;
60          if (arg.paramTypeString) {
61            argParamsSet = arg.paramTypeString;
62          }
63        }
64      });
65    });
66    if (isCallBack) {
67      functionBody += getCallbackStatement(mockApi, argParamsSet);
68    }
69    let isReturnPromise = false;
70    returnSet.forEach(value => {
71      if (value.startsWith('Promise')) {
72        isReturnPromise = true;
73      }
74    });
75
76    if (isReturnPromise && isCallBack) {
77      functionBody += `else {
78        return new Promise((resolve, reject) => {
79          resolve('[PC Preview] unknow boolean');
80        })
81      }`;
82    }
83  }
84  functionBody += '},';
85  return functionBody;
86}
87