[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 sourceMapBuilderConsumer =
if (sourceMapsEnabled) {
val sourceRoots = configuration.get(JSConfigurationKeys.SOURCE_MAP_SOURCE_ROOTS, emptyList<String>()).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
)
}
}
@@ -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
@@ -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<File>, outputDir: File? = null) {
private val sourceRoots = sourceRoots.mapTo(mutableSetOf<File>()) { it.absoluteFile }
private val outputDirPathResolver = outputDir?.let(::RelativePathCalculator)
private val cache = mutableMapOf<File, String>()
import java.io.File;
import java.io.IOException;
import java.util.*;
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);
@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<String> 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<String>()
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<File> 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<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);
}
}