JSON: Disallow sparse array (#10443)

This commit is contained in:
fisker Cheung authored and GitHub committed 2021-03-01 09:53:32 +08:00
1 parent 1227207909
commit 9324380c53
6 files changed
+166 -1

No files matched your search

+1 -1
View File
@@ -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.
+9
View File
@@ -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;
@@ -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"]
+9
View File
@@ -0,0 +1,9 @@
[
[
1,null],
[1,null,],
[null,],
[0,],
[false,],
['',]
]
@@ -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\\"}
+2
View File
@@ -21,6 +21,8 @@ run_spec(
snippets: [
"packages\\the-hub\\cypress\\fixtures\\gridConfiguration.json",
"1+2",
"[,]",
"[1,,2]",
],
},
["json"]