idlize

Форк
0
/
LazyTransformer.ts 
108 строк · 3.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 { Importer } from './Importer'
19
import { id } from './ApiUtils'
20
import { CallTable, idTextOrError } from './utils'
21

22

23
function statementIsLazyForEach(statement: ts.Statement): boolean {
24
    if (!ts.isExpressionStatement(statement)) return false
25
    if (!ts.isCallExpression(statement.expression)) return false
26
    const callee = statement.expression.expression
27
    if (!ts.isIdentifier(callee)) return false
28
    if (ts.idText(callee) != "LazyForEach") return false
29
    return true
30
}
31

32
function statementIsForEach(statement: ts.Statement): boolean {
33
    if (!ts.isExpressionStatement(statement)) return false
34
    if (!ts.isCallExpression(statement.expression)) return false
35
    const callee = statement.expression.expression
36
    if (!ts.isIdentifier(callee)) return false
37
    if (ts.idText(callee) != "ForEach") return false
38
    return true
39
}
40

41
export class LazyTransformer extends AbstractVisitor {
42
    constructor(
43
        sourceFile: ts.SourceFile,
44
        ctx: ts.TransformationContext,
45
        private importer: Importer,
46
        private callTable: CallTable
47
    ) {
48
        super(sourceFile, ctx)
49
    }
50

51

52
    convertChildIfNeeded(node: ts.Statement): ts.Statement {
53
        if (!statementIsForEach(node)) return node
54

55
        return this.convertForEachToForEachByArray(node as ts.ExpressionStatement)
56
    }
57

58
    convertForEachToForEachByArray(node: ts.ExpressionStatement): ts.ExpressionStatement {
59
        const call = node.expression as ts.CallExpression
60
        const newEts = ts.factory.updateCallExpression(
61
            call,
62
            id(this.importer.withAdaptorImport("LazyForEachByArray")),
63
            call.typeArguments,
64
            call.arguments
65
        )
66
        return ts.factory.updateExpressionStatement(
67
            node,
68
            newEts
69
        )
70
    }
71

72
    convertToLazy(node: ts.EtsComponentExpression): ts.EtsComponentExpression {
73
        const newBody = node.body ?
74
            ts.factory.updateBlock(
75
                node.body,
76
                node.body.statements.map(it => this.convertChildIfNeeded(it))
77
            ) : undefined
78

79
        const name = id("Lazy" + idTextOrError(node.expression))
80
        this.callTable.lazyCalls.add(name)
81
        return ts.factory.updateEtsComponentExpression(
82
            node,
83
            name,
84
            node.arguments,
85
            newBody
86
        )
87
    }
88

89

90
    visitor(beforeChildren: ts.Node): ts.Node {
91
        if (this.importer.__isArkoalaImplementation) return beforeChildren
92

93
        const node = this.visitEachChild(beforeChildren)
94
        if (ts.isEtsComponentExpression(node)) {
95
            const body = node.body
96
            if (!body) return node
97

98
            const lazy = body.statements.find(
99
                it => statementIsLazyForEach(it)
100
            )
101
            if (!lazy) return node
102

103
            this.importer.addAdaptorImport("LazyForEach")
104
            return this.convertToLazy(node)
105
        }
106
        return node
107
    }
108
}
109

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

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

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

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