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