Skip to content

Commit

Permalink
Improve babel parse error message (#15928)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jan 16, 2024
1 parent 1e1942d commit 70c9c56
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
10 changes: 9 additions & 1 deletion src/language-js/parse/utils/create-babel-parse-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ function createBabelParseError(error) {
let {
message,
loc: { line, column },
reasonCode,
} = error;

let cause = error;
if (reasonCode === "MissingPlugin" || reasonCode === "MissingOneOfPlugins") {
message = "Unexpected token.";
cause = undefined;
}

const suffix = ` (${line}:${column})`;
if (message.endsWith(suffix)) {
message = message.slice(0, -suffix.length);
}

return createError(message, {
loc: { start: { line, column: column + 1 } },
cause: error,
cause,
});
}

Expand Down
10 changes: 0 additions & 10 deletions src/language-json/parser-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ function createJsonParse(options = {}) {
attachComment: false,
});
} catch (/** @type {any} */ error) {
if (
error?.reasonCode === "MissingPlugin" ||
error?.reasonCode === "MissingOneOfPlugins"
) {
throw createBabelParseError({
message: "Unexpected token",
loc: error.loc,
});
}

throw createBabelParseError(error);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snippet: #0 [babel] format 1`] = `
"Unexpected token. (1:8)
> 1 | export type Foo = number;
| ^"
`;
10 changes: 10 additions & 0 deletions tests/format/misc/errors/babel-missing-plugins/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
run_spec(
{
importMeta: import.meta,
snippets: [
// https://github.com/babel/babel/commit/a466f9c310ace91484d4087f077ee6d6c8cd8789
"export type Foo = number;",
],
},
["babel"],
);
34 changes: 17 additions & 17 deletions tests/format/misc/errors/json/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ exports[`snippet: #3 [json] format 1`] = `
`;

exports[`snippet: #3 [json] format 2`] = `
"Unexpected token (1:2)
"Unexpected token. (1:2)
> 1 | [do {}]
| ^"
`;
Expand Down Expand Up @@ -184,7 +184,7 @@ exports[`snippet: #7 [json] format 1`] = `
`;

exports[`snippet: #7 [json] format 2`] = `
"Unexpected token (1:24)
"Unexpected token. (1:24)
> 1 | [function*() {function.sent}]
| ^"
`;
Expand All @@ -208,7 +208,7 @@ exports[`snippet: #8 [json] format 1`] = `
`;

exports[`snippet: #8 [json] format 2`] = `
"Unexpected token (1:2)
"Unexpected token. (1:2)
> 1 | [throw {}]
| ^"
`;
Expand All @@ -232,7 +232,7 @@ exports[`snippet: #9 [json] format 1`] = `
`;

exports[`snippet: #9 [json] format 2`] = `
"Unexpected token (1:6)
"Unexpected token. (1:6)
> 1 | [foo(?)]
| ^"
`;
Expand All @@ -256,7 +256,7 @@ exports[`snippet: #10 [json] format 1`] = `
`;

exports[`snippet: #10 [json] format 2`] = `
"Unexpected token (1:2)
"Unexpected token. (1:2)
> 1 | [@decorator class {}]
| ^"
`;
Expand All @@ -280,7 +280,7 @@ exports[`snippet: #11 [json] format 1`] = `
`;

exports[`snippet: #11 [json] format 2`] = `
"Unexpected token (1:28)
"Unexpected token. (1:28)
> 1 | [import('a', {type:'json'})]
| ^"
`;
Expand All @@ -304,7 +304,7 @@ exports[`snippet: #12 [json] format 1`] = `
`;

exports[`snippet: #12 [json] format 2`] = `
"Unexpected token (1:3)
"Unexpected token. (1:3)
> 1 | [1m]
| ^"
`;
Expand All @@ -328,7 +328,7 @@ exports[`snippet: #13 [json] format 1`] = `
`;

exports[`snippet: #13 [json] format 2`] = `
"Unexpected token (1:2)
"Unexpected token. (1:2)
> 1 | [module {}]
| ^"
`;
Expand All @@ -352,7 +352,7 @@ exports[`snippet: #14 [json] format 1`] = `
`;

exports[`snippet: #14 [json] format 2`] = `
"Unexpected token (1:8)
"Unexpected token. (1:8)
> 1 | [async do {}]
| ^"
`;
Expand Down Expand Up @@ -400,7 +400,7 @@ exports[`snippet: #16 [json] format 1`] = `
`;

exports[`snippet: #16 [json] format 2`] = `
"Unexpected token (1:28)
"Unexpected token. (1:28)
> 1 | [class {#foo;bar() {const {#foo: foo} = this;}}]
| ^"
`;
Expand All @@ -424,7 +424,7 @@ exports[`snippet: #17 [json] format 1`] = `
`;

exports[`snippet: #17 [json] format 2`] = `
"Unexpected token (1:18)
"Unexpected token. (1:18)
> 1 | [class {accessor foo = 1}]
| ^"
`;
Expand All @@ -448,7 +448,7 @@ exports[`snippet: #18 [json] format 1`] = `
`;

exports[`snippet: #18 [json] format 2`] = `
"Unexpected token (1:34)
"Unexpected token. (1:34)
> 1 | [import('a', {reflect: 'module'})]
| ^"
`;
Expand Down Expand Up @@ -497,7 +497,7 @@ exports[`snippet: #20 [json] format 1`] = `
`;

exports[`snippet: #20 [json] format 2`] = `
"Unexpected token (1:16)
"Unexpected token. (1:16)
> 1 | [function() { {using a = b} }]
| ^"
`;
Expand Down Expand Up @@ -584,7 +584,7 @@ Cause: Unexpected token (1:13)"
`;

exports[`snippet: #23 [json] format 2`] = `
"Unexpected token (1:2)
"Unexpected token. (1:2)
> 1 | [#{}]
| ^"
`;
Expand Down Expand Up @@ -637,7 +637,7 @@ exports[`snippet: #25 [json] format 1`] = `
`;

exports[`snippet: #25 [json] format 2`] = `
"Unexpected token (1:8)
"Unexpected token. (1:8)
> 1 | ['foo' |> bar]
| ^"
`;
Expand All @@ -661,7 +661,7 @@ exports[`snippet: #26 [json] format 1`] = `
`;

exports[`snippet: #26 [json] format 2`] = `
"Unexpected token (1:8)
"Unexpected token. (1:8)
> 1 | ['foo' |> bar(%)]
| ^"
`;
Expand All @@ -685,7 +685,7 @@ exports[`snippet: #27 [json] format 1`] = `
`;

exports[`snippet: #27 [json] format 2`] = `
"Unexpected token (1:2)
"Unexpected token. (1:2)
> 1 | [<foo></foo>]
| ^"
`;
Expand Down

0 comments on commit 70c9c56

Please sign in to comment.