Remove json.org library, use own JSON parser to parse source maps

This commit is contained in:
Alexey Andreev
2017-11-23 15:19:41 +03:00
parent 729715ec62
commit 8514a7706f
8 changed files with 60 additions and 98 deletions
-2
View File
@@ -64,7 +64,6 @@
<fileset dir="${dependencies}" includes="jline3.jar"/>
<fileset dir="${dependencies}" includes="jansi.jar"/>
<fileset dir="${dependencies}" includes="javaslang-2.0.6.jar"/>
<fileset dir="${dependencies}" includes="json-org.jar"/>
<fileset dir="${dependencies}" includes="kotlinx-coroutines-core.jar"/>
<fileset dir="${basedir}/ideaSDK/jps" includes="jps-model.jar"/>
</path>
@@ -402,7 +401,6 @@
<zipfileset src="${dependencies}/jline3.jar"/>
<zipfileset src="${dependencies}/jansi.jar"/>
<zipfileset src="${dependencies}/javaslang-2.0.6.jar"/>
<zipfileset src="${dependencies}/json-org.jar"/>
<zipfileset src="${protobuf.jar}"/>
<zipfileset src="${dependencies}/kotlinx-coroutines-core.jar"/>
-1
View File
@@ -41,7 +41,6 @@ val packagesToExcludeFromDummy =
"one/util/streamex/**",
"org/iq80/snappy/**",
"org/jline/**",
"org/json/**",
"org/xmlpull/**",
"*.txt")
+1 -1
View File
@@ -1,2 +1,2 @@
// EXISTS: out.js
// CONTAINS: out.js.map, \"©∑\\n\"
// CONTAINS: out.js.map, \"\u00a9\u2211\\n\"
-1
View File
@@ -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
-23
View File
@@ -1,23 +0,0 @@
JSON
Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-3
View File
@@ -272,9 +272,6 @@
<!-- node.js -->
<antcall target="download-nodejs-and-npm"/>
<!-- JSON.org -->
<get src="https://repo.maven.apache.org/maven2/org/json/json/20160807/json-20160807.jar" dest="${dependencies}/json-org.jar"/>
</target>
<macrodef name="get-protobuf-and-rename-packages">