-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtemplate.ts
40 lines (38 loc) · 1005 Bytes
/
template.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type {} from "svelte"; // FIXME: Workaround to get type information for "svelte/compiler"
import { parse } from "svelte/compiler";
import type * as SvAST from "./svelte-ast-types";
import type { Context } from "../context";
import { convertSvelteRoot } from "./converts/index";
import { sortNodes } from "./sort";
import type { SvelteProgram } from "../ast";
import { ParseError } from "..";
/**
* Parse for template
*/
export function parseTemplate(
code: string,
ctx: Context,
parserOptions: any = {}
): {
ast: SvelteProgram;
svelteAst: SvAST.Ast;
} {
try {
const svelteAst = parse(code, {
filename: parserOptions.filePath,
}) as SvAST.Ast;
const ast = convertSvelteRoot(svelteAst, ctx);
sortNodes(ast.body);
return {
ast,
svelteAst,
};
} catch (e: any) {
if (typeof e.pos === "number") {
const err = new ParseError(e.message, e.pos, ctx);
(err as any).svelteCompilerError = e;
throw err;
}
throw e;
}
}