idlize

Форк
0
78 строк · 2.4 Кб
1
/*
2
 * Copyright (c) 2022-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

16
import * as ts from 'typescript';
17
import { AbstractVisitor } from "./AbstractVisitor"
18
import { FunctionKind, isAnyMemoKind, isMemoKind } from "./util"
19

20
class FunctionScope<T> {
21
    constructor(
22
        public kind: FunctionKind,
23
        public data: T,
24
    ){}
25
}
26

27
class FunctionScopes<T> {
28
    constructor() {}
29

30
    private stack: FunctionScope<T>[] = []
31

32
    push(kind: FunctionKind, data: T) {
33
        this.stack.push(new FunctionScope(kind, data))
34
    }
35
    pop() {
36
        this.stack.pop()
37
    }
38
    peek(): FunctionScope<T>|undefined {
39
        if (this.stack.length == 0) return undefined
40
        return this.stack[this.stack.length-1]
41
    }
42
}
43

44
export abstract class ScopedVisitor<T> extends AbstractVisitor {
45
    readonly functionScopes: FunctionScopes<T>
46
    constructor(
47
        public functionTable: Map<ts.SignatureDeclarationBase, FunctionKind>,
48
        ctx: ts.TransformationContext
49
    ) {
50
        super(ctx)
51
        this.functionScopes = new FunctionScopes()
52
    }
53

54
    declarationKind(node: ts.FunctionLikeDeclarationBase): FunctionKind {
55
        const originalNode = ts.getOriginalNode(node) as ts.FunctionLikeDeclarationBase
56
        return this.functionTable.get(originalNode) ?? FunctionKind.REGULAR
57
    }
58

59
    currentKind(): FunctionKind {
60
        return this.functionScopes.peek()?.kind ?? FunctionKind.REGULAR
61
    }
62

63
    isAnyMemo(node: ts.FunctionLikeDeclarationBase): boolean {
64
        return isAnyMemoKind(this.declarationKind(node))
65
    }
66

67
    isMemoOrComponent(node: ts.FunctionLikeDeclarationBase): boolean {
68
        return isMemoKind(this.declarationKind(node))
69
    }
70

71
    withFunctionScope<R>(kind: FunctionKind, data: T, body: ()=>R): R {
72
        let result: R
73
        this.functionScopes.push(kind, data)
74
        result = body()
75
        this.functionScopes.pop()
76
        return result
77
    }
78
}
79

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

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

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

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