Skip to content

Commit 89e053a

Browse files
authored
feat: replace declaration property of SvelteConstTag with declarations property (#641)
1 parent d1ac7f0 commit 89e053a

File tree

11 files changed

+542
-507
lines changed

11 files changed

+542
-507
lines changed

.changeset/lovely-crabs-return.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": minor
3+
---
4+
5+
feat: replace `declaration` property of SvelteConstTag with `declarations` property

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"chai": "^5.0.0",
8585
"env-cmd": "^10.1.0",
8686
"esbuild": "^0.24.0",
87-
"eslint": "~9.16.0",
87+
"eslint": "~9.18.0",
8888
"eslint-config-prettier": "^9.1.0",
8989
"eslint-plugin-eslint-comments": "^3.2.0",
9090
"eslint-plugin-jsdoc": "^50.6.0",

src/ast/html.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ export interface SvelteDebugTag extends BaseNode {
262262
/** Node of const tag. e.g. `{@const}` */
263263
export interface SvelteConstTag extends BaseNode {
264264
type: "SvelteConstTag";
265-
declaration: ESTree.VariableDeclarator;
265+
/**
266+
* @deprecated Use `declarations` instead.
267+
*/
268+
declaration: ESTree.VariableDeclarator; // TODO Remove in v2 and later.
269+
declarations: [ESTree.VariableDeclarator];
266270
parent:
267271
| SvelteProgram
268272
| SvelteElement

src/parser/converts/const.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,28 @@ export function convertConstTag(
1313
const mustache: SvelteConstTag = {
1414
type: "SvelteConstTag",
1515
declaration: null as any,
16+
declarations: [] as any,
1617
parent,
1718
...ctx.getConvertLocation(node),
1819
};
20+
21+
// Link declaration and declarations for backward compatibility.
22+
// TODO Remove in v2 and later.
23+
Object.defineProperty(mustache, "declaration", {
24+
get() {
25+
return mustache.declarations[0];
26+
},
27+
set(value) {
28+
mustache.declarations = [value];
29+
},
30+
enumerable: false,
31+
});
32+
1933
ctx.scriptLet.addVariableDeclarator(
2034
getDeclaratorFromConstTag(node),
2135
mustache,
2236
(declaration) => {
23-
mustache.declaration = declaration;
37+
mustache.declarations = [declaration];
2438
},
2539
);
2640
const atConstStart = ctx.code.indexOf("@const", mustache.range[0]);

src/visitor-keys.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const svelteKeys: SvelteKeysType = {
2121
SvelteLiteral: [],
2222
SvelteMustacheTag: ["expression"],
2323
SvelteDebugTag: ["identifiers"],
24-
SvelteConstTag: ["declaration"],
24+
SvelteConstTag: ["declarations"],
2525
SvelteRenderTag: ["expression"],
2626
SvelteIfBlock: ["expression", "children", "else"],
2727
SvelteElseBlock: ["children"],

tests/fixtures/parser/ast/at-const01-within-component-output.json

+54-52
Original file line numberDiff line numberDiff line change
@@ -300,59 +300,75 @@
300300
},
301301
{
302302
"type": "SvelteConstTag",
303-
"declaration": {
304-
"type": "VariableDeclarator",
305-
"id": {
306-
"type": "Identifier",
307-
"name": "a",
308-
"range": [
309-
101,
310-
102
311-
],
312-
"loc": {
313-
"start": {
314-
"line": 6,
315-
"column": 10
316-
},
317-
"end": {
318-
"line": 6,
319-
"column": 11
320-
}
321-
}
322-
},
323-
"init": {
324-
"type": "BinaryExpression",
325-
"left": {
303+
"declarations": [
304+
{
305+
"type": "VariableDeclarator",
306+
"id": {
326307
"type": "Identifier",
327-
"name": "b",
308+
"name": "a",
328309
"range": [
329-
105,
330-
106
310+
101,
311+
102
331312
],
332313
"loc": {
333314
"start": {
334315
"line": 6,
335-
"column": 14
316+
"column": 10
336317
},
337318
"end": {
338319
"line": 6,
339-
"column": 15
320+
"column": 11
340321
}
341322
}
342323
},
343-
"operator": "*",
344-
"right": {
345-
"type": "Literal",
346-
"raw": "2",
347-
"value": 2,
324+
"init": {
325+
"type": "BinaryExpression",
326+
"left": {
327+
"type": "Identifier",
328+
"name": "b",
329+
"range": [
330+
105,
331+
106
332+
],
333+
"loc": {
334+
"start": {
335+
"line": 6,
336+
"column": 14
337+
},
338+
"end": {
339+
"line": 6,
340+
"column": 15
341+
}
342+
}
343+
},
344+
"operator": "*",
345+
"right": {
346+
"type": "Literal",
347+
"raw": "2",
348+
"value": 2,
349+
"range": [
350+
109,
351+
110
352+
],
353+
"loc": {
354+
"start": {
355+
"line": 6,
356+
"column": 18
357+
},
358+
"end": {
359+
"line": 6,
360+
"column": 19
361+
}
362+
}
363+
},
348364
"range": [
349-
109,
365+
105,
350366
110
351367
],
352368
"loc": {
353369
"start": {
354370
"line": 6,
355-
"column": 18
371+
"column": 14
356372
},
357373
"end": {
358374
"line": 6,
@@ -361,35 +377,21 @@
361377
}
362378
},
363379
"range": [
364-
105,
380+
101,
365381
110
366382
],
367383
"loc": {
368384
"start": {
369385
"line": 6,
370-
"column": 14
386+
"column": 10
371387
},
372388
"end": {
373389
"line": 6,
374390
"column": 19
375391
}
376392
}
377-
},
378-
"range": [
379-
101,
380-
110
381-
],
382-
"loc": {
383-
"start": {
384-
"line": 6,
385-
"column": 10
386-
},
387-
"end": {
388-
"line": 6,
389-
"column": 19
390-
}
391393
}
392-
},
394+
],
393395
"range": [
394396
93,
395397
111

0 commit comments

Comments
 (0)