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

feat: improve error location #6556

Merged
merged 12 commits into from
Mar 6, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration ExportAllDeclaration _error_ non-string-source TSESTree - Error 1`] = `
"TSError
> 1 | export * from module;
| ^ Module specifier must be a string literal.
| ^^^^^^ Module specifier must be a string literal.
2 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration ImportDeclaration _error_ non-string-source TSESTree - Error 1`] = `
"TSError
> 1 | import * as x from module;
| ^ Module specifier must be a string literal.
| ^^^^^^ Module specifier must be a string literal.
2 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`AST Fixtures legacy-fixtures basics _error_ class-with-export-parameter
2 |
3 | class Foo {
> 4 | constructor(export a: string) {
| ^ A parameter cannot have an export modifier.
| ^^^^^^ A parameter cannot have an export modifier.
5 |
6 | }
7 | }"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-index-signature-export TSESTree - Error 1`] = `
"TSError
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
> 3 | interface Foo {
| ^ An index signature cannot have an export modifier.
4 | export [baz: string]: string;
3 | interface Foo {
> 4 | export [baz: string]: string;
| ^^^^^^ An index signature cannot have an export modifier.
5 | }
6 |"
6 |
7 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-method-export TSESTree - Error 1`] = `
"TSError
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
> 3 | interface Foo {
| ^ A method signature cannot have an export modifier.
4 | export g(bar: string): void;
3 | interface Foo {
> 4 | export g(bar: string): void;
| ^^^^^^ A method signature cannot have an export modifier.
5 | }
6 |"
6 |
7 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-property-export TSESTree - Error 1`] = `
"TSError
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
> 3 | interface Foo {
| ^ A property signature cannot have an export modifier.
4 | export a: string;
3 | interface Foo {
> 4 | export a: string;
| ^^^^^^ A property signature cannot have an export modifier.
5 | }
6 |"
6 |
7 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-property-with-default-value TSESTree - Error 1`] = `
"TSError
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
> 3 | interface Foo {
| ^ A property signature cannot have an initializer.
4 | bar: string = 'a';
3 | interface Foo {
> 4 | bar: string = 'a';
| ^^^ A property signature cannot have an initializer.
5 | }
6 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ exports[`AST Fixtures legacy-fixtures errorRecovery _error_ object-assertion-not
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
> 3 | ({a!} = {})
| ^ A shorthand property assignment cannot have an exclamation token.
| ^ A shorthand property assignment cannot have an exclamation token.
4 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ exports[`AST Fixtures legacy-fixtures errorRecovery _error_ object-optional-not-
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
> 3 | ({a?} = {})
| ^ A shorthand property assignment cannot have a question token.
| ^ A shorthand property assignment cannot have a question token.
4 |"
`;
12 changes: 10 additions & 2 deletions packages/ast-spec/tests/util/serialize-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ export function serializeError(
return error;
}

const { message, lineNumber: line, column, name } = error;
const {
name,
message,
location: { start, end },
} = error;

return (
name +
'\n' +
codeFrameColumns(
contents,
{ start: { line, column: column + 1 } },
{
start: { line: start.line, column: start.column + 1 },
end: { line: end.line, column: end.column + 1 },
},
{ highlightCode: false, message },
)
);
Expand Down