Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: snapshot parse errors #9467

Merged
merged 9 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/language-js/parser-espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ function parse(originalText, parsers, options) {
} catch (moduleError) {
try {
ast = parse(text, { ...espreeOptions, sourceType: "script" });
} catch (scriptError) {
} catch (_) {
// throw the error for `module` parsing
const { message, lineNumber, column } = moduleError;

/* istanbul ignore next */
if (typeof moduleError.lineNumber !== "number") {
if (typeof lineNumber !== "number") {
throw moduleError;
}

throw createError(moduleError.message, {
start: { line: moduleError.lineNumber, column: moduleError.column },
});
throw createError(message, { start: { line: lineNumber, column } });
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/language-js/parser-meriyah.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ function parse(text, parsers, options) {
ast = parseWithOptions(text, /* module */ false);
} catch (_) {
// throw the error for `module` parsing
if (typeof moduleError.loc !== "number") {
const { message, line, column } = moduleError;

/* istanbul ignore next */
if (typeof line !== "number") {
throw moduleError;
}

throw createError(moduleError.message, {
start: moduleError.loc,
});
throw createError(message, { start: { line, column } });
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/flow/this-annotation/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ function baz(this: number, ...a) {}
================================================================================
`;

exports[`function_type.js - {"trailingComma":"all"} [babel-flow] format 1`] = `
"Unexpected token, expected \\")\\" (3:16)
1 | // @flow
2 |
> 3 | type T = (this : number, a : string, b : number) => void
| ^
4 |
5 | type U = (this : number, ...c : any) => void
6 | "
`;

exports[`function_type.js - {"trailingComma":"all"} format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
Expand Down Expand Up @@ -81,6 +92,17 @@ type V = (this: number) => void;
================================================================================
`;

exports[`line_break.js - {"trailingComma":"all"} [babel-flow] format 1`] = `
"Unexpected token, expected \\")\\" (3:15)
1 | // @flow
2 |
> 3 | type T = (this: boolean,
| ^
4 | a: number,
5 |
6 | b: number,"
`;

exports[`line_break.js - {"trailingComma":"all"} format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
Expand Down Expand Up @@ -173,6 +195,17 @@ class A {
================================================================================
`;

exports[`union_type.js - {"trailingComma":"all"} [babel-flow] format 1`] = `
"Unexpected token, expected \\")\\" (4:7)
2 |
3 | type A = (
> 4 | this: | SupperLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345,
| ^
5 | b: number,
6 | ) => boolean;
7 | "
`;

exports[`union_type.js - {"trailingComma":"all"} format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
Expand Down
30 changes: 30 additions & 0 deletions tests/js/arrow-call/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,26 @@ promise.then((result) =>
================================================================================
`;

exports[`class-property.js [espree] format 1`] = `
"Unexpected token = (3:22)
1 | const composition = (ViewComponent, ContainerComponent) =>
2 | class extends React.Component {
> 3 | static propTypes = {};
| ^
4 | };
5 | "
`;

exports[`class-property.js - {"arrowParens":"always"} [espree] format 1`] = `
"Unexpected token = (3:22)
1 | const composition = (ViewComponent, ContainerComponent) =>
2 | class extends React.Component {
> 3 | static propTypes = {};
| ^
4 | };
5 | "
`;

exports[`class-property.js - {"arrowParens":"always"} format 1`] = `
====================================options=====================================
arrowParens: "always"
Expand All @@ -323,6 +343,16 @@ const composition = (ViewComponent, ContainerComponent) =>
================================================================================
`;

exports[`class-property.js - {"trailingComma":"all"} [espree] format 1`] = `
"Unexpected token = (3:22)
1 | const composition = (ViewComponent, ContainerComponent) =>
2 | class extends React.Component {
> 3 | static propTypes = {};
| ^
4 | };
5 | "
`;

exports[`class-property.js - {"trailingComma":"all"} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
Expand Down
18 changes: 18 additions & 0 deletions tests/js/arrows-bind/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`arrows-bind.js [espree] format 1`] = `
"Unexpected token : (1:9)
> 1 | a => ({}::b()\`\`[''].c++ && 0 ? 0 : 0);
| ^
2 | (a => b)::c;
3 | a::(b => c);
4 | "
`;

exports[`arrows-bind.js [meriyah] format 1`] = `
"[1:9]: Expected ')' (1:9)
> 1 | a => ({}::b()\`\`[''].c++ && 0 ? 0 : 0);
| ^
2 | (a => b)::c;
3 | a::(b => c);
4 | "
`;

exports[`arrows-bind.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`newline-before-arrow.js [espree] format 1`] = `
"Unexpected token => (2:1)
1 | async x
> 2 | => x
| ^
3 | "
`;

exports[`newline-before-arrow.js [meriyah] format 1`] = `
"[2:2]: No line break is allowed after '=>' (2:2)
1 | async x
> 2 | => x
| ^
3 | "
`;

exports[`newline-before-arrow.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
Expand Down