idlize

Форк
0
/
AbstractVisitor.ts 
84 строки · 2.7 Кб
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 { asString } from './utils'
18

19
export abstract class AbstractVisitor {
20
    constructor(public sourceFile: ts.SourceFile, public ctx: ts.TransformationContext) { }
21

22
    abstract visitor(node: ts.Node): ts.Node
23

24
    visitEtsChildren(node: ts.EtsComponentExpression): ts.Node {
25
        const transformedExpression = this.visitor(node.expression)
26
        const transformedArguments = node.arguments.map(it => this.visitor(it))
27
        const transformedBody = node.body ? this.visitor(node.body) : undefined
28

29
        const newEts =  ts.factory.updateEtsComponentExpression(
30
            node,
31
            transformedExpression as ts.Identifier,
32
            transformedArguments as ts.Expression[],
33
            transformedBody as ts.Block
34
        )
35

36
        /*
37
        const oldOriginal = ts.getOriginalNode(node, ts.isEtsComponentExpression)
38
        const newOriginal = ts.getOriginalNode(newEts, ts.isEtsComponentExpression)
39

40
        if (oldOriginal !== newOriginal) {
41
            console.log("NO MATCH!")
42
            console.log(oldOriginal)
43
            console.log(newOriginal)
44
        }
45
        */
46

47
        return newEts
48
    }
49

50
    visitEachChild<T extends ts.Node>(node: T): ts.Node {
51
        if (ts.isEtsComponentExpression(node)) {
52
            return this.visitEtsChildren(node)
53
        }
54
        return ts.visitEachChild(node, it=> this.visitor(it), this.ctx)
55
    }
56
}
57

58
export class DebugVisitor extends AbstractVisitor {
59
    constructor(public sourceFile: ts.SourceFile, public ctx: ts.TransformationContext) {
60
        super(sourceFile, ctx)
61
    }
62

63
    indentation = 0
64

65
    withIndentation(exec: () => ts.Node): ts.Node {
66
        this.indentation++
67
        const result = exec()
68
        this.indentation--
69
        return result
70
    }
71

72
    visitor(node: ts.Node): ts.Node {
73
        console.log("@" + " ".repeat(this.indentation) + asString(node))
74
        return this.visitEachChild(node)
75
    }
76

77
    visitEachChild(node: ts.Node): ts.Node {
78
        return this.withIndentation(() =>
79
            super.visitEachChild(
80
                node
81
            )
82
        )
83
    }
84
}
85

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

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

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

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