Skip to content

Commit

Permalink
feat: improve error location (#6556)
Browse files Browse the repository at this point in the history
Co-authored-by: Armano <armano2@users.noreply.github.com>
  • Loading branch information
fisker and armano2 committed Mar 6, 2023
1 parent 53776c2 commit 355adf0
Show file tree
Hide file tree
Showing 25 changed files with 209 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration ClassDeclaration _error_ export-missing-name TSESTree - Error 1`] = `
"TSError
> 1 | export class { }
| ^ A class declaration without the 'default' modifier must have a name.
| ^^^^^^^^^^^^^^^^ A class declaration without the 'default' modifier must have a name.
2 |"
`;
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 ExportNamedDeclaration _error_ anonymous-class TSESTree - Error 1`] = `
"TSError
> 1 | export class {}
| ^ A class declaration without the 'default' modifier must have a name.
| ^^^^^^^^^^^^^^^ A class declaration without the 'default' modifier must have a name.
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 @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration TSEnumDeclaration _error_ decorator TSESTree - Error 1`] = `
"TSError
> 1 | @decl enum Test {}
| ^ Decorators are not valid here.
| ^^^^^ Decorators are not valid here.
2 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration TSInterfaceDeclaration _error_ decorator TSESTree - Error 1`] = `
"TSError
> 1 | @decl interface Test {}
| ^ Decorators are not valid here.
| ^^^^^ Decorators are not valid here.
2 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration TSTypeAliasDeclaration _error_ decorator TSESTree - Error 1`] = `
"TSError
> 1 | @decl type Test = {};
| ^ Decorators are not valid here.
| ^^^^^ Decorators are not valid here.
2 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration VariableDeclaration _error_ decorator TSESTree - Error 1`] = `
"TSError
> 1 | @decl type Test = {};
| ^ Decorators are not valid here.
| ^^^^^ Decorators are not valid here.
2 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration VariableDeclaration _error_ missing-id-without-value TSESTree - Error 1`] = `
"TSError
> 1 | const;
| ^ A variable declaration list must have at least one variable declarator.
| ^^^^^^ A variable declaration list must have at least one variable declarator.
2 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`AST Fixtures declaration VariableDeclaration _error_ no-variables TSESTree - Error 1`] = `
"TSError
> 1 | const;
| ^ A variable declaration list must have at least one variable declarator.
| ^^^^^^ A variable declaration list must have at least one variable declarator.
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,8 +2,8 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ decorator-on-enum-declaration TSESTree - Error 1`] = `
"TSError
> 1 | // TODO: This fixture might be too large, and if so should be split up.
| ^ Decorators are not valid here.
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
3 | @dec enum E {}"
> 3 | @dec enum E {}
| ^^^^ Decorators are not valid here."
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ decorator-on-interface-declaration TSESTree - Error 1`] = `
"TSError
> 1 | // TODO: This fixture might be too large, and if so should be split up.
| ^ Decorators are not valid here.
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
3 | @deco()
> 3 | @deco()
| ^^^^^^^ Decorators are not valid here.
4 | interface M {}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ decorator-on-variable TSESTree - Error 1`] = `
"TSError
> 1 | // TODO: This fixture might be too large, and if so should be split up.
| ^ Decorators are not valid here.
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
3 | @deco()
4 | const a = 1"
> 3 | @deco()
| ^^^^^^^ Decorators are not valid here.
4 | const a = 1
5 |"
`;
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 |"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ solo-const TSESTree - Error 1`] = `
"TSError
> 1 | // TODO: This fixture might be too large, and if so should be split up.
| ^ A variable declaration list must have at least one variable declarator.
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
3 | const"
> 3 | const
| ^^^^^ A variable declaration list must have at least one variable declarator."
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument TSESTree - Error 1`] = `
"TSError
> 1 | {
| ^ A throw statement must throw an expression.
2 | throw
1 | {
> 2 | throw
| ^^^^^ A throw statement must throw an expression.
3 | }
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

0 comments on commit 355adf0

Please sign in to comment.