• 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 { getClassNameSet, specialClassName } from '../common/commonUtils';
19import type { PropertyEntity } from '../declaration-node/propertyDeclaration';
20import { getTheRealReferenceFromImport, getWarnConsole } from './generateCommonUtil';
21import { ImportElementEntity } from '../declaration-node/importAndExportDeclaration';
22import { addExtraImport } from './generateInterfaceDeclaration';
23
24/**
25 * generate class property
26 * @param rootName
27 * @param propertyDeclaration
28 * @param sourceFile
29 * @returns
30 */
31export function generatePropertyDeclaration(rootName: string, propertyDeclaration: PropertyEntity,
32  sourceFile: SourceFile, extraImport: string[], importDeclarations?: ImportElementEntity[]): string {
33  let propertyBody = '';
34  if (propertyDeclaration.isInitializer) {
35    propertyBody = `this.${propertyDeclaration.propertyName} = ${propertyDeclaration.initializer};`;
36  } else {
37    propertyBody = `this.${propertyDeclaration.propertyName} = `;
38    if (propertyDeclaration.kinds === SyntaxKind.GetAccessor) {
39      const warnCon = getWarnConsole(rootName, propertyDeclaration.propertyName);
40      propertyBody += `(function () {\n ${warnCon} \n return `;
41    }
42    if (propertyDeclaration.propertyTypeName.startsWith('{')) {
43      propertyBody += '{};';
44    } else if (propertyDeclaration.kind === SyntaxKind.LiteralType) {
45      propertyBody += `${propertyDeclaration.propertyTypeName};`;
46    } else if (propertyDeclaration.kind === SyntaxKind.NumberKeyword) {
47      propertyBody += '0;';
48    } else if (propertyDeclaration.kind === SyntaxKind.StringKeyword) {
49      propertyBody += '\'\'';
50    } else if (propertyDeclaration.kind === SyntaxKind.BooleanKeyword) {
51      propertyBody += 'true';
52    } else if (propertyDeclaration.propertyTypeName.startsWith('Array')) {
53      propertyBody += '[];';
54    } else if (propertyDeclaration.propertyTypeName.startsWith('Map')) {
55      propertyBody += '{key: {}};';
56    } else if (propertyDeclaration.kind === SyntaxKind.TypeReference) {
57      propertyBody = generateTypeReference(propertyDeclaration, sourceFile, propertyBody);
58    } else if (propertyDeclaration.kind === SyntaxKind.NumericLiteral || propertyDeclaration.kind === SyntaxKind.StringLiteral) {
59      propertyBody += ` ${propertyDeclaration.propertyTypeName};`;
60    } else {
61      propertyBody += `'[PC Previwe] unknown ${propertyDeclaration.propertyName}';`;
62    }
63    if (propertyDeclaration.kinds === SyntaxKind.GetAccessor) {
64      addExtraImport(extraImport, importDeclarations, sourceFile, propertyDeclaration);
65      propertyBody += '\n })();';
66    }
67  }
68  return propertyBody;
69}
70
71/**
72 * generate type reference
73 * @param propertyDeclaration
74 * @param sourceFile
75 * @param propertyBody
76 * @returns
77 */
78function generateTypeReference(
79  propertyDeclaration: PropertyEntity,
80  sourceFile: SourceFile,
81  propertyBody: string
82): string {
83  if (getClassNameSet().has(propertyDeclaration.propertyTypeName)) {
84    if (!specialClassName.includes(propertyDeclaration.propertyTypeName)) {
85      propertyBody += `new ${getTheRealReferenceFromImport(sourceFile, propertyDeclaration.propertyTypeName)}();`;
86    } else {
87      propertyBody += `${getTheRealReferenceFromImport(sourceFile, propertyDeclaration.propertyTypeName)};`;
88    }
89  } else {
90    propertyBody += `${getTheRealReferenceFromImport(sourceFile, propertyDeclaration.propertyTypeName)};`;
91  }
92  return propertyBody;
93}
94