Skip to content

Commit 187053e

Browse files
committed
refactor type
1 parent 316eb9c commit 187053e

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

Diff for: src/parser/compat.ts

+9-9
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
}

Diff for: src/parser/converts/root.ts

+2-1
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
SvelteName,
45
SvelteProgram,
@@ -22,7 +23,7 @@ import {
2223
* Convert root
2324
*/
2425
export function convertSvelteRoot(
25-
svelteAst: SvAST.Ast | SvAST.AstLegacy,
26+
svelteAst: Compiler.Root | SvAST.AstLegacy,
2627
ctx: Context,
2728
): SvelteProgram {
2829
const ast: SvelteProgram = {

Diff for: src/parser/svelte-ast-types.ts

-6
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;

Diff for: src/parser/template.ts

+3-2
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)