diff --git a/src/language-json/parser-json.js b/src/language-json/parser-json.js index f9804a189..b2f047645 100644 --- a/src/language-json/parser-json.js +++ b/src/language-json/parser-json.js @@ -16,6 +16,16 @@ function createJsonParse(options = {}) { ranges: true, }); } catch (error) { + if ( + error?.reasonCode === "MissingPlugin" || + error?.reasonCode === "MissingOneOfPlugins" + ) { + throw createBabelParseError({ + message: "Unexpected token", + loc: error.loc, + }); + } + throw createBabelParseError(error); } diff --git a/tests/format/misc/errors/json/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/errors/json/__snapshots__/jsfmt.spec.js.snap index 30ff5ec81..67d31e5fb 100644 --- a/tests/format/misc/errors/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/errors/json/__snapshots__/jsfmt.spec.js.snap @@ -6,6 +6,12 @@ exports[`snippet: #0 [json] format 1`] = ` | ^^^" `; +exports[`snippet: #0 [json] format 2`] = ` +"A 'set' accessor must have exactly one formal parameter. (1:9) +> 1 | [class {set foo() {}}] + | ^" +`; + exports[`snippet: #0 [json-stringify] format 1`] = ` "Shorthand property is not allowed in JSON. (1:2) > 1 | {foo} @@ -24,6 +30,12 @@ exports[`snippet: #1 [json] format 1`] = ` | ^^^^^" `; +exports[`snippet: #1 [json] format 2`] = ` +"Private name #bar is not defined. (1:21) +> 1 | [class {foo() {this.#bar}}] + | ^" +`; + exports[`snippet: #1 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {["foo"]:"bar"} @@ -42,6 +54,12 @@ exports[`snippet: #2 [json] format 1`] = ` | ^^" `; +exports[`snippet: #2 [json] format 2`] = ` +"'await' is only allowed within async functions and at the top levels of modules. (1:14) +> 1 | [function(){ await 1 }] + | ^" +`; + exports[`snippet: #2 [json-stringify] format 1`] = ` "Operator '~' is not allowed in JSON. (1:9) > 1 | {"foo": ~1} @@ -60,6 +78,12 @@ exports[`snippet: #3 [json] format 1`] = ` | ^^^^^^^^^^^^^^" `; +exports[`snippet: #3 [json] format 2`] = ` +"Unexpected token (1:2) +> 1 | [do {}] + | ^" +`; + exports[`snippet: #3 [json-stringify] format 1`] = ` "'LogicalExpression' is not allowed in JSON. (1:9) > 1 | {"foo": false || "bar"} @@ -78,6 +102,12 @@ exports[`snippet: #4 [json] format 1`] = ` | ^^^^^^^^" `; +exports[`snippet: #4 [json] format 2`] = ` +"'import' and 'export' may only appear at the top level. (1:14) +> 1 | [function() {export a from 'a'}] + | ^" +`; + exports[`snippet: #4 [json-stringify] format 1`] = ` "'ArrowFunctionExpression' is not allowed in JSON. (1:9) > 1 | {"foo": () => {}} @@ -96,6 +126,12 @@ exports[`snippet: #5 [json] format 1`] = ` | ^" `; +exports[`snippet: #5 [json] format 2`] = ` +"Unexpected token (1:1) +> 1 | export a from 'a' + | ^" +`; + exports[`snippet: #5 [json-stringify] format 1`] = ` "Expecting Unicode escape sequence \\uXXXX. (1:10) > 1 | packages\\the-hub\\cypress\\fixtures\\gridConfiguration.json @@ -114,6 +150,12 @@ exports[`snippet: #6 [json] format 1`] = ` | ^^^" `; +exports[`snippet: #6 [json] format 2`] = ` +"Unexpected token, expected "," (1:3) +> 1 | [a::b] + | ^" +`; + exports[`snippet: #6 [json-stringify] format 1`] = ` "'BinaryExpression' is not allowed in JSON. (1:1) > 1 | 1+2 @@ -132,6 +174,12 @@ exports[`snippet: #7 [json] format 1`] = ` | ^^^^^^^^" `; +exports[`snippet: #7 [json] format 2`] = ` +"Unexpected token (1:24) +> 1 | [function*() {function.sent}] + | ^" +`; + exports[`snippet: #7 [json-stringify] format 1`] = ` "Shorthand property is not allowed in JSON. (1:2) > 1 | {Infinity} @@ -150,6 +198,12 @@ exports[`snippet: #8 [json] format 1`] = ` | ^^^" `; +exports[`snippet: #8 [json] format 2`] = ` +"Unexpected token (1:2) +> 1 | [throw {}] + | ^" +`; + exports[`snippet: #8 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[key]: 1} @@ -168,6 +222,12 @@ exports[`snippet: #9 [json] format 1`] = ` | ^^^^^" `; +exports[`snippet: #9 [json] format 2`] = ` +"Unexpected token (1:6) +> 1 | [foo(?)] + | ^" +`; + exports[`snippet: #9 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[key()]: 1} @@ -186,6 +246,12 @@ exports[`snippet: #10 [json] format 1`] = ` | ^^^^^^^^^^^^^^^^" `; +exports[`snippet: #10 [json] format 2`] = ` +"Unexpected token (1:2) +> 1 | [@decorator class {}] + | ^" +`; + exports[`snippet: #10 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {['CallExpression']: 1} @@ -204,6 +270,12 @@ exports[`snippet: #11 [json] format 1`] = ` | ^^^^^^^^^^^^^^^" `; +exports[`snippet: #11 [json] format 2`] = ` +"Unexpected token (1:28) +> 1 | [import('a', {type:'json'})] + | ^" +`; + exports[`snippet: #11 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {['StringLiteral']: 1} @@ -222,6 +294,12 @@ exports[`snippet: #12 [json] format 1`] = ` | ^^^^^^^^" `; +exports[`snippet: #12 [json] format 2`] = ` +"Unexpected token (1:3) +> 1 | [1m] + | ^" +`; + exports[`snippet: #12 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {['string']: 1} @@ -240,6 +318,12 @@ exports[`snippet: #13 [json] format 1`] = ` | ^" `; +exports[`snippet: #13 [json] format 2`] = ` +"Unexpected token (1:2) +> 1 | [module {}] + | ^" +`; + exports[`snippet: #13 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[1]: 1} @@ -258,6 +342,12 @@ exports[`snippet: #14 [json] format 1`] = ` | ^^^^^^^^" `; +exports[`snippet: #14 [json] format 2`] = ` +"Unexpected token (1:8) +> 1 | [async do {}] + | ^" +`; + exports[`snippet: #14 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[Infinity]: 1} @@ -276,6 +366,12 @@ exports[`snippet: #15 [json] format 1`] = ` | ^^^^^^^^^" `; +exports[`snippet: #15 [json] format 2`] = ` +"Unexpected token (1:6) +> 1 | [/a/v] + | ^" +`; + exports[`snippet: #15 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[-Infinity]: 1} @@ -294,6 +390,12 @@ exports[`snippet: #16 [json] format 1`] = ` | ^^^^^^^^^^^^^^" `; +exports[`snippet: #16 [json] format 2`] = ` +"Unexpected token (1:28) +> 1 | [class {#foo;bar() {const {#foo: foo} = this;}}] + | ^" +`; + exports[`snippet: #16 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[{key: 'value'}]: 1} @@ -312,6 +414,12 @@ exports[`snippet: #17 [json] format 1`] = ` | ^^" `; +exports[`snippet: #17 [json] format 2`] = ` +"Unexpected token (1:18) +> 1 | [class {accessor foo = 1}] + | ^" +`; + exports[`snippet: #17 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[[]]: 1} @@ -330,6 +438,12 @@ exports[`snippet: #18 [json] format 1`] = ` | ^^^^" `; +exports[`snippet: #18 [json] format 2`] = ` +"Unexpected token (1:34) +> 1 | [import('a', {reflect: 'module'})] + | ^" +`; + exports[`snippet: #18 [json-stringify] format 1`] = ` "Computed key is not allowed in JSON. (1:3) > 1 | {[null]: 1} @@ -348,6 +462,12 @@ exports[`snippet: #19 [json] format 1`] = ` | ^^^^^" `; +exports[`snippet: #19 [json] format 2`] = ` +"\`import\` can only be used in \`import()\` or \`import.meta\`. (1:1) +> 1 | import module a from 'a' + | ^" +`; + exports[`snippet: #19 [json-stringify] format 1`] = ` "Operator '+' before 'CallExpression' is not allowed in JSON. (1:8) > 1 | {key: +foo()} @@ -366,6 +486,12 @@ exports[`snippet: #20 [json] format 1`] = ` | ^^^^^^^^^^" `; +exports[`snippet: #20 [json] format 2`] = ` +"Unexpected token (1:16) +> 1 | [function() { {using a = b} }] + | ^" +`; + exports[`snippet: #20 [json-stringify] format 1`] = ` "Operator 'void' is not allowed in JSON. (1:7) > 1 | {key: void foo()} @@ -385,6 +511,12 @@ exports[`snippet: #21 [json] format 1`] = ` 2 | {}" `; +exports[`snippet: #21 [json] format 2`] = ` +"Unexpected token, expected "," (1:8) +> 1 | {using a = b} + | ^" +`; + exports[`snippet: #21 [json-stringify] format 1`] = ` "Unexpected token (1:1) > 1 | #!/usr/bin/env node @@ -406,6 +538,12 @@ exports[`snippet: #22 [json] format 1`] = ` | ^" `; +exports[`snippet: #22 [json] format 2`] = ` +"Unexpected token (1:2) +> 1 | [#[]] + | ^" +`; + exports[`snippet: #22 [json-stringify] format 1`] = ` "Unexpected token (2:1) 1 | "use strict" @@ -426,6 +564,12 @@ exports[`snippet: #23 [json] format 1`] = ` | ^" `; +exports[`snippet: #23 [json] format 2`] = ` +"Unexpected token (1:2) +> 1 | [#{}] + | ^" +`; + exports[`snippet: #23 [json-stringify] format 1`] = ` "Unexpected token (1:14) > 1 | /* comment */ @@ -444,6 +588,12 @@ exports[`snippet: #24 [json] format 1`] = ` | ^" `; +exports[`snippet: #24 [json] format 2`] = ` +"'BinaryExpression' is not allowed in JSON. (1:2) +> 1 | [foo%bar()] + | ^^^^^^^^^" +`; + exports[`snippet: #24 [json-stringify] format 1`] = ` "Unexpected token (1:11) > 1 | // comment @@ -462,6 +612,12 @@ exports[`snippet: #25 [json] format 1`] = ` | ^" `; +exports[`snippet: #25 [json] format 2`] = ` +"Unexpected token (1:8) +> 1 | ['foo' |> bar] + | ^" +`; + exports[`snippet: #25 [json-stringify] format 1`] = ` "'TemplateLiteral' with expression is not allowed in JSON. (1:7) > 1 | \`foo\${1}bar\` @@ -480,6 +636,12 @@ exports[`snippet: #26 [json] format 1`] = ` | ^^" `; +exports[`snippet: #26 [json] format 2`] = ` +"Unexpected token (1:8) +> 1 | ['foo' |> bar(%)] + | ^" +`; + exports[`snippet: #26 [json-stringify] format 1`] = ` "Operator '-' before 'UnaryExpression' is not allowed in JSON. (1:2) > 1 | -+1 @@ -498,6 +660,12 @@ exports[`snippet: #27 [json] format 1`] = ` | ^^^^^^^^^" `; +exports[`snippet: #27 [json] format 2`] = ` +"Unexpected token (1:2) +> 1 | [] + | ^" +`; + exports[`snippet: #27 [json-stringify] format 1`] = ` "Operator '-' before 'UnaryExpression' is not allowed in JSON. (1:2) > 1 | -+Infinity diff --git a/tests/format/misc/errors/json/jsfmt.spec.js b/tests/format/misc/errors/json/jsfmt.spec.js index 86c48f589..3a9390d5f 100644 --- a/tests/format/misc/errors/json/jsfmt.spec.js +++ b/tests/format/misc/errors/json/jsfmt.spec.js @@ -42,3 +42,62 @@ run_spec( }, ["json", "json5", "json-stringify"] ); + +run_spec( + { + importMeta: import.meta, + snippets: [ + // Invalid JS expressions + "[class {set foo() {}}]", + "[class {foo() {this.#bar}}]", + "[function(){ await 1 }]", + // Should not mention babel plugin in error message + // plugin `doExpressions` + "[do {}]", + // plugin `exportDefaultFrom` + "[function() {export a from 'a'}]", + "export a from 'a'", + // plugin `functionBind` + "[a::b]", + // plugin `functionSent` + "[function*() {function.sent}]", + // plugin `throwExpressions` + "[throw {}]", + // plugin `partialApplication` + "[foo(?)]", + // plugin `decorators` + "[@decorator class {}]", + // plugin `importAssertions` + "[import('a', {type:'json'})]", + // plugin `decimal` + "[1m]", + // plugin `moduleBlocks` + "[module {}]", + // plugin `asyncDoExpressions` + "[async do {}]", + // plugin `regexpUnicodeSets` + "[/a/v]", + // plugin `destructuringPrivate` + "[class {#foo;bar() {const {#foo: foo} = this;}}]", + // plugin `decoratorAutoAccessors` + "[class {accessor foo = 1}]", + // plugin `importReflection` + "[import('a', {reflect: 'module'})]", + "import module a from 'a'", + // plugin `explicitResourceManagement` + "[function() { {using a = b} }]", + "{using a = b}", + // plugin `recordAndTuple` + "[#[]]", + "[#{}]", + // plugin `v8intrinsic` + "[foo%bar()]", + // plugin `pipelineOperator` + "['foo' |> bar]", + "['foo' |> bar(%)]", + // plugin `jsx` + "[]", + ], + }, + ["json"] +);