-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtemplate.ts
48 lines (46 loc) · 1.38 KB
/
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
41
42
43
44
45
46
47
48
import type {} from "svelte"; // FIXME: Workaround to get type information for "svelte/compiler"
import { parse } from "svelte/compiler";
import type * as Compiler from "./svelte-ast-types-for-v5.js";
import type * as SvAST from "./svelte-ast-types.js";
import type { Context } from "../context/index.js";
import { convertSvelteRoot } from "./converts/index.js";
import type { SvelteProgram } from "../ast/index.js";
import { ParseError } from "../errors.js";
import type { NormalizedParserOptions } from "./parser-options.js";
import { svelteVersion } from "./svelte-version.js";
/**
* Parse for template
*/
export function parseTemplate(
code: string,
ctx: Context,
parserOptions: NormalizedParserOptions,
): {
ast: SvelteProgram;
svelteAst: Compiler.Root | SvAST.AstLegacy;
} {
try {
const options: {
filename?: string | undefined;
modern: true;
} = {
filename: parserOptions.filePath,
...(svelteVersion.gte(5) ? { modern: true } : ({} as never)),
};
const svelteAst = parse(code, options) as never as
| Compiler.Root
| SvAST.AstLegacy;
const ast = convertSvelteRoot(svelteAst, ctx);
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;
}
}