[JS] Convert SourceFilePathResolver to Kotlin, fix usages

This commit is contained in:
Sergej Jaskiewicz
2022-05-24 01:29:47 +03:00
committed by Space
parent 4bf79cd23b
commit 0614c519af
3 changed files with 51 additions and 75 deletions
@@ -208,11 +208,7 @@ class IrModuleToJsTransformer(
val sourceMapBuilder = SourceMap3Builder(null, jsCode, sourceMapPrefix) val sourceMapBuilder = SourceMap3Builder(null, jsCode, sourceMapPrefix)
val sourceMapBuilderConsumer = val sourceMapBuilderConsumer =
if (sourceMapsEnabled) { if (sourceMapsEnabled) {
val sourceRoots = configuration.get(JSConfigurationKeys.SOURCE_MAP_SOURCE_ROOTS, emptyList<String>()).map(::File) val pathResolver = SourceFilePathResolver.create(configuration)
val generateRelativePathsInSourceMap = sourceMapPrefix.isEmpty() && sourceRoots.isEmpty()
val outputDir = if (generateRelativePathsInSourceMap) configuration.get(JSConfigurationKeys.OUTPUT_DIR) else null
val pathResolver = SourceFilePathResolver(sourceRoots, outputDir)
val sourceMapContentEmbedding = val sourceMapContentEmbedding =
configuration.get(JSConfigurationKeys.SOURCE_MAP_EMBED_SOURCES, SourceMapSourceEmbedding.INLINING) configuration.get(JSConfigurationKeys.SOURCE_MAP_EMBED_SOURCES, SourceMapSourceEmbedding.INLINING)
@@ -503,4 +499,4 @@ fun processClassModels(
{ klass -> classModelMap[klass]?.superClasses ?: emptyList() }, { klass -> classModelMap[klass]?.superClasses ?: emptyList() },
declarationHandler declarationHandler
) )
} }
@@ -410,11 +410,7 @@ fun generateSingleWrappedModuleBody(
val sourceMapPrefix = sourceMapsInfo.sourceMapPrefix val sourceMapPrefix = sourceMapsInfo.sourceMapPrefix
sourceMapBuilder = SourceMap3Builder(null, jsCode, sourceMapPrefix) sourceMapBuilder = SourceMap3Builder(null, jsCode, sourceMapPrefix)
val sourceRoots = sourceMapsInfo.sourceRoots.map(::File) val pathResolver = SourceFilePathResolver.create(sourceMapsInfo.sourceRoots, sourceMapPrefix, sourceMapsInfo.outputDir)
val generateRelativePathsInSourceMap = sourceMapPrefix.isEmpty() && sourceRoots.isEmpty()
val outputDir = if (generateRelativePathsInSourceMap) sourceMapsInfo.outputDir else null
val pathResolver = SourceFilePathResolver(sourceRoots, outputDir)
val sourceMapContentEmbedding = val sourceMapContentEmbedding =
sourceMapsInfo.sourceMapContentEmbedding sourceMapsInfo.sourceMapContentEmbedding
@@ -2,87 +2,71 @@
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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; class SourceFilePathResolver(sourceRoots: List<File>, outputDir: File? = null) {
import org.jetbrains.annotations.NotNull; private val sourceRoots = sourceRoots.mapTo(mutableSetOf<File>()) { it.absoluteFile }
import org.jetbrains.annotations.Nullable; private val outputDirPathResolver = outputDir?.let(::RelativePathCalculator)
import org.jetbrains.kotlin.js.config.JSConfigurationKeys; private val cache = mutableMapOf<File, String>()
import org.jetbrains.kotlin.js.config.JsConfig;
import java.io.File; @Throws(IOException::class)
import java.io.IOException; fun getPathRelativeToSourceRoots(file: File): String {
import java.util.*; var path = cache[file]
import java.util.stream.Collectors;
public class SourceFilePathResolver {
@NotNull
private final Set<File> sourceRoots;
@Nullable
private final RelativePathCalculator outputDirPathResolver;
@NotNull
private final Map<File, String> cache = new HashMap<>();
public SourceFilePathResolver(@NotNull List<File> sourceRoots) {
this(sourceRoots, null);
}
public SourceFilePathResolver(@NotNull List<File> 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);
if (path == null) { if (path == null) {
path = calculatePathRelativeToSourceRoots(file); path = calculatePathRelativeToSourceRoots(file)
cache.put(file, path); cache[file] = path
} }
return path; return path
} }
@NotNull @Throws(IOException::class)
private String calculatePathRelativeToSourceRoots(@NotNull File file) throws IOException { private fun calculatePathRelativeToSourceRoots(file: File): String {
String pathRelativeToOutput = calculatePathRelativeToOutput(file); val pathRelativeToOutput = calculatePathRelativeToOutput(file)
if (pathRelativeToOutput != null) return pathRelativeToOutput; if (pathRelativeToOutput != null) return pathRelativeToOutput
val parts = mutableListOf<String>()
List<String> parts = new ArrayList<>(); var currentFile: File? = file.canonicalFile
File currentFile = file.getCanonicalFile();
while (currentFile != null) { while (currentFile != null) {
if (sourceRoots.contains(currentFile)) { if (sourceRoots.contains(currentFile)) {
if (parts.isEmpty()) { if (parts.isEmpty()) {
break; break
} }
Collections.reverse(parts); parts.reverse()
return StringUtil.join(parts, File.separator); return parts.joinToString(File.separator)
} }
parts.add(currentFile.getName()); parts.add(currentFile.name)
currentFile = currentFile.getParentFile(); currentFile = currentFile.parentFile
} }
return file.getName(); return file.name
} }
@Nullable private fun calculatePathRelativeToOutput(file: File): String? {
private String calculatePathRelativeToOutput(@NotNull File file) { return outputDirPathResolver?.calculateRelativePathTo(file)
return outputDirPathResolver != null ? outputDirPathResolver.calculateRelativePathTo(file) : null;
} }
@NotNull companion object {
public static SourceFilePathResolver create(@NotNull JsConfig config) { @JvmStatic
List<File> sourceRoots = config.getSourceMapRoots().stream().map(File::new).collect(Collectors.toList()); fun create(config: JsConfig) = create(config.configuration)
File outputDir = null;
if (config.shouldGenerateRelativePathsInSourceMap()) { @JvmStatic
outputDir = config.getConfiguration().get(JSConfigurationKeys.OUTPUT_DIR); 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<String>, sourceMapPrefix: String, outputDir: File?): SourceFilePathResolver {
val generateRelativePathsInSourceMap = sourceMapPrefix.isEmpty() && sourceRoots.isEmpty()
return SourceFilePathResolver(
sourceRoots.map(::File),
outputDir.takeIf { generateRelativePathsInSourceMap }
)
} }
return new SourceFilePathResolver(sourceRoots, outputDir);
} }
} }