Skip to content

Commit e774eb0

Browse files
committed
fix for SvelteSnippetBlock
1 parent b282014 commit e774eb0

14 files changed

+363
-345
lines changed

docs/AST.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ This is the `{#snippet}` tag node.
555555
interface SvelteSnippetBlock extends Node {
556556
type: "SvelteSnippetBlock";
557557
id: Identifier;
558-
context: null | Pattern;
558+
params: Pattern[];
559559
children: Child[];
560560
}
561561
```

src/ast/html.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ export interface SvelteKeyBlock extends BaseNode {
480480
export interface SvelteSnippetBlock extends BaseNode {
481481
type: "SvelteSnippetBlock";
482482
id: ESTree.Identifier;
483-
context: null | ESTree.Pattern;
483+
params: ESTree.Pattern[];
484484
children: Child[];
485485
parent:
486486
| SvelteProgram

src/context/script-let.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export class ScriptLetContext {
428428
id: ESTree.Identifier,
429429
closeParentIndex: number,
430430
snippetBlock: SvelteSnippetBlock,
431-
callback: (id: ESTree.Identifier, ctx: ESTree.Pattern | null) => void,
431+
callback: (id: ESTree.Identifier, params: ESTree.Pattern[]) => void,
432432
): void {
433433
const idRange = getNodeRange(id);
434434
const part = this.ctx.code.slice(idRange[0], closeParentIndex + 1);
@@ -438,14 +438,14 @@ export class ScriptLetContext {
438438
(st, tokens, _comments, result) => {
439439
const fnDecl = st as ESTree.FunctionDeclaration;
440440
const idNode = fnDecl.id;
441-
const context = fnDecl.params.length > 0 ? fnDecl.params[0] : null;
441+
const params = [...fnDecl.params];
442442
const scope = result.getScope(fnDecl);
443443

444444
// Process for nodes
445-
callback(idNode, context);
445+
callback(idNode, params);
446446
(idNode as any).parent = snippetBlock;
447-
if (context) {
448-
(context as any).parent = snippetBlock;
447+
for (const param of params) {
448+
(param as any).parent = snippetBlock;
449449
}
450450

451451
// Process for scope

src/parser/converts/block.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,24 +462,28 @@ export function convertSnippetBlock(
462462
const snippetBlock: SvelteSnippetBlock = {
463463
type: "SvelteSnippetBlock",
464464
id: null as any,
465-
context: null as any,
465+
params: [],
466466
children: [],
467467
parent,
468468
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
469469
};
470470

471471
const closeParenIndex = ctx.code.indexOf(
472472
")",
473-
getWithLoc(node.context || node.expression).end,
473+
getWithLoc(
474+
node.parameters.length > 0
475+
? node.parameters[node.parameters.length - 1]
476+
: node.expression,
477+
).end,
474478
);
475479

476480
ctx.scriptLet.nestSnippetBlock(
477481
node.expression,
478482
closeParenIndex,
479483
snippetBlock,
480-
(id, context) => {
484+
(id, params) => {
481485
snippetBlock.id = id;
482-
snippetBlock.context = context;
486+
snippetBlock.params = params;
483487
},
484488
);
485489

src/parser/svelte-ast-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export interface KeyBlock extends BaseNode {
116116
export interface SnippetBlock extends BaseNode {
117117
type: "SnippetBlock";
118118
expression: ESTree.Identifier;
119-
context: null | ESTree.Pattern;
119+
parameters: ESTree.Pattern[];
120120
children: TemplateNode[];
121121
}
122122

src/visitor-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const svelteKeys: SvelteKeysType = {
3838
SvelteAwaitThenBlock: ["value", "children"],
3939
SvelteAwaitCatchBlock: ["error", "children"],
4040
SvelteKeyBlock: ["expression", "children"],
41-
SvelteSnippetBlock: ["id", "context", "children"],
41+
SvelteSnippetBlock: ["id", "params", "children"],
4242
SvelteAttribute: ["key", "value"],
4343
SvelteShorthandAttribute: ["key", "value"],
4444
SvelteSpreadAttribute: ["argument"],

tests/fixtures/parser/ast/svelte5/docs/snippets/01-output.json

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -782,24 +782,6 @@
782782
}
783783
}
784784
],
785-
"context": {
786-
"type": "Identifier",
787-
"name": "image",
788-
"range": [
789-
17,
790-
22
791-
],
792-
"loc": {
793-
"start": {
794-
"line": 1,
795-
"column": 17
796-
},
797-
"end": {
798-
"line": 1,
799-
"column": 22
800-
}
801-
}
802-
},
803785
"id": {
804786
"type": "Identifier",
805787
"name": "figure",
@@ -818,6 +800,26 @@
818800
}
819801
}
820802
},
803+
"params": [
804+
{
805+
"type": "Identifier",
806+
"name": "image",
807+
"range": [
808+
17,
809+
22
810+
],
811+
"loc": {
812+
"start": {
813+
"line": 1,
814+
"column": 17
815+
},
816+
"end": {
817+
"line": 1,
818+
"column": 22
819+
}
820+
}
821+
}
822+
],
821823
"range": [
822824
0,
823825
201

0 commit comments

Comments
 (0)