Skip to content

Commit 92aeee3

Browse files
authored
feat: change AST of {@render} and {#snippet} to match the latest version of svelte v5. (#476)
1 parent fcd8fd4 commit 92aeee3

22 files changed

+2361
-535
lines changed

.changeset/yellow-hornets-bow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": minor
3+
---
4+
5+
feat: change AST of `{@render}` and `{#snippet}` to match the latest version of svelte v5.

docs/AST.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ interface SvelteConstTag extends Node {
423423
}
424424
```
425425

426+
[VariableDeclarator] is a node defined in ESTree.
427+
426428
### SvelteRenderTag
427429

428430
This is the `{@render}` tag node.
@@ -431,12 +433,10 @@ This is the `{@render}` tag node.
431433
interface SvelteRenderTag extends Node {
432434
type: "SvelteRenderTag";
433435
callee: Identifier;
434-
argument: Expression | null;
436+
arguments: (Expression | SpreadElement)[];
435437
}
436438
```
437439

438-
[VariableDeclarator] is a node defined in ESTree.
439-
440440
### SvelteIfBlock
441441

442442
This is the `{#if}` tag node. `{:else if}` is also included in this node.
@@ -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
```

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
4848
},
4949
"peerDependencies": {
50-
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.37"
50+
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.65"
5151
},
5252
"peerDependenciesMeta": {
5353
"svelte": {
@@ -105,7 +105,7 @@
105105
"prettier-plugin-svelte": "^3.1.2",
106106
"rimraf": "^5.0.5",
107107
"semver": "^7.5.4",
108-
"svelte": "^5.0.0-next.37",
108+
"svelte": "^5.0.0-next.65",
109109
"svelte2tsx": "^0.7.0",
110110
"typescript": "~5.3.0",
111111
"typescript-eslint-parser-for-extra-files": "^0.6.0"

src/ast/html.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export interface SvelteConstTag extends BaseNode {
277277
export interface SvelteRenderTag extends BaseNode {
278278
type: "SvelteRenderTag";
279279
callee: ESTree.Identifier;
280-
argument: ESTree.Expression | null;
280+
arguments: (ESTree.Expression | ESTree.SpreadElement)[];
281281
parent:
282282
| SvelteProgram
283283
| SvelteElement
@@ -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

+5-5
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

+8-4
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/converts/render.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ export function convertRenderTag(
1313
const mustache: SvelteRenderTag = {
1414
type: "SvelteRenderTag",
1515
callee: null as any,
16-
argument: null,
16+
arguments: [],
1717
parent,
1818
...ctx.getConvertLocation(node),
1919
};
2020
const calleeRange = getWithLoc(node.expression);
2121
const closeParenIndex = ctx.code.indexOf(
2222
")",
23-
node.argument ? getWithLoc(node.argument).end : calleeRange.end,
23+
node.arguments.length
24+
? getWithLoc(node.arguments[node.arguments.length - 1]).end
25+
: calleeRange.end,
2426
);
2527
ctx.scriptLet.addExpressionFromRange(
2628
[calleeRange.start, closeParenIndex + 1],
@@ -29,9 +31,9 @@ export function convertRenderTag(
2931
(expression: ESTree.SimpleCallExpression) => {
3032
mustache.callee = expression.callee as ESTree.Identifier;
3133
(mustache.callee as any).parent = mustache;
32-
if (expression.arguments.length) {
33-
mustache.argument = expression.arguments[0] as ESTree.Expression;
34-
(mustache.argument as any).parent = mustache;
34+
for (const argument of expression.arguments) {
35+
mustache.arguments.push(argument);
36+
(argument as any).parent = mustache;
3537
}
3638
},
3739
);

src/parser/svelte-ast-types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface ConstTag extends BaseNode {
6161
export interface RenderTag extends BaseNode {
6262
type: "RenderTag";
6363
expression: ESTree.Identifier;
64-
argument: null | ESTree.Expression;
64+
arguments: (ESTree.Expression | ESTree.SpreadElement)[];
6565
}
6666
export interface IfBlock extends BaseNode {
6767
type: "IfBlock";
@@ -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

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const svelteKeys: SvelteKeysType = {
2222
SvelteMustacheTag: ["expression"],
2323
SvelteDebugTag: ["identifiers"],
2424
SvelteConstTag: ["declaration"],
25-
SvelteRenderTag: ["callee", "argument"],
25+
SvelteRenderTag: ["callee", "arguments"],
2626
SvelteIfBlock: ["expression", "children", "else"],
2727
SvelteElseBlock: ["children"],
2828
SvelteEachBlock: [
@@ -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

+56-50
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
@@ -1124,24 +1126,26 @@
11241126
},
11251127
{
11261128
"type": "SvelteRenderTag",
1127-
"argument": {
1128-
"type": "Identifier",
1129-
"name": "image",
1130-
"range": [
1131-
288,
1132-
293
1133-
],
1134-
"loc": {
1135-
"start": {
1136-
"line": 16,
1137-
"column": 19
1138-
},
1139-
"end": {
1140-
"line": 16,
1141-
"column": 24
1129+
"arguments": [
1130+
{
1131+
"type": "Identifier",
1132+
"name": "image",
1133+
"range": [
1134+
288,
1135+
293
1136+
],
1137+
"loc": {
1138+
"start": {
1139+
"line": 16,
1140+
"column": 19
1141+
},
1142+
"end": {
1143+
"line": 16,
1144+
"column": 24
1145+
}
11421146
}
11431147
}
1144-
},
1148+
],
11451149
"callee": {
11461150
"type": "Identifier",
11471151
"name": "figure",
@@ -1233,24 +1237,26 @@
12331237
"children": [
12341238
{
12351239
"type": "SvelteRenderTag",
1236-
"argument": {
1237-
"type": "Identifier",
1238-
"name": "image",
1239-
"range": [
1240-
330,
1241-
335
1242-
],
1243-
"loc": {
1244-
"start": {
1245-
"line": 19,
1246-
"column": 18
1247-
},
1248-
"end": {
1249-
"line": 19,
1250-
"column": 23
1240+
"arguments": [
1241+
{
1242+
"type": "Identifier",
1243+
"name": "image",
1244+
"range": [
1245+
330,
1246+
335
1247+
],
1248+
"loc": {
1249+
"start": {
1250+
"line": 19,
1251+
"column": 18
1252+
},
1253+
"end": {
1254+
"line": 19,
1255+
"column": 23
1256+
}
12511257
}
12521258
}
1253-
},
1259+
],
12541260
"callee": {
12551261
"type": "Identifier",
12561262
"name": "figure",

0 commit comments

Comments
 (0)