• 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 { Node, SourceFile, TypeParameterDeclaration } from 'typescript';
17
18/**
19 * get generic type node info
20 * @param node
21 * @param sourceFile
22 * @returns
23 */
24export function getTypeParameterDeclaration(node: Node, sourceFile: SourceFile): TypeParameterEntity {
25  const typeParameterNode = node as TypeParameterDeclaration;
26  const typeParameterName = typeParameterNode.name.escapedText.toString();
27  let constraitValue = '';
28  let constraintDefaultValue = '';
29  const constraint = typeParameterNode.constraint;
30  if (constraint !== undefined) {
31    constraitValue = sourceFile.text.substring(constraint.pos, constraint.end).trimStart().trimEnd();
32  }
33
34  const defaultValue = typeParameterNode.default;
35  if (defaultValue !== undefined) {
36    constraintDefaultValue = sourceFile.text.substring(defaultValue.pos, defaultValue.end).trimStart().trimEnd();
37  }
38
39  return {
40    typeParameterName: typeParameterName,
41    constraitValue: constraitValue,
42    constraintDefaultValue: constraintDefaultValue
43  };
44}
45
46export interface TypeParameterEntity {
47  typeParameterName: string,
48  constraitValue: string,
49  constraintDefaultValue: string
50}
51