• 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 { 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(staticMethod: StaticMethodEntity, isSystem: boolean, sourceFile: SourceFile, mockApi: string): string {
30  let methodBody = '';
31  const rootName = staticMethod.className;
32  const methodEntity = staticMethod.methodEntity;
33  if (isSystem) {
34    methodBody += `${methodEntity.functionName.name}: function(...args) {`;
35  } else {
36    methodBody += `${firstCharacterToUppercase(staticMethod.className)}.${methodEntity.functionName.name} = function(...args) {`;
37  }
38
39  methodBody += getWarnConsole(rootName, methodEntity.functionName.name);
40  if (methodEntity.functionName.name === 'Symbol.iterator') {
41    methodBody += generateSymbolIterator(methodEntity);
42    methodBody += '}';
43    return methodBody;
44  }
45
46  const args = methodEntity.args;
47  const len = args.length;
48  if (args.length > 0 && args[len - 1].paramName === 'callback') {
49    methodBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString);
50  }
51
52  if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) {
53    methodBody += getReturnStatement(methodEntity.returnType, sourceFile);
54  }
55  methodBody += '}';
56
57  if (isSystem) {
58    methodBody += ',';
59  } else {
60    methodBody += ';';
61  }
62  return methodBody;
63}
64