Test json parse errors (#8103)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-04-21 14:38:30 +03:00
1 parent d2f51d7494
commit 09d24debf0
3 files changed
+52 -5

No files matched your search

+1 -5
View File
@@ -140,11 +140,10 @@ function assertJsonNode(node, parent) {
case "ObjectExpression":
return node.properties.forEach(assertJsonChildNode);
case "ObjectProperty":
// istanbul ignore if
if (node.computed) {
throw createJsonError("computed");
}
// istanbul ignore if
if (node.shorthand) {
throw createJsonError("shorthand");
}
@@ -154,7 +153,6 @@ function assertJsonNode(node, parent) {
case "+":
case "-":
return assertJsonChildNode(node.argument);
// istanbul ignore next
default:
throw createJsonError("operator");
}
@@ -168,7 +166,6 @@ function assertJsonNode(node, parent) {
case "NumericLiteral":
case "StringLiteral":
return;
// istanbul ignore next
default:
throw createJsonError();
}
@@ -177,7 +174,6 @@ function assertJsonNode(node, parent) {
return assertJsonNode(child, node);
}
// istanbul ignore next
function createJsonError(attribute) {
const name = !attribute
? node.type
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`snippet: #0 error test 1`] = `
"ObjectProperty with shorthand=true is not allowed in JSON. (1:2)
> 1 | {foo}
| ^"
`;
exports[`snippet: #1 error test 1`] = `
"ObjectProperty with computed=true is not allowed in JSON. (1:2)
> 1 | {[\\"foo\\"]:\\"bar\\"}
| ^"
`;
exports[`snippet: #2 error test 1`] = `
"UnaryExpression with operator=\\"~\\" is not allowed in JSON. (1:9)
> 1 | {\\"foo\\": ~1}
| ^"
`;
exports[`snippet: #3 error test 1`] = `
"LogicalExpression is not allowed in JSON. (1:9)
> 1 | {\\"foo\\": false || \\"bar\\"}
| ^"
`;
exports[`snippet: #4 error test 1`] = `
"Identifier is not allowed in JSON. (1:9)
> 1 | {\\"foo\\": undefined}
| ^"
`;
exports[`snippet: #5 error test 1`] = `
"ArrowFunctionExpression is not allowed in JSON. (1:9)
> 1 | {\\"foo\\": () => {}}
| ^"
`;
+14
View File
@@ -0,0 +1,14 @@
run_spec(
{
dirname: __dirname,
snippets: [
"{foo}",
'{["foo"]:"bar"}',
'{"foo": ~1}',
'{"foo": false || "bar"}',
'{"foo": undefined}',
'{"foo": () => {}}',
],
},
["json-stringify"]
);