diff --git a/changelog_unreleased/json/10346.md b/changelog_unreleased/json/10346.md index 1bbccf9d5..2d42024cf 100644 --- a/changelog_unreleased/json/10346.md +++ b/changelog_unreleased/json/10346.md @@ -1,4 +1,4 @@ -#### Stricter JSON parser (#10346 by @fisker) +#### Stricter JSON parser (#10346, #10443 by @fisker) Previously we allow all kinds of JavaScript expressions in `json` and `json5` parser, now they are stricter. diff --git a/src/language-js/parser/json.js b/src/language-js/parser/json.js index 53c110e7b..f7aa95e33 100644 --- a/src/language-js/parser/json.js +++ b/src/language-js/parser/json.js @@ -35,6 +35,15 @@ function assertJsonNode(node, parent) { switch (node.type) { case "ArrayExpression": for (const element of node.elements) { + if (element === null) { + throw createError("Sparse array is not allowed in JSON.", { + start: { + line: node.loc.start.line, + column: node.loc.start.column + 1, + }, + }); + } + assertJsonChildNode(element); } return; diff --git a/tests/json/json/__snapshots__/jsfmt.spec.js.snap b/tests/json/json/__snapshots__/jsfmt.spec.js.snap index 74094e36e..31e7f1547 100644 --- a/tests/json/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/json/json/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,138 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`array.json - {"trailingComma":"all"} format 1`] = ` +====================================options===================================== +parsers: ["json"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +[ + [ +1,null], + [1,null,], + [null,], + [0,], + [false,], + ['',] +] + +=====================================output===================================== +[[1, null], [1, null], [null], [0], [false], [""]] + +================================================================================ +`; + +exports[`array.json - {"trailingComma":"all"} format 2`] = ` +====================================options===================================== +parsers: ["json5"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +[ + [ +1,null], + [1,null,], + [null,], + [0,], + [false,], + ['',] +] + +=====================================output===================================== +[[1, null], [1, null], [null], [0], [false], [""]] + +================================================================================ +`; + +exports[`array.json format 1`] = ` +====================================options===================================== +parsers: ["json"] +printWidth: 80 + | printWidth +=====================================input====================================== +[ + [ +1,null], + [1,null,], + [null,], + [0,], + [false,], + ['',] +] + +=====================================output===================================== +[[1, null], [1, null], [null], [0], [false], [""]] + +================================================================================ +`; + +exports[`array.json format 2`] = ` +====================================options===================================== +parsers: ["json5"] +printWidth: 80 + | printWidth +=====================================input====================================== +[ + [ +1,null], + [1,null,], + [null,], + [0,], + [false,], + ['',] +] + +=====================================output===================================== +[[1, null], [1, null], [null], [0], [false], [""]] + +================================================================================ +`; + +exports[`array.json format 3`] = ` +====================================options===================================== +parsers: ["json-stringify"] +printWidth: 80 + | printWidth +=====================================input====================================== +[ + [ +1,null], + [1,null,], + [null,], + [0,], + [false,], + ['',] +] + +=====================================output===================================== +[ + [ + 1, + null + ], + [ + 1, + null + ], + [ + null + ], + [ + 0 + ], + [ + false + ], + [ + "" + ] +] + +================================================================================ +`; + exports[`boolean.json - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["json"] diff --git a/tests/json/json/array.json b/tests/json/json/array.json new file mode 100644 index 000000000..6ca5a28a4 --- /dev/null +++ b/tests/json/json/array.json @@ -0,0 +1,9 @@ +[ + [ +1,null], + [1,null,], + [null,], + [0,], + [false,], + ['',] +] diff --git a/tests/misc/errors/json/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/json/__snapshots__/jsfmt.spec.js.snap index 488f87de9..4b26a300f 100644 --- a/tests/misc/errors/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/json/__snapshots__/jsfmt.spec.js.snap @@ -24,12 +24,24 @@ exports[`snippet: #1 [json-stringify] format 1`] = ` | ^" `; +exports[`snippet: #2 [json] format 1`] = ` +"Sparse array is not allowed in JSON. (1:1) +> 1 | [,] + | ^" +`; + exports[`snippet: #2 [json-stringify] format 1`] = ` "UnaryExpression with operator=\\"~\\" is not allowed in JSON. (1:9) > 1 | {\\"foo\\": ~1} | ^" `; +exports[`snippet: #3 [json] format 1`] = ` +"Sparse array is not allowed in JSON. (1:1) +> 1 | [1,,2] + | ^" +`; + exports[`snippet: #3 [json-stringify] format 1`] = ` "LogicalExpression is not allowed in JSON. (1:9) > 1 | {\\"foo\\": false || \\"bar\\"} diff --git a/tests/misc/errors/json/jsfmt.spec.js b/tests/misc/errors/json/jsfmt.spec.js index 725a04abd..e26dd130f 100644 --- a/tests/misc/errors/json/jsfmt.spec.js +++ b/tests/misc/errors/json/jsfmt.spec.js @@ -21,6 +21,8 @@ run_spec( snippets: [ "packages\\the-hub\\cypress\\fixtures\\gridConfiguration.json", "1+2", + "[,]", + "[1,,2]", ], }, ["json"]