idlize

Форк
0
/
LegacyCallTransformer.ts 
127 строк · 4.3 Кб
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 { asString, CallTable } from './utils'
19
import { Importer } from './Importer'
20

21
export class LegacyCallTransformer extends AbstractVisitor {
22
    static parameterName = "elementId"
23
    static page = "page"
24
    static line = "line"
25
    static LegacyNode = "LegacyNode"
26

27
	constructor(
28
		sourceFile: ts.SourceFile,
29
        ctx: ts.TransformationContext,
30
        private importer: Importer,
31
        private callTable: CallTable
32
	) {
33
        super(sourceFile, ctx)
34
    }
35

36
    isLegacyStructCall(call: ts.CallExpression): boolean {
37
        const originalCall = ts.getOriginalNode(call) as ts.CallExpression
38
        return this.callTable.legacyCalls.has(originalCall)
39
    }
40

41
    createLambda(name: ts.Identifier): ts.Expression {
42
        const locationObject = ts.factory.createObjectLiteralExpression(
43
            [
44
                ts.factory.createPropertyAssignment(
45
                    ts.factory.createIdentifier(LegacyCallTransformer.page),
46
                    // TODO: entry/src/main/ets/pages/LocalPage.ets
47
                    ts.factory.createStringLiteral("")
48
                ),
49
                ts.factory.createPropertyAssignment(
50
                    ts.factory.createIdentifier(LegacyCallTransformer.line),
51
                    // TODO: provide line number
52
                    ts.factory.createNumericLiteral("0")
53
                )
54
            ],
55
            false
56
        )
57
        const emptyLambda = ts.factory.createArrowFunction(
58
            undefined,
59
            undefined,
60
            [],
61
            undefined,
62
            ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
63
            ts.factory.createBlock(
64
                [],
65
                false
66
            )
67
        )
68
        const emptyObject = ts.factory.createObjectLiteralExpression(
69
            [],
70
            false
71
        )
72
        const elementIdParameter = ts.factory.createParameterDeclaration(
73
            undefined,
74
            undefined,
75
            ts.factory.createIdentifier(LegacyCallTransformer.parameterName),
76
            undefined,
77
            ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
78
            undefined
79
        )
80
        const newExpression = ts.factory.createNewExpression(
81
            name,
82
            undefined,
83
            [
84
                ts.factory.createIdentifier("undefined"),
85
                emptyObject,
86
                ts.factory.createIdentifier("undefined"),
87
                ts.factory.createIdentifier(LegacyCallTransformer.parameterName),
88
                emptyLambda,
89
                locationObject
90
            ]
91
        )
92
        return ts.factory.createArrowFunction(
93
            undefined,
94
            undefined,
95
            [ elementIdParameter ],
96
            undefined,
97
            ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
98
            newExpression
99
        )
100
    }
101

102
    transformLegacyCall(node: ts.CallExpression): ts.CallExpression {
103
        const name = node.expression as ts.Identifier
104
        const instanceLambda = this.createLambda(name)
105
        return ts.factory.createCallExpression(
106
            ts.factory.createIdentifier(
107
                this.importer.withAdaptorImport(LegacyCallTransformer.LegacyNode)
108
            ),
109
            undefined,
110
            [ instanceLambda ],
111
        )
112
    }
113

114
    visitor(beforeChildren: ts.Node): ts.Node {
115
        const node = this.visitEachChild(beforeChildren)
116

117
        if (ts.isExpressionStatement(node) &&
118
            ts.isCallExpression(node.expression) &&
119
            ts.isIdentifier(node.expression.expression) &&
120
            this.isLegacyStructCall(node.expression)
121
        ) {
122
            return this.transformLegacyCall(node.expression)
123
        }
124

125
        return node
126
    }
127
}
128

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

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

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

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