Skip to content

Commit f0c95ec

Browse files
committed
refactor
1 parent 3333a85 commit f0c95ec

File tree

6 files changed

+28
-47
lines changed

6 files changed

+28
-47
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
@@ -38,6 +38,13 @@ import { svelteVersion } from "../svelte-version";
3838
import { hasTypeInfo } from "../../utils";
3939
import { getModifiers } from "../compat";
4040

41+
type LetDirective = Omit<Compiler.LetDirective, "expression"> & {
42+
expression: SvAST.LetDirective["expression"];
43+
};
44+
type StandardDirective =
45+
| Exclude<Compiler.Directive, Compiler.StyleDirective | Compiler.LetDirective>
46+
| LetDirective;
47+
4148
/** Convert for Attributes */
4249
export function* convertAttributes(
4350
attributes: (
@@ -92,7 +99,11 @@ export function* convertAttributes(
9299
yield convertActionDirective(attr, parent, ctx);
93100
continue;
94101
}
95-
if (attr.type === "LetDirective" || attr.type === "Let") {
102+
if (attr.type === "LetDirective") {
103+
yield convertLetDirective(attr as LetDirective, parent, ctx);
104+
continue;
105+
}
106+
if (attr.type === "Let") {
96107
yield convertLetDirective(attr, parent, ctx);
97108
continue;
98109
}
@@ -272,7 +283,7 @@ function processAttributeValue(
272283
});
273284
if (
274285
nodes.length === 1 &&
275-
(nodes[0].type === "MustacheTag" || nodes[0].type === "ExpressionTag") &&
286+
(nodes[0].type === "ExpressionTag" || nodes[0].type === "MustacheTag") &&
276287
attribute.type === "SvelteAttribute"
277288
) {
278289
const typing = buildAttributeType(
@@ -691,7 +702,7 @@ function convertActionDirective(
691702

692703
/** Convert for Let Directive */
693704
function convertLetDirective(
694-
node: SvAST.LetDirective | Compiler.LetDirective,
705+
node: SvAST.LetDirective | LetDirective,
695706
parent: SvelteLetDirective["parent"],
696707
ctx: Context,
697708
): SvelteLetDirective {
@@ -703,7 +714,7 @@ function convertLetDirective(
703714
parent,
704715
...ctx.getConvertLocation(node),
705716
};
706-
processDirective(node as any, directive, ctx, {
717+
processDirective(node, directive, ctx, {
707718
processPattern(pattern) {
708719
return ctx.letDirCollections
709720
.getCollection()
@@ -799,9 +810,7 @@ function buildLetDirectiveType(
799810
}
800811

801812
type DirectiveProcessors<
802-
D extends
803-
| SvAST.Directive
804-
| Exclude<Compiler.Directive, Compiler.StyleDirective>,
813+
D extends SvAST.Directive | StandardDirective,
805814
S extends SvelteDirective,
806815
E extends D["expression"] & S["expression"],
807816
> =
@@ -828,9 +837,7 @@ type DirectiveProcessors<
828837

829838
/** Common process for directive */
830839
function processDirective<
831-
D extends
832-
| SvAST.Directive
833-
| Exclude<Compiler.Directive, Compiler.StyleDirective>,
840+
D extends SvAST.Directive | StandardDirective,
834841
S extends SvelteDirective,
835842
E extends D["expression"] & S["expression"],
836843
>(
@@ -848,7 +855,7 @@ function processDirectiveKey<
848855
D extends
849856
| SvAST.Directive
850857
| SvAST.StyleDirective
851-
| Compiler.Directive
858+
| StandardDirective
852859
| Compiler.StyleDirective,
853860
S extends SvelteDirective | SvelteStyleDirective,
854861
>(node: D, directive: S, ctx: Context) {
@@ -889,7 +896,9 @@ function processDirectiveKey<
889896
const key = (directive.key = {
890897
type: "SvelteDirectiveKey",
891898
name: null as any,
892-
modifiers: getModifiers(node),
899+
modifiers: getModifiers(
900+
node as SvAST.Directive | SvAST.StyleDirective | Compiler.Directive,
901+
),
893902
parent: directive,
894903
...ctx.getConvertLocation({ start: node.start, end: keyEndIndex }),
895904
});
@@ -905,9 +914,7 @@ function processDirectiveKey<
905914

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

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 {
@@ -656,13 +655,12 @@ export function convertSnippetBlock(
656655
},
657656
);
658657

659-
const body = getBodyFromSnippetBlock(node);
660658
snippetBlock.children.push(
661659
...convertChildren(
662660
{
663661
nodes:
664662
// Adjust for Svelte v5
665-
trimChildren(getChildren(body)),
663+
trimChildren(node.body.nodes),
666664
},
667665
snippetBlock,
668666
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 & 16 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,12 +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:
70-
| ESTree.SimpleCallExpression
71-
| (ESTree.ChainExpression & { expression: ESTree.SimpleCallExpression });
72-
}
7365
export interface IfBlock extends BaseNode {
7466
type: "IfBlock";
7567
expression: ESTree.Expression;
@@ -120,13 +112,6 @@ export interface KeyBlock extends BaseNode {
120112
expression: ESTree.Expression;
121113
children: TemplateNode[];
122114
}
123-
export interface SnippetBlock extends BaseNode {
124-
type: "SnippetBlock";
125-
expression: ESTree.Identifier;
126-
parameters: ESTree.Pattern[];
127-
children: TemplateNode[];
128-
}
129-
130115
export interface BaseElement extends BaseNode {
131116
type: "Element";
132117
name: string;

0 commit comments

Comments
 (0)