Compare JS source maps in readable way in IC tests
This commit is contained in:
+25
-4
@@ -17,7 +17,11 @@
|
||||
package org.jetbrains.kotlin.incremental.testingUtils
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream
|
||||
import org.jetbrains.kotlin.incremental.LocalFileKotlinClass
|
||||
import org.jetbrains.kotlin.js.parser.sourcemaps.SourceMapError
|
||||
import org.jetbrains.kotlin.js.parser.sourcemaps.SourceMapParser
|
||||
import org.jetbrains.kotlin.js.parser.sourcemaps.SourceMapSuccess
|
||||
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
import org.jetbrains.kotlin.metadata.DebugProtoBuf
|
||||
import org.jetbrains.kotlin.metadata.js.DebugJsProtoBuf
|
||||
@@ -32,10 +36,7 @@ import org.jetbrains.org.objectweb.asm.ClassReader
|
||||
import org.jetbrains.org.objectweb.asm.util.TraceClassVisitor
|
||||
import org.junit.Assert
|
||||
import org.junit.Assert.assertNotNull
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.File
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
import java.util.zip.CRC32
|
||||
import java.util.zip.GZIPInputStream
|
||||
@@ -186,6 +187,22 @@ private fun metaJsToString(metaJsFile: File): String {
|
||||
return out.toString()
|
||||
}
|
||||
|
||||
private fun sourceMapFileToString(sourceMapFile: File, generatedJsFile: File): String {
|
||||
val sourceMapParseResult = SourceMapParser.parse(StringReader(sourceMapFile.readText()))
|
||||
return when (sourceMapParseResult) {
|
||||
is SourceMapSuccess -> {
|
||||
val bytesOut = ByteArrayOutputStream()
|
||||
PrintStream(bytesOut).use { printStream ->
|
||||
sourceMapParseResult.value.debugVerbose(printStream, generatedJsFile)
|
||||
}
|
||||
bytesOut.toString()
|
||||
}
|
||||
is SourceMapError -> {
|
||||
sourceMapParseResult.message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getExtensionRegistry(): ExtensionRegistry {
|
||||
val registry = ExtensionRegistry.newInstance()!!
|
||||
DebugJvmProtoBuf.registerAllExtensions(registry)
|
||||
@@ -200,6 +217,10 @@ private fun fileToStringRepresentation(file: File): String {
|
||||
file.name.endsWith(KotlinJavascriptMetadataUtils.META_JS_SUFFIX) -> {
|
||||
metaJsToString(file)
|
||||
}
|
||||
file.name.endsWith(".js.map") -> {
|
||||
val generatedJsPath = file.canonicalPath.removeSuffix(".map")
|
||||
sourceMapFileToString(file, File(generatedJsPath))
|
||||
}
|
||||
else -> {
|
||||
file.readText()
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.parser.sourcemaps
|
||||
|
||||
import java.io.File
|
||||
import java.io.PrintStream
|
||||
import java.io.Reader
|
||||
|
||||
@@ -31,13 +32,30 @@ class SourceMap(val sourceContentResolver: (String) -> Reader?) {
|
||||
writer.println()
|
||||
}
|
||||
}
|
||||
|
||||
fun debugVerbose(writer: PrintStream, generatedJsFile: File) {
|
||||
assert(generatedJsFile.exists()) { "$generatedJsFile does not exist!" }
|
||||
val generatedLines = generatedJsFile.readLines().toTypedArray()
|
||||
for ((index, group) in groups.withIndex()) {
|
||||
writer.print("${index + 1}:")
|
||||
val generatedLine = generatedLines[index]
|
||||
val segmentsByColumn = group.segments.map { it.generatedColumnNumber to it }.toMap()
|
||||
for (i in generatedLine.indices) {
|
||||
segmentsByColumn[i]?.let { (_, sourceFile, sourceLine, sourceColumn) ->
|
||||
writer.print("<$sourceFile:${sourceLine + 1}:${sourceColumn + 1}>")
|
||||
}
|
||||
writer.print(generatedLine[i])
|
||||
}
|
||||
writer.println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SourceMapSegment(
|
||||
val generatedColumnNumber: Int,
|
||||
val sourceFileName: String?,
|
||||
val sourceLineNumber: Int,
|
||||
val sourceColumnNumber: Int
|
||||
data class SourceMapSegment(
|
||||
val generatedColumnNumber: Int,
|
||||
val sourceFileName: String?,
|
||||
val sourceLineNumber: Int,
|
||||
val sourceColumnNumber: Int
|
||||
)
|
||||
|
||||
class SourceMapGroup {
|
||||
|
||||
Reference in New Issue
Block a user