From 09d24debf033c69ef1bbbf891f45c180f7584ae4 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 21 Apr 2020 19:38:30 +0800 Subject: [PATCH] Test json parse errors (#8103) --- src/language-js/parser-babel.js | 6 +-- .../json/__snapshots__/jsfmt.spec.js.snap | 37 +++++++++++++++++++ tests/errors/json/jsfmt.spec.js | 14 +++++++ 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 tests/errors/json/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/errors/json/jsfmt.spec.js diff --git a/src/language-js/parser-babel.js b/src/language-js/parser-babel.js index 9121ef6f4..3d9bc7406 100644 --- a/src/language-js/parser-babel.js +++ b/src/language-js/parser-babel.js @@ -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 diff --git a/tests/errors/json/__snapshots__/jsfmt.spec.js.snap b/tests/errors/json/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..3c7fb6bc4 --- /dev/null +++ b/tests/errors/json/__snapshots__/jsfmt.spec.js.snap @@ -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\\": () => {}} + | ^" +`; diff --git a/tests/errors/json/jsfmt.spec.js b/tests/errors/json/jsfmt.spec.js new file mode 100644 index 000000000..38ba22de3 --- /dev/null +++ b/tests/errors/json/jsfmt.spec.js @@ -0,0 +1,14 @@ +run_spec( + { + dirname: __dirname, + snippets: [ + "{foo}", + '{["foo"]:"bar"}', + '{"foo": ~1}', + '{"foo": false || "bar"}', + '{"foo": undefined}', + '{"foo": () => {}}', + ], + }, + ["json-stringify"] +);