idlize
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
16import * as ts from 'typescript';17import * as path from "path"18import { AnalysisVisitor } from './analysis-visitor';19import { DiagnosticsVisitor } from './diagnostics-visitor';20import { FunctionTransformer } from "./function-transformer"21import { ParameterTransformer } from "./parameter-transformer"22import { Rewrite } from './transformation-context';23import { VariableTypeTransformer } from './variable-type-transformer';24import { Tracer, TransformerOptions } from "./util"25import { DumpVisitor } from './dump-visitor';26import { ThisTransformer } from './this-transformer';27import { ReturnTransformer } from './return-transformer';28
29function get_unmemoized_path(source_path: string, separator: string) {30var koala_root_path = process.cwd().slice(0, process.cwd().lastIndexOf(separator));31var component_path_end_index = source_path.indexOf(separator, koala_root_path.length + 1)32var unmemoized_component_path = source_path.slice(0, component_path_end_index) + separator + "unmemoized";33return unmemoized_component_path + source_path.slice(component_path_end_index, source_path.length)34}
35
36export function memoTransform(37sourceFile: ts.SourceFile,38typeChecker: ts.TypeChecker,39ctx: ts.TransformationContext,40extras: ts.TransformerExtras | undefined,41tracer: Tracer,42rewrite: Rewrite43): ts.SourceFile {44const analysisVisitor = new AnalysisVisitor(tracer, typeChecker, sourceFile, rewrite, ctx)45const diagnosticsVisitor = new DiagnosticsVisitor(tracer, typeChecker, sourceFile, rewrite, extras, ctx)46const functionTransformer = new FunctionTransformer(tracer, typeChecker, sourceFile, rewrite, ctx)47const parameterTransformer = new ParameterTransformer(tracer, typeChecker, sourceFile, rewrite.positionalIdTracker, rewrite.functionTable, ctx)48const thisTransformer = new ThisTransformer(tracer, typeChecker, sourceFile, rewrite.positionalIdTracker, rewrite.functionTable, ctx)49const variableTransformer = new VariableTypeTransformer(tracer, typeChecker, sourceFile, rewrite, ctx)50const returnTransformer = new ReturnTransformer(typeChecker, rewrite.functionTable, ctx)51
52analysisVisitor.visitor(sourceFile)53diagnosticsVisitor.visitor(sourceFile)54
55// The AST tree should be intact above this line.56
57const transformedFunctions = functionTransformer.visitor(sourceFile)58const transformedParameters = parameterTransformer.visitor(transformedFunctions)59const transformedThis = thisTransformer.visitor(transformedParameters)60const transformedReturn = returnTransformer.visitor(transformedThis)61return variableTransformer.visitor(transformedReturn) as ts.SourceFile62}
63
64export default function koala_transformer(program: ts.Program, pluginOptions: TransformerOptions, extras: ts.TransformerExtras) {65const printer = ts.createPrinter()66const tracer = new Tracer(pluginOptions, printer)67return (ctx: ts.TransformationContext) => {68const typechecker = program.getTypeChecker();69return (sourceFile: ts.SourceFile) => {70console.log("Koala rewrite: " + path.normalize(sourceFile.fileName))71const rewrite = new Rewrite(sourceFile, pluginOptions)72const dumpVisitor = new DumpVisitor(tracer, sourceFile, rewrite.functionTable, ctx)73const result = memoTransform(sourceFile, typechecker, ctx, extras, tracer, rewrite)74dumpVisitor.visitor(result)75if (pluginOptions.only_unmemoize) {76const content = tracer.printer.printNode(ts.EmitHint.Unspecified, result, sourceFile)77var rewrite_path = get_unmemoized_path(path.normalize(sourceFile.fileName), path.sep);78tracer.createDirs(rewrite_path.slice(0, rewrite_path.lastIndexOf(path.sep)))79tracer.writeTextToFile(content+"\n", rewrite_path)80} else if (pluginOptions.trace) {81tracer.trace("OUTPUT:\n" + printer.printNode(ts.EmitHint.SourceFile, result, sourceFile))82}83return result84}85}86}
87