Skip to content

Commit 0446b03

Browse files
authored
Improved parse error location (#19)
1 parent def05d2 commit 0446b03

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/parser/template.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Context } from "../context"
44
import { convertSvelteRoot } from "./converts/index"
55
import { sort } from "./sort"
66
import type { SvelteProgram } from "../ast"
7+
import { ParseError } from ".."
78

89
/**
910
* Parse for template
@@ -16,14 +17,23 @@ export function parseTemplate(
1617
ast: SvelteProgram
1718
svelteAst: SvAST.Ast
1819
} {
19-
const svelteAst = parse(code, {
20-
filename: parserOptions.filePath,
21-
}) as SvAST.Ast
22-
const ast = convertSvelteRoot(svelteAst, ctx)
23-
sort(ast.body)
20+
try {
21+
const svelteAst = parse(code, {
22+
filename: parserOptions.filePath,
23+
}) as SvAST.Ast
24+
const ast = convertSvelteRoot(svelteAst, ctx)
25+
sort(ast.body)
2426

25-
return {
26-
ast,
27-
svelteAst,
27+
return {
28+
ast,
29+
svelteAst,
30+
}
31+
} catch (e: any) {
32+
if (typeof e.pos === "number") {
33+
const err = new ParseError(e.message, e.pos, ctx)
34+
;(err as any).svelteCompilerError = e
35+
throw err
36+
}
37+
throw e
2838
}
2939
}

0 commit comments

Comments
 (0)