• 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 } from '../common/commonUtils';
19import type { PropertyEntity } from '../declaration-node/propertyDeclaration';
20import { getTheRealReferenceFromImport } from './generateCommonUtil';
21
22/**
23 * generate class property
24 * @param rootName
25 * @param propertyDeclaration
26 * @param sourceFile
27 * @returns
28 */
29export function generatePropertyDeclaration(rootName: string, propertyDeclaration: PropertyEntity, sourceFile: SourceFile): string {
30  let propertyBody = '';
31  if (propertyDeclaration.isInitializer) {
32    propertyBody = `this.${propertyDeclaration.propertyName} = ${propertyDeclaration.initializer};`;
33  } else {
34    if (propertyDeclaration.propertyTypeName.startsWith('{')) {
35      propertyBody = `this.${propertyDeclaration.propertyName} = {};`;
36    } else if (propertyDeclaration.kind === SyntaxKind.LiteralType) {
37      propertyBody = `this.${propertyDeclaration.propertyName} = ${propertyDeclaration.propertyTypeName};`;
38    } else if (propertyDeclaration.kind === SyntaxKind.NumberKeyword) {
39      propertyBody = `this.${propertyDeclaration.propertyName} = 0;`;
40    } else if (propertyDeclaration.kind === SyntaxKind.StringKeyword) {
41      propertyBody = `this.${propertyDeclaration.propertyName} = ''`;
42    } else if (propertyDeclaration.kind === SyntaxKind.BooleanKeyword) {
43      propertyBody = `this.${propertyDeclaration.propertyName} = true`;
44    } else if (propertyDeclaration.propertyTypeName.startsWith('Array')) {
45      propertyBody = `this.${propertyDeclaration.propertyName} = [];`;
46    } else if (propertyDeclaration.propertyTypeName.startsWith('Map')) {
47      propertyBody = `this.${propertyDeclaration.propertyName} = {key: {}};`;
48    } else if (propertyDeclaration.kind === SyntaxKind.TypeReference) {
49      propertyBody = `this.${propertyDeclaration.propertyName} = `;
50      if (getClassNameSet().has(propertyDeclaration.propertyTypeName)) {
51        if (!['Want', 'InputMethodExtensionContext'].includes(propertyDeclaration.propertyTypeName)) {
52          propertyBody += `new ${getTheRealReferenceFromImport(sourceFile, propertyDeclaration.propertyTypeName)}();`;
53        } else {
54          propertyBody += `${getTheRealReferenceFromImport(sourceFile, propertyDeclaration.propertyTypeName)};`;
55        }
56      } else {
57        propertyBody += `${getTheRealReferenceFromImport(sourceFile, propertyDeclaration.propertyTypeName)};`;
58      }
59    } else if (propertyDeclaration.kind === SyntaxKind.NumericLiteral || propertyDeclaration.kind === SyntaxKind.StringLiteral) {
60      propertyBody = `this.${propertyDeclaration.propertyName} = ${propertyDeclaration.propertyTypeName};`;
61    } else {
62      propertyBody = `this.${propertyDeclaration.propertyName} = '[PC Previwe] unknown ${propertyDeclaration.propertyName}';`;
63    }
64  }
65  return propertyBody;
66}
67