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 { SyntaxKind } from 'typescript';
17import type { SourceFile } from 'typescript';
18import { firstCharacterToUppercase } from '../common/commonUtils';
19import type { StaticMethodEntity } from '../declaration-node/methodDeclaration';
20import { generateSymbolIterator, getCallbackStatement, getReturnStatement, getWarnConsole } from './generateCommonUtil';
21
22/**
23 * generate static method
24 * @param staticMethod
25 * @param isSystem
26 * @param sourceFile
27 * @returns
28 */
29export function generateStaticFunction(
30  staticMethod: StaticMethodEntity,
31  isSystem: boolean,
32  sourceFile: SourceFile,
33  mockApi: string
34): string {
35  let methodBody = '';
36  const rootName = staticMethod.className;
37  const methodEntity = staticMethod.methodEntity;
38  if (isSystem) {
39    methodBody += `${methodEntity.functionName.name}: function(...args) {`;
40  } else {
41    methodBody += `${firstCharacterToUppercase(staticMethod.className)}.${
42      methodEntity.functionName.name
43    } = function(...args) {`;
44  }
45
46  methodBody += getWarnConsole(rootName, methodEntity.functionName.name);
47  if (methodEntity.functionName.name === 'Symbol.iterator') {
48    methodBody += generateSymbolIterator(methodEntity);
49    methodBody += '}';
50    return methodBody;
51  }
52
53  const args = methodEntity.args;
54  const len = args.length;
55  if (args.length > 0 && args[len - 1].paramName === 'callback') {
56    methodBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString);
57  }
58
59  if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) {
60    methodBody += getReturnStatement(methodEntity.returnType, sourceFile);
61  }
62  methodBody += '}';
63
64  if (isSystem) {
65    methodBody += ',';
66  } else {
67    methodBody += ';';
68  }
69  return methodBody;
70}
71