Skip to content

Commit

Permalink
Improve code frame location
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Aug 31, 2022
1 parent 884d05f commit e190d1e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
21 changes: 11 additions & 10 deletions src/language-js/parse/postprocess/typescript.js
@@ -1,3 +1,4 @@
import isNonEmptyArray from "../../../utils/is-non-empty-array.js";
import visitNode from "./visit-node.js";
import throwTsSyntaxError from "./throw-ts-syntax-error.js";

Expand All @@ -13,19 +14,19 @@ function getSourceFileOfNode(node) {
// https://github.com/typescript-eslint/typescript-eslint/pull/2375
// There is a `checkGrammarDecorators` in `typescript` package, consider use it directly in future
function throwErrorForInvalidDecorator(tsNode) {
const illegalDecorator = tsNode.illegalDecorators?.[0];
if (!illegalDecorator) {
const { illegalDecorators } = tsNode;
if (!isNonEmptyArray(illegalDecorators)) {
return;
}

const sourceFile = getSourceFileOfNode(illegalDecorator);
const [start, end] = [illegalDecorator.pos, illegalDecorator.end].map(
(position) => {
const { line, character: column } =
sourceFile.getLineAndCharacterOfPosition(position);
return { line: line + 1, column };
}
);
const [{ expression }] = illegalDecorators;

const sourceFile = getSourceFileOfNode(expression);
const [start, end] = [expression.pos, expression.end].map((position) => {
const { line, character: column } =
sourceFile.getLineAndCharacterOfPosition(position);
return { line: line + 1, column };
});

throwTsSyntaxError({ loc: { start, end } }, "Decorators are not valid here.");
}
Expand Down
Expand Up @@ -10,13 +10,11 @@ exports[`decorator.ts [babel-ts] format 1`] = `
`;

exports[`decorator.ts [typescript] format 1`] = `
"Decorators are not valid here. (1:39)
> 1 | declare function dec<T>(target: T): T;
| ^
> 2 |
| ^
"Decorators are not valid here. (3:2)
1 | declare function dec<T>(target: T): T;
2 |
> 3 | @dec
| ^^^^^
| ^^^
4 | enum E {}
5 |"
`;
Expand All @@ -33,13 +31,11 @@ exports[`enums.ts [babel-ts] format 1`] = `
`;
exports[`enums.ts [typescript] format 1`] = `
"Decorators are not valid here. (1:1)
> 1 | // https://github.com/typescript-eslint/typescript-eslint/pull/2375
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 |
| ^
"Decorators are not valid here. (3:2)
1 | // https://github.com/typescript-eslint/typescript-eslint/pull/2375
2 |
> 3 | @decorator()
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^
4 | enum Direction {
5 | Up = 1,
6 | Down,"
Expand All @@ -55,13 +51,11 @@ exports[`function.ts [babel-ts] format 1`] = `
`;
exports[`function.ts [typescript] format 1`] = `
"Decorators are not valid here. (1:1)
> 1 | // https://github.com/typescript-eslint/typescript-eslint/pull/2375
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 |
| ^
"Decorators are not valid here. (3:2)
1 | // https://github.com/typescript-eslint/typescript-eslint/pull/2375
2 |
> 3 | @decorator()
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^
4 | function foo( ){}
5 |"
`;
Expand All @@ -77,15 +71,11 @@ exports[`interface.ts [babel-ts] format 1`] = `
`;
exports[`interface.ts [typescript] format 1`] = `
"Decorators are not valid here. (1:1)
> 1 | // invalid place but shouldn't be removed
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 | // #4632
| ^^^^^^^^
> 3 |
| ^^^^^^^^
"Decorators are not valid here. (4:2)
2 | // #4632
3 |
> 4 | @hello()
| ^^^^^^^^^
| ^^^^^^^
5 | interface MyInterface {id: string;
6 | }
7 |"
Expand All @@ -101,9 +91,9 @@ exports[`issue-9102.ts [babel-ts] format 1`] = `
`;
exports[`issue-9102.ts [typescript] format 1`] = `
"Decorators are not valid here. (1:1)
"Decorators are not valid here. (1:2)
> 1 | @Decorator()
| ^^^^^^^^^^^^
| ^^^^^^^^^^^
2 | type T = 1;
3 | class C {}
4 |"
Expand Down

0 comments on commit e190d1e

Please sign in to comment.