idlize

Форк
0
/
koala-transformer.ts 
86 строк · 4.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 'typescript';
17
import * as path from "path"
18
import { AnalysisVisitor } from './analysis-visitor';
19
import { DiagnosticsVisitor } from './diagnostics-visitor';
20
import { FunctionTransformer } from "./function-transformer"
21
import { ParameterTransformer } from "./parameter-transformer"
22
import { Rewrite } from './transformation-context';
23
import { VariableTypeTransformer } from './variable-type-transformer';
24
import { Tracer, TransformerOptions } from "./util"
25
import { DumpVisitor } from './dump-visitor';
26
import { ThisTransformer } from './this-transformer';
27
import { ReturnTransformer } from './return-transformer';
28

29
function get_unmemoized_path(source_path: string, separator: string) {
30
    var koala_root_path = process.cwd().slice(0, process.cwd().lastIndexOf(separator));
31
    var component_path_end_index = source_path.indexOf(separator, koala_root_path.length + 1)
32
    var unmemoized_component_path = source_path.slice(0, component_path_end_index) + separator + "unmemoized";
33
    return unmemoized_component_path + source_path.slice(component_path_end_index, source_path.length)
34
}
35

36
export function memoTransform(
37
    sourceFile: ts.SourceFile,
38
    typeChecker: ts.TypeChecker,
39
    ctx: ts.TransformationContext,
40
    extras: ts.TransformerExtras | undefined,
41
    tracer: Tracer,
42
    rewrite: Rewrite
43
): ts.SourceFile {
44
    const analysisVisitor = new AnalysisVisitor(tracer, typeChecker, sourceFile, rewrite, ctx)
45
    const diagnosticsVisitor = new DiagnosticsVisitor(tracer, typeChecker, sourceFile, rewrite, extras, ctx)
46
    const functionTransformer = new FunctionTransformer(tracer, typeChecker, sourceFile, rewrite, ctx)
47
    const parameterTransformer = new ParameterTransformer(tracer, typeChecker, sourceFile, rewrite.positionalIdTracker, rewrite.functionTable, ctx)
48
    const thisTransformer = new ThisTransformer(tracer, typeChecker, sourceFile, rewrite.positionalIdTracker, rewrite.functionTable, ctx)
49
    const variableTransformer = new VariableTypeTransformer(tracer, typeChecker, sourceFile, rewrite, ctx)
50
    const returnTransformer = new ReturnTransformer(typeChecker, rewrite.functionTable, ctx)
51

52
    analysisVisitor.visitor(sourceFile)
53
    diagnosticsVisitor.visitor(sourceFile)
54

55
    // The AST tree should be intact above this line.
56

57
    const transformedFunctions = functionTransformer.visitor(sourceFile)
58
    const transformedParameters = parameterTransformer.visitor(transformedFunctions)
59
    const transformedThis = thisTransformer.visitor(transformedParameters)
60
    const transformedReturn = returnTransformer.visitor(transformedThis)
61
    return variableTransformer.visitor(transformedReturn) as ts.SourceFile
62
}
63

64
export default function koala_transformer(program: ts.Program, pluginOptions: TransformerOptions, extras: ts.TransformerExtras) {
65
    const printer = ts.createPrinter()
66
    const tracer = new Tracer(pluginOptions, printer)
67
    return (ctx: ts.TransformationContext) => {
68
        const typechecker = program.getTypeChecker();
69
        return (sourceFile: ts.SourceFile) => {
70
            console.log("Koala rewrite: " + path.normalize(sourceFile.fileName))
71
            const rewrite = new Rewrite(sourceFile, pluginOptions)
72
            const dumpVisitor = new DumpVisitor(tracer, sourceFile, rewrite.functionTable, ctx)
73
            const result = memoTransform(sourceFile, typechecker, ctx, extras, tracer, rewrite)
74
            dumpVisitor.visitor(result)
75
            if (pluginOptions.only_unmemoize) {
76
                const content = tracer.printer.printNode(ts.EmitHint.Unspecified, result, sourceFile)
77
                var rewrite_path = get_unmemoized_path(path.normalize(sourceFile.fileName), path.sep);
78
                tracer.createDirs(rewrite_path.slice(0, rewrite_path.lastIndexOf(path.sep)))
79
                tracer.writeTextToFile(content+"\n", rewrite_path)
80
            } else if (pluginOptions.trace) {
81
                tracer.trace("OUTPUT:\n" + printer.printNode(ts.EmitHint.SourceFile, result, sourceFile))
82
            }
83
            return result
84
        }
85
    }
86
}
87

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

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

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

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