From 0614c519afea7f84348b28997f0c86b7cebbf1d4 Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Tue, 24 May 2022 01:29:47 +0300 Subject: [PATCH] [JS] Convert SourceFilePathResolver to Kotlin, fix usages --- .../irToJs/IrModuleToJsTransformer.kt | 8 +- .../irToJs/IrModuleToJsTransformerTmp.kt | 6 +- .../js/sourceMap/SourceFilePathResolver.kt | 112 ++++++++---------- 3 files changed, 51 insertions(+), 75 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformer.kt index e3e6d4e3cff..86103e872b4 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformer.kt @@ -208,11 +208,7 @@ class IrModuleToJsTransformer( val sourceMapBuilder = SourceMap3Builder(null, jsCode, sourceMapPrefix) val sourceMapBuilderConsumer = if (sourceMapsEnabled) { - val sourceRoots = configuration.get(JSConfigurationKeys.SOURCE_MAP_SOURCE_ROOTS, emptyList()).map(::File) - val generateRelativePathsInSourceMap = sourceMapPrefix.isEmpty() && sourceRoots.isEmpty() - val outputDir = if (generateRelativePathsInSourceMap) configuration.get(JSConfigurationKeys.OUTPUT_DIR) else null - - val pathResolver = SourceFilePathResolver(sourceRoots, outputDir) + val pathResolver = SourceFilePathResolver.create(configuration) val sourceMapContentEmbedding = configuration.get(JSConfigurationKeys.SOURCE_MAP_EMBED_SOURCES, SourceMapSourceEmbedding.INLINING) @@ -503,4 +499,4 @@ fun processClassModels( { klass -> classModelMap[klass]?.superClasses ?: emptyList() }, declarationHandler ) -} \ No newline at end of file +} diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformerTmp.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformerTmp.kt index 7e454cf410b..16bdbc9ffe6 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformerTmp.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrModuleToJsTransformerTmp.kt @@ -410,11 +410,7 @@ fun generateSingleWrappedModuleBody( val sourceMapPrefix = sourceMapsInfo.sourceMapPrefix sourceMapBuilder = SourceMap3Builder(null, jsCode, sourceMapPrefix) - val sourceRoots = sourceMapsInfo.sourceRoots.map(::File) - val generateRelativePathsInSourceMap = sourceMapPrefix.isEmpty() && sourceRoots.isEmpty() - val outputDir = if (generateRelativePathsInSourceMap) sourceMapsInfo.outputDir else null - - val pathResolver = SourceFilePathResolver(sourceRoots, outputDir) + val pathResolver = SourceFilePathResolver.create(sourceMapsInfo.sourceRoots, sourceMapPrefix, sourceMapsInfo.outputDir) val sourceMapContentEmbedding = sourceMapsInfo.sourceMapContentEmbedding diff --git a/js/js.sourcemap/src/org/jetbrains/kotlin/js/sourceMap/SourceFilePathResolver.kt b/js/js.sourcemap/src/org/jetbrains/kotlin/js/sourceMap/SourceFilePathResolver.kt index abcf1f7a219..009fbbab1be 100644 --- a/js/js.sourcemap/src/org/jetbrains/kotlin/js/sourceMap/SourceFilePathResolver.kt +++ b/js/js.sourcemap/src/org/jetbrains/kotlin/js/sourceMap/SourceFilePathResolver.kt @@ -2,87 +2,71 @@ * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +package org.jetbrains.kotlin.js.sourceMap -package org.jetbrains.kotlin.js.sourceMap; +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.js.config.JSConfigurationKeys +import org.jetbrains.kotlin.js.config.JsConfig +import java.io.File +import java.io.IOException -import com.intellij.openapi.util.text.StringUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.js.config.JSConfigurationKeys; -import org.jetbrains.kotlin.js.config.JsConfig; +class SourceFilePathResolver(sourceRoots: List, outputDir: File? = null) { + private val sourceRoots = sourceRoots.mapTo(mutableSetOf()) { it.absoluteFile } + private val outputDirPathResolver = outputDir?.let(::RelativePathCalculator) + private val cache = mutableMapOf() -import java.io.File; -import java.io.IOException; -import java.util.*; -import java.util.stream.Collectors; - -public class SourceFilePathResolver { - @NotNull - private final Set sourceRoots; - - @Nullable - private final RelativePathCalculator outputDirPathResolver; - - @NotNull - private final Map cache = new HashMap<>(); - - public SourceFilePathResolver(@NotNull List sourceRoots) { - this(sourceRoots, null); - } - - public SourceFilePathResolver(@NotNull List sourceRoots, @Nullable File outputDir) { - this.sourceRoots = new HashSet<>(); - for (File sourceRoot : sourceRoots) { - this.sourceRoots.add(sourceRoot.getAbsoluteFile()); - } - - outputDirPathResolver = outputDir != null ? new RelativePathCalculator(outputDir) : null; - } - - @NotNull - public String getPathRelativeToSourceRoots(@NotNull File file) throws IOException { - String path = cache.get(file); + @Throws(IOException::class) + fun getPathRelativeToSourceRoots(file: File): String { + var path = cache[file] if (path == null) { - path = calculatePathRelativeToSourceRoots(file); - cache.put(file, path); + path = calculatePathRelativeToSourceRoots(file) + cache[file] = path } - return path; + return path } - @NotNull - private String calculatePathRelativeToSourceRoots(@NotNull File file) throws IOException { - String pathRelativeToOutput = calculatePathRelativeToOutput(file); - if (pathRelativeToOutput != null) return pathRelativeToOutput; - - List parts = new ArrayList<>(); - File currentFile = file.getCanonicalFile(); - + @Throws(IOException::class) + private fun calculatePathRelativeToSourceRoots(file: File): String { + val pathRelativeToOutput = calculatePathRelativeToOutput(file) + if (pathRelativeToOutput != null) return pathRelativeToOutput + val parts = mutableListOf() + var currentFile: File? = file.canonicalFile while (currentFile != null) { if (sourceRoots.contains(currentFile)) { if (parts.isEmpty()) { - break; + break } - Collections.reverse(parts); - return StringUtil.join(parts, File.separator); + parts.reverse() + return parts.joinToString(File.separator) } - parts.add(currentFile.getName()); - currentFile = currentFile.getParentFile(); + parts.add(currentFile.name) + currentFile = currentFile.parentFile } - return file.getName(); + return file.name } - @Nullable - private String calculatePathRelativeToOutput(@NotNull File file) { - return outputDirPathResolver != null ? outputDirPathResolver.calculateRelativePathTo(file) : null; + private fun calculatePathRelativeToOutput(file: File): String? { + return outputDirPathResolver?.calculateRelativePathTo(file) } - @NotNull - public static SourceFilePathResolver create(@NotNull JsConfig config) { - List sourceRoots = config.getSourceMapRoots().stream().map(File::new).collect(Collectors.toList()); - File outputDir = null; - if (config.shouldGenerateRelativePathsInSourceMap()) { - outputDir = config.getConfiguration().get(JSConfigurationKeys.OUTPUT_DIR); + companion object { + @JvmStatic + fun create(config: JsConfig) = create(config.configuration) + + @JvmStatic + fun create(configuration: CompilerConfiguration) = create( + sourceRoots = configuration.get(JSConfigurationKeys.SOURCE_MAP_SOURCE_ROOTS, emptyList()), + sourceMapPrefix = configuration.get(JSConfigurationKeys.SOURCE_MAP_PREFIX, ""), + outputDir = configuration.get(JSConfigurationKeys.OUTPUT_DIR) + ) + + @JvmStatic + fun create(sourceRoots: List, sourceMapPrefix: String, outputDir: File?): SourceFilePathResolver { + val generateRelativePathsInSourceMap = sourceMapPrefix.isEmpty() && sourceRoots.isEmpty() + return SourceFilePathResolver( + sourceRoots.map(::File), + outputDir.takeIf { generateRelativePathsInSourceMap } + ) } - return new SourceFilePathResolver(sourceRoots, outputDir); } }