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 { getClassNameSet } from '../common/commonUtils';
18import type { StatementEntity } from '../declaration-node/variableStatementResolve';
19
20/**
21 * generate const variable statement
22 * @param statementEntity
23 * @returns
24 */
25export function generateVariableStatementDelcatation(statementEntity: StatementEntity, isInnerModule: boolean): string {
26  let statementBody = '';
27  if (isInnerModule) {
28    statementBody = `const ${statementEntity.statementName} = `;
29  } else {
30    statementBody = `${statementEntity.statementName}: `;
31  }
32  let statementValue: string;
33  if (statementEntity.typeKind === SyntaxKind.StringKeyword) {
34    statementValue = '\'\'';
35  } else if (statementEntity.typeKind === SyntaxKind.LiteralType || statementEntity.typeKind === SyntaxKind.StringLiteral ||
36    statementEntity.typeKind === SyntaxKind.NumericLiteral) {
37    statementValue = judgmentStatementEntity(statementEntity, statementValue);
38  } else if (statementEntity.typeKind === SyntaxKind.NumberKeyword) {
39    statementValue = '0';
40  } else if (statementEntity.typeKind === SyntaxKind.UnionType) {
41    statementValue = statementEntity.typeName.split('|')[0];
42  } else if (statementEntity.typeKind === SyntaxKind.TypeReference) {
43    statementValue = judgmentStatementEntityTypeName(statementEntity, statementValue);
44  } else if (statementEntity.typeKind === SyntaxKind.BooleanKeyword) {
45    statementValue = 'true';
46  } else {
47    statementValue = `'[PC Preivew] unknown ${statementEntity.statementName}'`;
48  }
49  statementBody += statementValue;
50  if (isInnerModule) {
51    statementBody += ';';
52  } else {
53    statementBody += ',';
54  }
55  return statementBody;
56}
57
58function judgmentStatementEntity(statementEntity: StatementEntity, statementValue: string): string {
59  if (statementEntity.initializer === '') {
60    if (statementEntity.typeName.endsWith('n')) {
61      statementValue = statementEntity.typeName.replace('n', '');
62    } else {
63      statementValue = statementEntity.typeName;
64    }
65  } else {
66    statementValue = statementEntity.initializer;
67  }
68  return statementValue;
69}
70function judgmentStatementEntityTypeName(statementEntity: StatementEntity, statementValue: string): string {
71  if (statementEntity.typeName.includes('<')) {
72    const tmpTypeName = statementEntity.typeName.split('<')[0];
73    if (getClassNameSet().has(tmpTypeName)) {
74      statementValue = `new ${tmpTypeName}()`;
75    } else {
76      statementValue = `${tmpTypeName}`;
77    }
78  } else {
79    statementValue = statementEntity.typeName;
80  }
81  return statementValue;
82}
83