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 { PropertySignature, SourceFile } from 'typescript';
17import { getPropertyName } from '../common/commonUtils';
18
19/**
20 * get interface property signature info
21 * @param node
22 * @param sourceFile
23 * @returns
24 */
25export function getPropertySignatureDeclaration(
26  node: PropertySignature,
27  sourceFile: SourceFile
28): PropertySignatureEntity {
29  let propertyName = '';
30  let propertyTypeName = '';
31  let kind = -1;
32
33  propertyName = getPropertyName(node.name, sourceFile);
34  const propertyType = node.type;
35  const modifiers: Array<string> = [];
36  const fileText = sourceFile.getFullText();
37  if (node.modifiers !== undefined) {
38    node.modifiers.forEach(value => {
39      modifiers.push(fileText.slice(value.pos, value.end));
40    });
41  }
42
43  if (propertyType !== undefined) {
44    propertyTypeName = fileText.slice(propertyType.pos, propertyType.end).trim();
45    kind = propertyType.kind;
46  }
47
48  return {
49    modifiers: modifiers,
50    propertyName: propertyName,
51    propertyTypeName: propertyTypeName,
52    kind: kind
53  };
54}
55
56export interface PropertySignatureEntity {
57  modifiers: Array<string>;
58  propertyName: string;
59  propertyTypeName: string;
60  kind: number;
61  kinds?: number;
62}
63