idlize
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
16import * as ts from 'ohos-typescript'
17import { asString } from './utils'
18
19export abstract class AbstractVisitor {
20constructor(public sourceFile: ts.SourceFile, public ctx: ts.TransformationContext) { }
21
22abstract visitor(node: ts.Node): ts.Node
23
24visitEtsChildren(node: ts.EtsComponentExpression): ts.Node {
25const transformedExpression = this.visitor(node.expression)
26const transformedArguments = node.arguments.map(it => this.visitor(it))
27const transformedBody = node.body ? this.visitor(node.body) : undefined
28
29const newEts = ts.factory.updateEtsComponentExpression(
30node,
31transformedExpression as ts.Identifier,
32transformedArguments as ts.Expression[],
33transformedBody as ts.Block
34)
35
36/*
37const oldOriginal = ts.getOriginalNode(node, ts.isEtsComponentExpression)
38const newOriginal = ts.getOriginalNode(newEts, ts.isEtsComponentExpression)
39
40if (oldOriginal !== newOriginal) {
41console.log("NO MATCH!")
42console.log(oldOriginal)
43console.log(newOriginal)
44}
45*/
46
47return newEts
48}
49
50visitEachChild<T extends ts.Node>(node: T): ts.Node {
51if (ts.isEtsComponentExpression(node)) {
52return this.visitEtsChildren(node)
53}
54return ts.visitEachChild(node, it=> this.visitor(it), this.ctx)
55}
56}
57
58export class DebugVisitor extends AbstractVisitor {
59constructor(public sourceFile: ts.SourceFile, public ctx: ts.TransformationContext) {
60super(sourceFile, ctx)
61}
62
63indentation = 0
64
65withIndentation(exec: () => ts.Node): ts.Node {
66this.indentation++
67const result = exec()
68this.indentation--
69return result
70}
71
72visitor(node: ts.Node): ts.Node {
73console.log("@" + " ".repeat(this.indentation) + asString(node))
74return this.visitEachChild(node)
75}
76
77visitEachChild(node: ts.Node): ts.Node {
78return this.withIndentation(() =>
79super.visitEachChild(
80node
81)
82)
83}
84}
85