From 5a4fe1b4dadc6bb7eaceee789ec0b83faa1d91b7 Mon Sep 17 00:00:00 2001 From: Asger F Date: Thu, 27 Apr 2023 10:55:36 +0200 Subject: [PATCH 1/3] JS: Stop complaining about comments in JSON files --- .../src/com/semmle/js/parser/JSONParser.java | 11 ++++------- .../tests/json/output/trap/comments.json.trap | 10 ---------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/javascript/extractor/src/com/semmle/js/parser/JSONParser.java b/javascript/extractor/src/com/semmle/js/parser/JSONParser.java index 2e7f7a96b5f..58f28644870 100644 --- a/javascript/extractor/src/com/semmle/js/parser/JSONParser.java +++ b/javascript/extractor/src/com/semmle/js/parser/JSONParser.java @@ -21,6 +21,7 @@ import com.semmle.util.exception.Exceptions; import com.semmle.util.io.WholeIO; import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -32,7 +33,6 @@ public class JSONParser { private int offset; private int length; private String src; - private List recoverableErrors; public static Pair> parseValue(String json) throws ParseError { JSONParser parser = new JSONParser(json); @@ -41,14 +41,13 @@ public class JSONParser { parser.consumeWhitespace(); if (parser.offset < parser.length) parser.raise("Expected end of input"); - return Pair.make(value, parser.recoverableErrors); + return Pair.make(value, Collections.emptyList()); } private JSONParser(String json) throws ParseError { this.line = 1; this.column = 0; this.offset = 0; - this.recoverableErrors = new ArrayList(); if (json == null) raise("Input string may not be null"); this.length = json.length(); @@ -351,17 +350,16 @@ public class JSONParser { } } - /** Skips the line comment starting at the current position and records a recoverable error. */ + /** Skips the line comment starting at the current position. */ private void skipLineComment() throws ParseError { Position pos = new Position(line, column, offset); char c; next(); next(); while ((c = peek()) != '\r' && c != '\n' && c != -1) next(); - recoverableErrors.add(new ParseError("Comments are not legal in JSON.", pos)); } - /** Skips the block comment starting at the current position and records a recoverable error. */ + /** Skips the block comment starting at the current position. */ private void skipBlockComment() throws ParseError { Position pos = new Position(line, column, offset); char c; @@ -376,7 +374,6 @@ public class JSONParser { break; } } while (true); - recoverableErrors.add(new ParseError("Comments are not legal in JSON.", pos)); } private void consume(char token) throws ParseError { diff --git a/javascript/extractor/tests/json/output/trap/comments.json.trap b/javascript/extractor/tests/json/output/trap/comments.json.trap index 320a172e3df..4d697f469a1 100644 --- a/javascript/extractor/tests/json/output/trap/comments.json.trap +++ b/javascript/extractor/tests/json/output/trap/comments.json.trap @@ -18,15 +18,5 @@ locations_default(#20003,#10000,3,12,3,18) json_locations(#20002,#20003) json_literals("world","""world""",#20002) json_properties(#20000,"hello",#20002) -#20004=* -json_errors(#20004,"Error: Comments are not legal in JSON.") -#20005=@"loc,{#10000},2,3,2,3" -locations_default(#20005,#10000,2,3,2,3) -json_locations(#20004,#20005) -#20006=* -json_errors(#20006,"Error: Comments are not legal in JSON.") -#20007=@"loc,{#10000},4,3,4,3" -locations_default(#20007,#10000,4,3,4,3) -json_locations(#20006,#20007) numlines(#10000,5,0,0) filetype(#10000,"json") From 410719fd9e5af80e38e241d29bd7850c8f8fedf5 Mon Sep 17 00:00:00 2001 From: Asger F Date: Thu, 27 Apr 2023 10:57:38 +0200 Subject: [PATCH 2/3] Update JSONError.expected --- javascript/ql/test/library-tests/JSON/JSONError.expected | 1 - 1 file changed, 1 deletion(-) diff --git a/javascript/ql/test/library-tests/JSON/JSONError.expected b/javascript/ql/test/library-tests/JSON/JSONError.expected index e63d3862491..e69de29bb2d 100644 --- a/javascript/ql/test/library-tests/JSON/JSONError.expected +++ b/javascript/ql/test/library-tests/JSON/JSONError.expected @@ -1 +0,0 @@ -| invalid.json:3:1:3:1 | Error: Comments are not legal in JSON. | From 1b75afb5b10a74bd1bf726d42e9db28957059dc6 Mon Sep 17 00:00:00 2001 From: Asger F Date: Fri, 28 Apr 2023 14:30:48 +0200 Subject: [PATCH 3/3] JS: Change note --- .../ql/src/change-notes/2023-04-28-json-with-comments.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 javascript/ql/src/change-notes/2023-04-28-json-with-comments.md diff --git a/javascript/ql/src/change-notes/2023-04-28-json-with-comments.md b/javascript/ql/src/change-notes/2023-04-28-json-with-comments.md new file mode 100644 index 00000000000..3ce9949a39a --- /dev/null +++ b/javascript/ql/src/change-notes/2023-04-28-json-with-comments.md @@ -0,0 +1,5 @@ +--- +category: fix +--- +* Fixed a spurious diagnostic warning about comments in JSON files being illegal. + Comments in JSON files are in fact fully supported, and the diagnostic message was misleading.