From 8514a7706f66267c03b3421e6818f3ffe392cd9a Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 23 Nov 2017 15:19:41 +0300 Subject: [PATCH] Remove json.org library, use own JSON parser to parse source maps --- build.xml | 2 - buildSrc/src/main/kotlin/embeddable.kt | 1 - .../testData/cli/js/sourceMapCharEscape.test | 2 +- js/js.parser/build.gradle.kts | 1 - .../js/parser/sourcemaps/SourceMapParser.kt | 77 ++++++++++--------- .../js/sourceMap/SourceMap3Builder.java | 49 ++++-------- license/third_party/json_LICENSE.txt | 23 ------ update_dependencies.xml | 3 - 8 files changed, 60 insertions(+), 98 deletions(-) delete mode 100644 license/third_party/json_LICENSE.txt diff --git a/build.xml b/build.xml index 853f59d8497..4cc8d1f9546 100644 --- a/build.xml +++ b/build.xml @@ -64,7 +64,6 @@ - @@ -402,7 +401,6 @@ - diff --git a/buildSrc/src/main/kotlin/embeddable.kt b/buildSrc/src/main/kotlin/embeddable.kt index 06b66f443b5..8f7c6bc7cfc 100644 --- a/buildSrc/src/main/kotlin/embeddable.kt +++ b/buildSrc/src/main/kotlin/embeddable.kt @@ -41,7 +41,6 @@ val packagesToExcludeFromDummy = "one/util/streamex/**", "org/iq80/snappy/**", "org/jline/**", - "org/json/**", "org/xmlpull/**", "*.txt") diff --git a/compiler/testData/cli/js/sourceMapCharEscape.test b/compiler/testData/cli/js/sourceMapCharEscape.test index 50967e8fe84..4568e450600 100644 --- a/compiler/testData/cli/js/sourceMapCharEscape.test +++ b/compiler/testData/cli/js/sourceMapCharEscape.test @@ -1,2 +1,2 @@ // EXISTS: out.js -// CONTAINS: out.js.map, \"©∑\\n\" +// CONTAINS: out.js.map, \"\u00a9\u2211\\n\" diff --git a/js/js.parser/build.gradle.kts b/js/js.parser/build.gradle.kts index 1cc9cdcaebd..0e5ab7353a5 100644 --- a/js/js.parser/build.gradle.kts +++ b/js/js.parser/build.gradle.kts @@ -5,7 +5,6 @@ dependencies { compile(project(":compiler:util")) compile(project(":js:js.ast")) compile(ideaSdkCoreDeps("intellij-core")) - compile(preloadedDeps("json-org")) } sourceSets { diff --git a/js/js.parser/src/org/jetbrains/kotlin/js/parser/sourcemaps/SourceMapParser.kt b/js/js.parser/src/org/jetbrains/kotlin/js/parser/sourcemaps/SourceMapParser.kt index e1d137928fe..56102ae8254 100644 --- a/js/js.parser/src/org/jetbrains/kotlin/js/parser/sourcemaps/SourceMapParser.kt +++ b/js/js.parser/src/org/jetbrains/kotlin/js/parser/sourcemaps/SourceMapParser.kt @@ -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 = 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 = 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 } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/sourceMap/SourceMap3Builder.java b/js/js.translator/src/org/jetbrains/kotlin/js/sourceMap/SourceMap3Builder.java index 43db1fe0f15..fac20451593 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/sourceMap/SourceMap3Builder.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/sourceMap/SourceMap3Builder.java @@ -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 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 diff --git a/license/third_party/json_LICENSE.txt b/license/third_party/json_LICENSE.txt deleted file mode 100644 index 9be5071d079..00000000000 --- a/license/third_party/json_LICENSE.txt +++ /dev/null @@ -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. diff --git a/update_dependencies.xml b/update_dependencies.xml index 3ac3acf2b7d..7800be3738b 100644 --- a/update_dependencies.xml +++ b/update_dependencies.xml @@ -272,9 +272,6 @@ - - -