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
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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
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."
`;
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 {}"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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 |"
`;
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."
`;
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
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