[JS] Support the 'names' field in sourcemaps

This commit is contained in:
Sergej Jaskiewicz
2022-09-29 15:06:42 +02:00
committed by Space Team
parent 227864c6ec
commit 1d0025c3cf
9 changed files with 92 additions and 31 deletions
@@ -28,7 +28,8 @@ class SourceMap(val sourceContentResolver: (String) -> Reader?) {
for ((index, group) in groups.withIndex()) {
writer.print("${index + 1}:")
for (segment in group.segments) {
writer.print(" ${segment.generatedColumnNumber + 1}:${segment.sourceLineNumber + 1},${segment.sourceColumnNumber + 1}")
val nameIfPresent = if (segment.name != null) "(${segment.name})" else ""
writer.print(" ${segment.generatedColumnNumber + 1}:${segment.sourceLineNumber + 1},${segment.sourceColumnNumber + 1}$nameIfPresent")
}
writer.println()
}
@@ -42,8 +43,9 @@ class SourceMap(val sourceContentResolver: (String) -> Reader?) {
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}>")
segmentsByColumn[i]?.let { (_, sourceFile, sourceLine, sourceColumn, name) ->
val nameIfPresent = if (name != null) "($name)" else ""
writer.print("<$sourceFile:${sourceLine + 1}:${sourceColumn + 1}$nameIfPresent>")
}
writer.print(generatedLine[i])
}
@@ -96,7 +98,8 @@ data class SourceMapSegment(
val generatedColumnNumber: Int,
val sourceFileName: String?,
val sourceLineNumber: Int,
val sourceColumnNumber: Int
val sourceColumnNumber: Int,
val name: String?,
)
class SourceMapGroup {
@@ -70,10 +70,10 @@ class SourceMapLocationRemapper(private val sourceMap: SourceMap, private val so
val segment = findCorrespondingSegment(node)
val sourceFileName = segment?.sourceFileName
node.source = if (sourceFileName != null) {
val location = JsLocation(sourceMapPathMapper(sourceFileName), segment.sourceLineNumber, segment.sourceColumnNumber)
val location =
JsLocation(sourceMapPathMapper(sourceFileName), segment.sourceLineNumber, segment.sourceColumnNumber, segment.name)
JsLocationWithEmbeddedSource(location, null) { sourceMap.sourceContentResolver(segment.sourceFileName) }
}
else {
} else {
null
}
}
@@ -86,6 +86,17 @@ object SourceMapParser {
}
}
val names = jsonObject.properties["names"].let {
if (it != null) {
val namesProperty = it as? JsonArray ?: return SourceMapError("'names' property is not of array type")
namesProperty.elements.map {
(it as? JsonString ?: return SourceMapError("'names' array must contain strings")).value
}
} else {
emptyList()
}
}
val sourcePathToContent = sources.zip(sourcesContent).associate { it }
val mappings = jsonObject.properties["mappings"] ?: return SourceMapError("'mappings' property not found")
@@ -95,6 +106,7 @@ object SourceMapParser {
var sourceLine = 0
var sourceColumn = 0
var sourceIndex = 0
var nameIndex = 0
val stream = MappingStream(mappings.value)
val sourceMap = SourceMap { sourcePathToContent[it]?.let { StringReader(it) } }
var currentGroup = SourceMapGroup().also { sourceMap.groups += it }
@@ -113,17 +125,20 @@ object SourceMapParser {
sourceIndex += stream.readInt() ?: return stream.createError("VLQ-encoded source index expected")
sourceLine += stream.readInt() ?: return stream.createError("VLQ-encoded source line expected")
sourceColumn += stream.readInt() ?: return stream.createError("VLQ-encoded source column expected")
if (stream.isEncodedInt) {
stream.readInt() ?: return stream.createError("VLQ-encoded name index expected")
}
val name = if (stream.isEncodedInt) {
nameIndex += stream.readInt() ?: return stream.createError("VLQ-encoded name index expected")
if (nameIndex !in names.indices) {
return stream.createError("Name index $nameIndex is out of bounds ${names.indices}")
}
names[nameIndex]
} else null
if (sourceIndex !in sources.indices) {
return stream.createError("Source index $sourceIndex is out of bounds ${sources.indices}")
}
currentGroup.segments += SourceMapSegment(jsColumn, sourceRoot + sources[sourceIndex], sourceLine, sourceColumn)
}
else {
currentGroup.segments += SourceMapSegment(jsColumn, null, -1, -1)
currentGroup.segments += SourceMapSegment(jsColumn, sourceRoot + sources[sourceIndex], sourceLine, sourceColumn, name)
} else {
currentGroup.segments += SourceMapSegment(jsColumn, null, -1, -1, null)
}
when {