[JS IR] Don't use full paths when generating comments with a path for file blocks

This commit is contained in:
Zalim Bashorov
2019-11-20 22:57:32 +03:00
parent c05c07f243
commit 3fa431e114
3 changed files with 33 additions and 10 deletions
@@ -44,6 +44,7 @@ class IrModuleToJsTransformer(
statements.addWithComment("block: pre-declaration", preDeclarationBlock)
val generateFilePaths = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_COMMENTS_WITH_FILE_PATH)
val pathPrefixMap = backendContext.configuration.getMap(JSConfigurationKeys.FILE_PATHS_PREFIX_MAP)
module.files.forEach {
val fileStatements = it.accept(IrFileToJsTransformer(), context).statements
@@ -55,7 +56,13 @@ class IrModuleToJsTransformer(
}
if (generateRegionComments || generateFilePaths) {
startComment += "file: ${it.path}"
val originalPath = it.path
val path = pathPrefixMap.entries
.find { (k, _) -> originalPath.startsWith(k) }
?.let { (k, v) -> v + originalPath.substring(k.length) }
?: originalPath
startComment += "file: $path"
}
if (startComment.isNotEmpty()) {
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.serialization.js.ModuleKind;
import java.io.File;
import java.util.List;
import java.util.Map;
public class JSConfigurationKeys {
public static final CompilerConfigurationKey<List<String>> TRANSITIVE_LIBRARIES =
@@ -80,5 +81,10 @@ public class JSConfigurationKeys {
CompilerConfigurationKey.create("generate comments with file path at the start of each file block");
public static final CompilerConfigurationKey<Boolean> GENERATE_REGION_COMMENTS =
CompilerConfigurationKey.create("generate special comments at the start and the end of each file block, it allows to fold them and navigate to them in the IDEA");
CompilerConfigurationKey.create("generate special comments at the start and the end of each file block, " +
"it allows to fold them and navigate to them in the IDEA");
public static final CompilerConfigurationKey<Map<String, String>> FILE_PATHS_PREFIX_MAP =
CompilerConfigurationKey.create("this map used to shorten/replace prefix of paths in comments with file paths, " +
"including region comments");
}
@@ -156,10 +156,10 @@ abstract class BasicBoxTest(
val outputFileName = module.outputFileName(outputDir) + ".js"
val isMainModule = mainModuleName == module.name
generateJavaScriptFile(
testFactory.tmpDir,
file.parent, module, outputFileName, dependencies, allDependencies, friends, modules.size > 1,
!SKIP_SOURCEMAP_REMAPPING.matcher(fileContent).find(),
outputPrefixFile, outputPostfixFile, actualMainCallParameters, testPackage, testFunction,
needsFullIrRuntime, isMainModule
!SKIP_SOURCEMAP_REMAPPING.matcher(fileContent).find(), outputPrefixFile, outputPostfixFile,
actualMainCallParameters, testPackage, testFunction, needsFullIrRuntime, isMainModule
)
when {
@@ -336,6 +336,7 @@ abstract class BasicBoxTest(
private fun TestModule.outputFileName(directory: File) = directory.absolutePath + "/" + outputFileSimpleName() + "_v5"
private fun generateJavaScriptFile(
tmpDir: File,
directory: String,
module: TestModule,
outputFileName: String,
@@ -366,7 +367,7 @@ abstract class BasicBoxTest(
val psiFiles = createPsiFiles(allSourceFiles.sortedBy { it.canonicalPath }.map { it.canonicalPath })
val sourceDirs = (testFiles + additionalFiles).map { File(it).parent }.distinct()
val config = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, incrementalData = null)
val config = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, tmpDir, incrementalData = null)
val outputFile = File(outputFileName)
val incrementalData = IncrementalData()
@@ -377,7 +378,7 @@ abstract class BasicBoxTest(
if (incrementalCompilationChecksEnabled && module.hasFilesToRecompile) {
checkIncrementalCompilation(
sourceDirs, module, kotlinFiles, dependencies, allDependencies, friends, multiModule, remap,
sourceDirs, module, kotlinFiles, dependencies, allDependencies, friends, multiModule, tmpDir, remap,
outputFile, outputPrefixFile, outputPostfixFile, mainCallParameters, incrementalData, testPackage, testFunction, needsFullIrRuntime
)
}
@@ -391,6 +392,7 @@ abstract class BasicBoxTest(
allDependencies: List<String>,
friends: List<String>,
multiModule: Boolean,
tmpDir: File,
remap: Boolean,
outputFile: File,
outputPrefixFile: File?,
@@ -417,7 +419,7 @@ abstract class BasicBoxTest(
.sortedBy { it.canonicalPath }
.map { sourceToTranslationUnit[it]!! }
val recompiledConfig = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, incrementalData)
val recompiledConfig = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, tmpDir, incrementalData)
val recompiledOutputFile = File(outputFile.parentFile, outputFile.nameWithoutExtension + "-recompiled.js")
translateFiles(
@@ -628,8 +630,8 @@ abstract class BasicBoxTest(
private fun createPsiFiles(fileNames: List<String>): List<KtFile> = fileNames.map(this::createPsiFile)
private fun createConfig(
sourceDirs: List<String>, module: TestModule, dependencies: List<String>, allDependencies: List<String>, friends: List<String>,
multiModule: Boolean, incrementalData: IncrementalData?
sourceDirs: List<String>, module: TestModule, dependencies: List<String>, allDependencies: List<String>, friends: List<String>,
multiModule: Boolean, tmpDir: File, incrementalData: IncrementalData?
): JsConfig {
val configuration = environment.configuration.copy()
@@ -673,6 +675,14 @@ abstract class BasicBoxTest(
configuration.put(JSConfigurationKeys.GENERATE_REGION_COMMENTS, true)
configuration.put(
JSConfigurationKeys.FILE_PATHS_PREFIX_MAP,
mapOf(
tmpDir.absolutePath to "<TMP>",
File(".").absolutePath.removeSuffix(".") to ""
)
)
return JsConfig(project, configuration, METADATA_CACHE, (JsConfig.JS_STDLIB + JsConfig.JS_KOTLIN_TEST).toSet())
}