Skip to content

Commit b103486

Browse files
committed
refactor type
1 parent c909bd5 commit b103486

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

src/parser/compat.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ export type Child =
1212
type HasChildren = { children?: SvAST.TemplateNode[] };
1313
// Root
1414
export function getFragmentFromRoot(
15-
svelteAst: SvAST.Ast | SvAST.AstLegacy,
15+
svelteAst: Compiler.Root | SvAST.AstLegacy,
1616
): SvAST.Fragment | Compiler.Fragment | undefined {
1717
return (
18-
(svelteAst as SvAST.Ast).fragment ?? (svelteAst as SvAST.AstLegacy).html
18+
(svelteAst as Compiler.Root).fragment ?? (svelteAst as SvAST.AstLegacy).html
1919
);
2020
}
2121
export function getInstanceFromRoot(
22-
svelteAst: SvAST.Ast | SvAST.AstLegacy,
23-
): SvAST.Script | Compiler.Script | undefined {
24-
return (svelteAst as SvAST.AstLegacy).instance;
22+
svelteAst: Compiler.Root | SvAST.AstLegacy,
23+
): SvAST.Script | Compiler.Script | null | undefined {
24+
return svelteAst.instance;
2525
}
2626
export function getModuleFromRoot(
27-
svelteAst: SvAST.Ast | SvAST.AstLegacy,
28-
): SvAST.Script | Compiler.Script | undefined {
29-
return (svelteAst as SvAST.AstLegacy).module;
27+
svelteAst: Compiler.Root | SvAST.AstLegacy,
28+
): SvAST.Script | Compiler.Script | null | undefined {
29+
return svelteAst.module;
3030
}
3131
export function getOptionsFromRoot(
32-
svelteAst: SvAST.Ast | SvAST.AstLegacy,
32+
svelteAst: Compiler.Root | SvAST.AstLegacy,
3333
): Compiler.SvelteOptionsRaw | null {
3434
return (svelteAst as any).options?.__raw__ ?? null;
3535
}

src/parser/converts/root.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type * as SvAST from "../svelte-ast-types";
2+
import * as Compiler from "svelte/compiler";
23
import type {
34
SvelteAttribute,
45
SvelteGenericsDirective,
@@ -30,7 +31,7 @@ import {
3031
* Convert root
3132
*/
3233
export function convertSvelteRoot(
33-
svelteAst: SvAST.Ast | SvAST.AstLegacy,
34+
svelteAst: Compiler.Root | SvAST.AstLegacy,
3435
ctx: Context,
3536
): SvelteProgram {
3637
const ast: SvelteProgram = {

src/parser/svelte-ast-types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import type ESTree from "estree";
2-
import type * as Compiler from "svelte/compiler";
32
interface BaseNode {
43
start: number;
54
end: number;
65
}
7-
export interface Ast {
8-
fragment?: Compiler.Fragment;
9-
js: Compiler.Script[];
10-
css?: Compiler.Style;
11-
}
126
export interface AstLegacy {
137
html?: Fragment;
148
css?: Style;

src/parser/template.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {} from "svelte"; // FIXME: Workaround to get type information for "svelte/compiler"
22
import { parse } from "svelte/compiler";
3+
import * as Compiler from "svelte/compiler";
34
import type * as SvAST from "./svelte-ast-types";
45
import type { Context } from "../context";
56
import { convertSvelteRoot } from "./converts/index";
@@ -18,13 +19,13 @@ export function parseTemplate(
1819
parserOptions: NormalizedParserOptions,
1920
): {
2021
ast: SvelteProgram;
21-
svelteAst: SvAST.Ast | SvAST.AstLegacy;
22+
svelteAst: Compiler.Root | SvAST.AstLegacy;
2223
} {
2324
try {
2425
const svelteAst = parse(code, {
2526
filename: parserOptions.filePath,
2627
...(svelteVersion.gte(5) ? { modern: true } : {}),
27-
}) as never as SvAST.Ast | SvAST.AstLegacy;
28+
}) as never as Compiler.Root | SvAST.AstLegacy;
2829
const ast = convertSvelteRoot(svelteAst, ctx);
2930
sortNodes(ast.body);
3031

0 commit comments

Comments
 (0)