• 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 type { GetAccessorDeclaration, PropertyDeclaration, SourceFile } from 'typescript';
17import { getPropertyName } from '../common/commonUtils';
18
19/**
20 * get property node info
21 * @param node
22 * @param sourceFile
23 * @returns
24 */
25export function getPropertyDeclaration(node: PropertyDeclaration, sourceFile: SourceFile): PropertyEntity {
26  let propertyName = '';
27  let propertyTypeName = '';
28  let kind = -1;
29  let isInitializer = false;
30  let initializer = '';
31  const modifiers: Array<string> = [];
32  if (node.modifiers !== undefined) {
33    node.modifiers.forEach(value => {
34      modifiers.push(sourceFile.text.substring(value.pos, value.end));
35    });
36  }
37  if (node.initializer !== undefined) {
38    isInitializer = true;
39    initializer = sourceFile.text.substring(node.initializer.pos, node.initializer.end).trim();
40  }
41
42  propertyName = getPropertyName(node.name, sourceFile);
43
44  const propertyType = node.type;
45  if (propertyType !== undefined) {
46    propertyTypeName = sourceFile.text.substring(propertyType.pos, propertyType.end).trim();
47    kind = propertyType.kind;
48  }
49
50  return {
51    modifiers: modifiers,
52    propertyName: propertyName,
53    propertyTypeName: propertyTypeName,
54    kind: kind,
55    kinds: -1,
56    isInitializer: isInitializer,
57    initializer: initializer
58  };
59}
60
61export function getGetDeclaration(node: GetAccessorDeclaration, sourceFile: SourceFile): PropertyEntity {
62  let kind = -1;
63  let kinds = -1;
64  let propertyName = '';
65  let propertyTypeName = '';
66  const modifiers: Array<string> = [];
67
68  if (node.modifiers !== undefined) {
69    node.modifiers.forEach(value => {
70      modifiers.push(sourceFile.text.substring(value.pos, value.end));
71    });
72  }
73
74  propertyName = getPropertyName(node.name, sourceFile);
75  const propertyType = node.type;
76  if (propertyType !== undefined) {
77    propertyTypeName = sourceFile.text.substring(propertyType.pos, propertyType.end).trim();
78    kind = propertyType.kind;
79    kinds = node.kind;
80  }
81
82  return {
83    modifiers: modifiers,
84    propertyName: propertyName,
85    propertyTypeName: propertyTypeName,
86    kind: kind,
87    kinds: kinds,
88    isInitializer: false,
89    initializer: ''
90  };
91}
92
93export interface PropertyEntity {
94  modifiers: Array<string>,
95  propertyName: string,
96  propertyTypeName: string,
97  kind: number,
98  kinds: number,
99  isInitializer: boolean,
100  initializer: string
101}
102