Skip to content

Commit b5c6a4d

Browse files
authored
fix: wrong error location (#110)
* fix: wrong error location * Create great-horses-shop.md
1 parent c9d671c commit b5c6a4d

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

.changeset/great-horses-shop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro-eslint-parser": patch
3+
---
4+
5+
fix: wrong error location

src/context/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class Context {
3030
};
3131
}
3232

33+
public getIndexFromLoc(loc: { line: number; column: number }): number {
34+
return this.locs.getIndexFromLoc(loc);
35+
}
36+
3337
/**
3438
* Get the location information of the given indexes.
3539
*/

src/errors.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ export class ParseError extends SyntaxError {
1515
/**
1616
* Initialize this ParseError instance.
1717
*/
18-
public constructor(message: string, offset: number, ctx: Context) {
18+
public constructor(
19+
message: string,
20+
offset: number | { line: number; column: number },
21+
ctx: Context
22+
) {
1923
super(message);
20-
this.index = offset;
21-
const loc = ctx.getLocFromIndex(offset);
22-
this.lineNumber = loc.line;
23-
this.column = loc.column;
24+
if (typeof offset === "number") {
25+
this.index = offset;
26+
const loc = ctx.getLocFromIndex(offset);
27+
this.lineNumber = loc.line;
28+
this.column = loc.column;
29+
} else {
30+
this.index = ctx.getIndexFromLoc(offset);
31+
this.lineNumber = offset.line;
32+
this.column = offset.column;
33+
}
2434
this.originalAST = ctx.originalAST;
2535
}
2636
}

src/parser/astro-parser/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function parse(code: string, ctx: Context): ParseResult {
2727
for (const { code, text, location, severity } of result.diagnostics || []) {
2828
if (severity === 1 /* Error */) {
2929
ctx.originalAST = result.ast;
30-
throw new ParseError(`${text} [${code}]`, location.length, ctx);
30+
throw new ParseError(`${text} [${code}]`, location, ctx);
3131
}
3232
}
3333
if (!result.ast.children) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"index": 8,
2+
"index": 5,
33
"lineNumber": 2,
44
"message": "set:text directive will overwrite child nodes. [2006]",
5-
"column": 7
5+
"column": 4
66
}

0 commit comments

Comments
 (0)