Skip to content

Commit 69c48f2

Browse files
committed
refactor
1 parent 49edeb7 commit 69c48f2

File tree

6 files changed

+28
-46
lines changed

6 files changed

+28
-46
lines changed

src/parser/compat.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,6 @@ export function getFallbackFromEachBlock(
179179
}
180180
return (block as SvAST.EachBlock).else ?? null;
181181
}
182-
// SnippetBlock
183-
export function getBodyFromSnippetBlock(
184-
block: SvAST.SnippetBlock | Compiler.SnippetBlock,
185-
): Compiler.Fragment | SvAST.SnippetBlock {
186-
if ((block as Compiler.SnippetBlock).body) {
187-
return (block as Compiler.SnippetBlock).body;
188-
}
189-
return block as SvAST.SnippetBlock;
190-
}
191182
// AwaitBlock
192183
export function getPendingFromAwaitBlock(
193184
block: SvAST.AwaitBlock | Compiler.AwaitBlock,

src/parser/converts/attr.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ import type { AttributeToken } from "../html";
3737
import { svelteVersion } from "../svelte-version";
3838
import { getModifiers } from "../compat";
3939

40+
type LetDirective = Omit<Compiler.LetDirective, "expression"> & {
41+
expression: SvAST.LetDirective["expression"];
42+
};
43+
type StandardDirective =
44+
| Exclude<Compiler.Directive, Compiler.StyleDirective | Compiler.LetDirective>
45+
| LetDirective;
46+
4047
/** Convert for Attributes */
4148
export function* convertAttributes(
4249
attributes: (
@@ -91,7 +98,11 @@ export function* convertAttributes(
9198
yield convertActionDirective(attr, parent, ctx);
9299
continue;
93100
}
94-
if (attr.type === "LetDirective" || attr.type === "Let") {
101+
if (attr.type === "LetDirective") {
102+
yield convertLetDirective(attr as LetDirective, parent, ctx);
103+
continue;
104+
}
105+
if (attr.type === "Let") {
95106
yield convertLetDirective(attr, parent, ctx);
96107
continue;
97108
}
@@ -271,7 +282,7 @@ function processAttributeValue(
271282
});
272283
if (
273284
nodes.length === 1 &&
274-
(nodes[0].type === "MustacheTag" || nodes[0].type === "ExpressionTag") &&
285+
(nodes[0].type === "ExpressionTag" || nodes[0].type === "MustacheTag") &&
275286
attribute.type === "SvelteAttribute"
276287
) {
277288
const typing = buildAttributeType(
@@ -690,7 +701,7 @@ function convertActionDirective(
690701

691702
/** Convert for Let Directive */
692703
function convertLetDirective(
693-
node: SvAST.LetDirective | Compiler.LetDirective,
704+
node: SvAST.LetDirective | LetDirective,
694705
parent: SvelteLetDirective["parent"],
695706
ctx: Context,
696707
): SvelteLetDirective {
@@ -702,7 +713,7 @@ function convertLetDirective(
702713
parent,
703714
...ctx.getConvertLocation(node),
704715
};
705-
processDirective(node as any, directive, ctx, {
716+
processDirective(node, directive, ctx, {
706717
processPattern(pattern) {
707718
return ctx.letDirCollections
708719
.getCollection()
@@ -798,9 +809,7 @@ function buildLetDirectiveType(
798809
}
799810

800811
type DirectiveProcessors<
801-
D extends
802-
| SvAST.Directive
803-
| Exclude<Compiler.Directive, Compiler.StyleDirective>,
812+
D extends SvAST.Directive | StandardDirective,
804813
S extends SvelteDirective,
805814
E extends D["expression"] & S["expression"],
806815
> =
@@ -827,9 +836,7 @@ type DirectiveProcessors<
827836

828837
/** Common process for directive */
829838
function processDirective<
830-
D extends
831-
| SvAST.Directive
832-
| Exclude<Compiler.Directive, Compiler.StyleDirective>,
839+
D extends SvAST.Directive | StandardDirective,
833840
S extends SvelteDirective,
834841
E extends D["expression"] & S["expression"],
835842
>(
@@ -847,7 +854,7 @@ function processDirectiveKey<
847854
D extends
848855
| SvAST.Directive
849856
| SvAST.StyleDirective
850-
| Compiler.Directive
857+
| StandardDirective
851858
| Compiler.StyleDirective,
852859
S extends SvelteDirective | SvelteStyleDirective,
853860
>(node: D, directive: S, ctx: Context) {
@@ -888,7 +895,9 @@ function processDirectiveKey<
888895
const key = (directive.key = {
889896
type: "SvelteDirectiveKey",
890897
name: null as any,
891-
modifiers: getModifiers(node),
898+
modifiers: getModifiers(
899+
node as SvAST.Directive | SvAST.StyleDirective | Compiler.Directive,
900+
),
892901
parent: directive,
893902
...ctx.getConvertLocation({ start: node.start, end: keyEndIndex }),
894903
});
@@ -904,9 +913,7 @@ function processDirectiveKey<
904913

905914
/** Common process for directive expression */
906915
function processDirectiveExpression<
907-
D extends
908-
| SvAST.Directive
909-
| Exclude<Compiler.Directive, Compiler.StyleDirective>,
916+
D extends SvAST.Directive | StandardDirective,
910917
S extends SvelteDirective,
911918
E extends D["expression"],
912919
>(

src/parser/converts/block.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type * as ESTree from "estree";
2424
import {
2525
getAlternateFromIfBlock,
2626
getBodyFromEachBlock,
27-
getBodyFromSnippetBlock,
2827
getCatchFromAwaitBlock,
2928
getChildren,
3029
getConsequentFromIfBlock,
@@ -622,7 +621,7 @@ export function convertKeyBlock(
622621

623622
/** Convert for SnippetBlock */
624623
export function convertSnippetBlock(
625-
node: SvAST.SnippetBlock | Compiler.SnippetBlock,
624+
node: Compiler.SnippetBlock,
626625
parent: SvelteSnippetBlock["parent"],
627626
ctx: Context,
628627
): SvelteSnippetBlock {
@@ -652,13 +651,12 @@ export function convertSnippetBlock(
652651
},
653652
);
654653

655-
const body = getBodyFromSnippetBlock(node);
656654
snippetBlock.children.push(
657655
...convertChildren(
658656
{
659657
nodes:
660658
// Adjust for Svelte v5
661-
trimChildren(getChildren(body)),
659+
trimChildren(node.body.nodes),
662660
},
663661
snippetBlock,
664662
ctx,

src/parser/converts/element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ function extractLetDirectives(fragment: {
239239
SvAST.LetDirective | Compiler.LetDirective
240240
>[] = [];
241241
for (const attr of fragment.attributes) {
242-
if (attr.type === "Let" || attr.type === "LetDirective") {
242+
if (attr.type === "LetDirective" || attr.type === "Let") {
243243
letDirectives.push(attr);
244244
} else {
245245
attributes.push(attr);

src/parser/converts/render.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type * as ESTree from "estree";
22
import type { SvelteRenderTag } from "../../ast";
33
import type { Context } from "../../context";
4-
import type * as SvAST from "../svelte-ast-types";
54
import { getWithLoc } from "./common";
5+
import type * as Compiler from "svelte/compiler";
66

77
/** Convert for RenderTag */
88
export function convertRenderTag(
9-
node: SvAST.RenderTag,
9+
node: Compiler.RenderTag,
1010
parent: SvelteRenderTag["parent"],
1111
ctx: Context,
1212
): SvelteRenderTag {

src/parser/svelte-ast-types.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export declare type TemplateNode =
2121
| RawMustacheTag
2222
| DebugTag
2323
| ConstTag
24-
| RenderTag
2524
| Directive
2625
| StyleDirective
2726
| Element
@@ -38,8 +37,7 @@ export declare type TemplateNode =
3837
| IfBlock
3938
| EachBlock
4039
| AwaitBlock
41-
| KeyBlock
42-
| SnippetBlock;
40+
| KeyBlock;
4341
export interface Fragment extends BaseNode {
4442
type: "Fragment";
4543
children: TemplateNode[];
@@ -64,11 +62,6 @@ export interface ConstTag extends BaseNode {
6462
type: "ConstTag";
6563
expression: ESTree.AssignmentExpression;
6664
}
67-
export interface RenderTag extends BaseNode {
68-
type: "RenderTag";
69-
expression: ESTree.Identifier;
70-
argument: null | ESTree.Expression;
71-
}
7265
export interface IfBlock extends BaseNode {
7366
type: "IfBlock";
7467
expression: ESTree.Expression;
@@ -119,13 +112,6 @@ export interface KeyBlock extends BaseNode {
119112
expression: ESTree.Expression;
120113
children: TemplateNode[];
121114
}
122-
export interface SnippetBlock extends BaseNode {
123-
type: "SnippetBlock";
124-
expression: ESTree.Identifier;
125-
context: null | ESTree.Pattern;
126-
children: TemplateNode[];
127-
}
128-
129115
export interface BaseElement extends BaseNode {
130116
type: "Element";
131117
name: string;

0 commit comments

Comments
 (0)