idlize

Форк
0
/
inheritance.ts 
75 строк · 2.7 Кб
1
/*
2
 * Copyright (c) 2024 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

16

17
import * as ts from "typescript";
18
import { getDeclarationsByNode } from "../util";
19
import { PeerGeneratorConfig } from "./PeerGeneratorConfig";
20

21
export enum InheritanceRole {
22
    Finalizable,
23
    PeerNode,
24
    Root,
25
    Heir,
26
    Standalone,
27
}
28

29
export function determineInheritanceRole(name: string): InheritanceRole {
30
    if (PeerGeneratorConfig.rootComponents.includes(name)) return InheritanceRole.Root
31
    if (PeerGeneratorConfig.standaloneComponents.includes(name)) return InheritanceRole.Standalone
32
    return InheritanceRole.Heir
33
}
34

35
export function determineParentRole(name: string|undefined, parent: string | undefined): InheritanceRole {
36
    if (!name) throw new Error(`name must be known: ${parent}`)
37
    if (parent === undefined) {
38
        if (isStandalone(name)) return InheritanceRole.PeerNode
39
        if (isCommonMethod(name)) return InheritanceRole.PeerNode
40
        if (isRoot(name)) return InheritanceRole.PeerNode
41
        throw new Error(`Expected check to be exhaustive. node: ${name}`)
42
    }
43
    if (isRoot(parent)) return InheritanceRole.Root
44
    return InheritanceRole.Heir
45
}
46

47
export function isCommonMethod(name: string): boolean {
48
    return name === "CommonMethod"
49
}
50

51
export function isRoot(name: string): boolean {
52
    return determineInheritanceRole(name) === InheritanceRole.Root
53
}
54

55
export function isStandalone(name: string): boolean {
56
    return determineInheritanceRole(name) === InheritanceRole.Standalone
57
}
58

59
export function isHeir(name: string): boolean {
60
    return determineInheritanceRole(name) === InheritanceRole.Heir
61
}
62

63
export function singleParentDeclaration(
64
    typeChecker: ts.TypeChecker,
65
    component: ts.ClassDeclaration | ts.InterfaceDeclaration
66
): ts.ClassDeclaration | ts.InterfaceDeclaration | undefined {
67
    const parentTypeNode = component.heritageClauses
68
        ?.filter(it => it.token == ts.SyntaxKind.ExtendsKeyword)[0]?.types[0]?.expression
69
    if (parentTypeNode) {
70
        const declaration = getDeclarationsByNode(typeChecker, parentTypeNode)
71
            .find(it => ts.isClassDeclaration(it) || ts.isInterfaceDeclaration(it))
72
        return declaration as (ts.ClassDeclaration | ts.InterfaceDeclaration | undefined)
73
    }
74
    return undefined
75
}
76

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.