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