idlize

Форк
0
/
DollarTransformer.ts 
74 строки · 2.5 Кб
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 "ohos-typescript"
17
import { AbstractVisitor } from "./AbstractVisitor";
18
import { NameTable } from "./utils";
19

20

21
export class DollarTransformer extends AbstractVisitor {
22
    constructor(
23
        sourceFile: ts.SourceFile,
24
        ctx: ts.TransformationContext,
25
        private nameTable: NameTable
26
    ) {
27
        super(sourceFile, ctx)
28
    }
29

30
    currentStruct: ts.StructDeclaration | undefined = undefined
31

32
    whiteList = ["$$", "$r", "$rawfile"]
33

34
    isDollarVariableAccess(node: ts.Identifier): boolean {
35
        const name = ts.idText(node)
36
        if (!name.startsWith("$")) return false
37
        const fieldName = name.substring(1)
38
        // TODO: is it correct?
39
        // The white list has a preference over the fields.
40
        if (this.whiteList.includes(name)) return false
41
        if (!this.currentStruct) return false
42
        if (this.currentStruct.members.find(it => (
43
            it.name &&
44
            ts.isIdentifier(it.name) &&
45
            ts.idText(it.name) == fieldName
46
        )) == undefined) return false
47
        // Already has a receiver.
48
        if (ts.isPropertyAccessExpression(node.parent)) return false
49

50
        return true
51
    }
52

53
    transformDollarVariableAccess(node: ts.Identifier): ts.Expression {
54
        this.nameTable.dollars.push(ts.idText(node))
55
        return ts.factory.createPropertyAccessExpression(
56
            ts.factory.createThis(),
57
            node
58
        )
59
    }
60

61
    visitor(node: ts.Node): ts.Node {
62
        if (ts.isStruct(node)) {
63
            this.currentStruct = node
64
            const transformed = this.visitEachChild(node)
65
            this.currentStruct = undefined
66
            return transformed
67
        } else if (ts.isIdentifier(node)) {
68
            if (this.currentStruct && this.isDollarVariableAccess(node)) {
69
                return this.transformDollarVariableAccess(node)
70
            }
71
        }
72
        return this.visitEachChild(node)
73
    }
74
}

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

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

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

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