Remove json.org library, use own JSON parser to parse source maps
This commit is contained in:
@@ -5,7 +5,6 @@ dependencies {
|
||||
compile(project(":compiler:util"))
|
||||
compile(project(":js:js.ast"))
|
||||
compile(ideaSdkCoreDeps("intellij-core"))
|
||||
compile(preloadedDeps("json-org"))
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.parser.sourcemaps
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import org.json.JSONTokener
|
||||
import java.io.IOException
|
||||
import java.io.Reader
|
||||
import java.io.StringReader
|
||||
@@ -28,57 +24,68 @@ object SourceMapParser {
|
||||
@Throws(IOException::class)
|
||||
fun parse(reader: Reader): SourceMapParseResult {
|
||||
val jsonObject = try {
|
||||
JSONObject(JSONTokener(reader))
|
||||
parseJson(reader)
|
||||
}
|
||||
catch (e: JSONException) {
|
||||
catch (e: JsonSyntaxException) {
|
||||
return SourceMapError(e.message ?: "parse error")
|
||||
}
|
||||
|
||||
if (!jsonObject.has("version")) return SourceMapError("Version not defined")
|
||||
jsonObject.get("version").let {
|
||||
if (it != 3) return SourceMapError("Unsupported version: $it")
|
||||
if (jsonObject !is JsonObject) return SourceMapError("Top-level object expected")
|
||||
|
||||
val version = jsonObject.properties["version"] ?: return SourceMapError("Version not defined")
|
||||
version.let {
|
||||
if (version !is JsonNumber || version.value != 3.0) return SourceMapError("Unsupported version: $it")
|
||||
}
|
||||
|
||||
val sourceRoot = if (jsonObject.has("sourceRoot")) {
|
||||
jsonObject.get("sourceRoot") as? String ?: return SourceMapError("'sourceRoot' property is not of string type")
|
||||
}
|
||||
else {
|
||||
""
|
||||
}
|
||||
|
||||
val sources = if (jsonObject.has("sources")) {
|
||||
val sourcesProperty = jsonObject.get("sources") as? JSONArray ?:
|
||||
return SourceMapError("'sources' property is not of array type")
|
||||
sourcesProperty.map {
|
||||
it as? String ?: return SourceMapError("'sources' array must contain strings")
|
||||
val sourceRoot = jsonObject.properties["sourceRoot"].let {
|
||||
if (it != null) {
|
||||
(it as? JsonString ?: return SourceMapError("'sourceRoot' property is not of string type")).value
|
||||
}
|
||||
else {
|
||||
""
|
||||
}
|
||||
}
|
||||
else {
|
||||
emptyList()
|
||||
|
||||
val sources = jsonObject.properties["sources"].let {
|
||||
if (it != null) {
|
||||
val sourcesProperty = it as? JsonArray ?:
|
||||
return SourceMapError("'sources' property is not of array type")
|
||||
sourcesProperty.elements.map {
|
||||
(it as? JsonString ?: return SourceMapError("'sources' array must contain strings")).value
|
||||
}
|
||||
}
|
||||
else {
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
val sourcesContent: List<String?> = if (jsonObject.has("sourcesContent")) {
|
||||
val sourcesContentProperty = jsonObject.get("sourcesContent") as? JSONArray ?:
|
||||
return SourceMapError("'sourcesContent' property is not of array type")
|
||||
if (sourcesContentProperty.any { it != JSONObject.NULL && it !is String? }) {
|
||||
return SourceMapError("'sources' array must contain strings")
|
||||
val sourcesContent: List<String?> = jsonObject.properties["sourcesContent"].let {
|
||||
if (it != null) {
|
||||
val sourcesContentProperty = it as? JsonArray ?:
|
||||
return SourceMapError("'sourcesContent' property is not of array type")
|
||||
sourcesContentProperty.elements.map {
|
||||
when (it) {
|
||||
is JsonNull -> null
|
||||
is JsonString -> it.value
|
||||
else -> return SourceMapError("'sources' array must contain strings")
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
emptyList()
|
||||
}
|
||||
sourcesContentProperty.map { if (it == JSONObject.NULL) null else it as String? }
|
||||
}
|
||||
else {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
val sourcePathToContent = sources.zip(sourcesContent).associate { it }
|
||||
|
||||
if (!jsonObject.has("mappings")) return SourceMapError("'mappings' property not found")
|
||||
val mappings = jsonObject.get("mappings") as? String ?: return SourceMapError("'mappings' property is not of string type")
|
||||
val mappings = jsonObject.properties["mappings"] ?: return SourceMapError("'mappings' property not found")
|
||||
if (mappings !is JsonString) return SourceMapError("'mappings' property is not of string type")
|
||||
|
||||
var jsColumn = 0
|
||||
var sourceLine = 0
|
||||
var sourceColumn = 0
|
||||
var sourceIndex = 0
|
||||
val stream = MappingStream(mappings)
|
||||
val stream = MappingStream(mappings.value)
|
||||
val sourceMap = SourceMap { sourcePathToContent[it]?.let { StringReader(it) } }
|
||||
var currentGroup = SourceMapGroup().also { sourceMap.groups += it }
|
||||
|
||||
|
||||
@@ -21,13 +21,11 @@ import gnu.trove.TObjectIntHashMap;
|
||||
import kotlin.io.TextStreamsKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.js.parser.sourcemaps.*;
|
||||
import org.jetbrains.kotlin.js.util.TextOutput;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
@@ -72,44 +70,31 @@ public class SourceMap3Builder implements SourceMapBuilder {
|
||||
|
||||
@Override
|
||||
public String build() {
|
||||
try {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JSONWriter writer = new JSONWriter(stringWriter);
|
||||
writer.object();
|
||||
writer.key("version").value(3);
|
||||
writer.key("file").value(generatedFile.getName());
|
||||
|
||||
appendSources(writer);
|
||||
appendSourcesContent(writer);
|
||||
|
||||
writer.key("names").array().endArray();
|
||||
writer.key("mappings").value(out.toString());
|
||||
|
||||
writer.endObject();
|
||||
stringWriter.close();
|
||||
|
||||
return stringWriter.toString();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new AssertionError("This exception should have not been thrown from StringWriter", e);
|
||||
}
|
||||
JsonObject json = new JsonObject();
|
||||
json.getProperties().put("version", new JsonNumber(3));
|
||||
json.getProperties().put("file", new JsonString(generatedFile.getName()));
|
||||
appendSources(json);
|
||||
appendSourcesContent(json);
|
||||
json.getProperties().put("names", new JsonArray());
|
||||
json.getProperties().put("mappings", new JsonString(out.toString()));
|
||||
return json.toString();
|
||||
}
|
||||
|
||||
private void appendSources(JSONWriter writer) {
|
||||
writer.key("sources").array();
|
||||
private void appendSources(JsonObject json) {
|
||||
JsonArray array = new JsonArray();
|
||||
for (String source : orderedSources) {
|
||||
writer.value(pathPrefix + source);
|
||||
array.getElements().add(new JsonString(pathPrefix + source));
|
||||
}
|
||||
writer.endArray();
|
||||
json.getProperties().put("sources", array);
|
||||
}
|
||||
|
||||
private void appendSourcesContent(JSONWriter writer) {
|
||||
writer.key("sourcesContent").array();
|
||||
private void appendSourcesContent(JsonObject json) {
|
||||
JsonArray array = new JsonArray();
|
||||
for (Supplier<Reader> contentSupplier : orderedSourceContentSuppliers) {
|
||||
Reader reader = contentSupplier.get();
|
||||
writer.value(reader != null ? TextStreamsKt.readText(reader) : null);
|
||||
array.getElements().add(reader != null ? new JsonString(TextStreamsKt.readText(reader)) : JsonNull.INSTANCE);
|
||||
}
|
||||
writer.endArray();
|
||||
json.getProperties().put("sourcesContent", array);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user