idlize

Форк
0
/
parameter-transformer.ts 
93 строки · 3.1 Кб
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 { ScopedVisitor } from './ScopedVisitor';
18
import {
19
    FunctionKind,
20
    Tracer,
21
    isTrackableParameter,
22
    parameterStateName as localStateName,
23
    PositionalIdTracker,
24
    runtimeIdentifier,
25
    RuntimeNames,
26
    isFunctionOrMethod,
27
    getDeclarationsByNode,
28
    isMemoKind,
29
    asString,
30
} from "./util"
31

32
export class ParameterTransformer extends ScopedVisitor<void> {
33
    constructor(
34
        public tracer: Tracer,
35
        public typechecker: ts.TypeChecker,
36
        public sourceFile: ts.SourceFile,
37
        public positionalIdTracker: PositionalIdTracker,
38
        public functionTable: Map<ts.SignatureDeclarationBase, FunctionKind>,
39
        ctx: ts.TransformationContext
40
    ) {
41
        super(functionTable, ctx)
42
    }
43

44
    transformParameterUse(node: ts.Identifier): ts.PropertyAccessExpression {
45
        const originalName = ts.idText(node)
46
        const newName = localStateName(originalName)
47

48
        return ts.factory.createPropertyAccessExpression(
49
            ts.factory.createIdentifier(newName),
50
            runtimeIdentifier(RuntimeNames.VALUE)
51
        )
52
    }
53

54
    isTrackedParameter(node: ts.Identifier): boolean {
55
        const declarations = getDeclarationsByNode(this.typechecker, node)
56
        const declaration = declarations[0]
57
        if (!declaration) return false
58
        if (!ts.isParameter(declaration)) return false
59
        if (!isTrackableParameter(this.sourceFile, declaration)) return false
60

61
        const func = declaration.parent
62
        if (!isFunctionOrMethod(func)) return false
63

64
        const originalFunc = ts.getOriginalNode(func) as ts.FunctionLikeDeclarationBase
65
        const kind = this.functionTable.get(originalFunc)
66

67
        return isMemoKind(kind)
68
    }
69

70
    visitor(node: ts.Node): ts.Node {
71
        if (ts.isParameter(node)) {
72
            return ts.factory.updateParameterDeclaration(
73
                node,
74
                node.modifiers,
75
                node.dotDotDotToken,
76
                node.name,
77
                node.questionToken,
78
                node.type,
79
                node.initializer ?
80
                    this.visitEachChild(node.initializer) as ts.Expression :
81
                    undefined
82
            )
83
        }
84
        if (isFunctionOrMethod(node)) {
85
            const kind = this.declarationKind(node)
86
            return this.withFunctionScope(kind, undefined, ()=>this.visitEachChild(node))
87
        }
88
        if (ts.isIdentifier(node) && this.isTrackedParameter(node)) {
89
            return this.transformParameterUse(node)
90
        }
91
        return this.visitEachChild(node)
92
    }
93
}
94

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

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

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

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