Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 74351f7

Browse files
committedMay 15, 2025·
update according to review
1 parent 3feedf0 commit 74351f7

22 files changed

+9734
-12347
lines changed
 

‎docs/AST.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ interface SvelteStartTag extends Node {
169169
| SvelteAttribute
170170
| SvelteShorthandAttribute
171171
| SvelteSpreadAttribute
172+
| SvelteAttachTag
172173
| SvelteDirective
173174
| SvelteStyleDirective
174175
| SvelteSpecialDirective
@@ -451,6 +452,18 @@ interface SvelteRenderTag extends Node {
451452
}
452453
```
453454

455+
### SvelteAttachTag
456+
457+
This is `{@attach}` tag node.
458+
459+
```ts
460+
export interface SvelteAttachTag extends BaseNode {
461+
type: "SvelteAttachTag";
462+
expression: ESTree.Expression;
463+
parent: SvelteStartTag;
464+
}
465+
```
466+
454467
### SvelteIfBlock
455468

456469
This is the `{#if}` tag node. `{:else if}` is also included in this node.

‎src/parser/converts/attr.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function convertSpreadAttribute(
352352
}
353353

354354
function convertAttachTag(
355-
node: SvAST.AttachTag | Compiler.AttachTag,
355+
node: Compiler.AttachTag,
356356
parent: SvelteAttachTag["parent"],
357357
ctx: Context,
358358
): SvelteAttachTag {
@@ -367,6 +367,12 @@ function convertAttachTag(
367367
attachTag.expression = es;
368368
});
369369

370+
const atAttachStart = ctx.code.indexOf("@attach", attachTag.range[0]);
371+
ctx.addToken("MustacheKeyword", {
372+
start: atAttachStart,
373+
end: atAttachStart + 7,
374+
});
375+
370376
return attachTag;
371377
}
372378

‎src/parser/svelte-ast-types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,6 @@ export interface AttributeShorthand extends BaseNode {
203203
type: "AttributeShorthand";
204204
expression: ESTree.Identifier;
205205
}
206-
207-
export interface AttachTag extends BaseNode {
208-
type: "AttachTag";
209-
expression: ESTree.Expression;
210-
}
211-
212206
export type AttributeOrDirective =
213207
| Attribute
214208
| Spread

‎tests/fixtures/parser/ast/svelte5/attach-ts-01-output.json

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,124 +1939,16 @@
19391939
}
19401940
},
19411941
{
1942-
"type": "Punctuator",
1943-
"value": "@",
1942+
"type": "MustacheKeyword",
1943+
"value": "@attach",
19441944
"range": [
19451945
242,
1946-
243
1947-
],
1948-
"loc": {
1949-
"start": {
1950-
"line": 14,
1951-
"column": 6
1952-
},
1953-
"end": {
1954-
"line": 14,
1955-
"column": 7
1956-
}
1957-
}
1958-
},
1959-
{
1960-
"type": "Identifier",
1961-
"value": "a",
1962-
"range": [
1963-
243,
1964-
244
1965-
],
1966-
"loc": {
1967-
"start": {
1968-
"line": 14,
1969-
"column": 7
1970-
},
1971-
"end": {
1972-
"line": 14,
1973-
"column": 8
1974-
}
1975-
}
1976-
},
1977-
{
1978-
"type": "Identifier",
1979-
"value": "t",
1980-
"range": [
1981-
244,
1982-
245
1983-
],
1984-
"loc": {
1985-
"start": {
1986-
"line": 14,
1987-
"column": 8
1988-
},
1989-
"end": {
1990-
"line": 14,
1991-
"column": 9
1992-
}
1993-
}
1994-
},
1995-
{
1996-
"type": "Identifier",
1997-
"value": "t",
1998-
"range": [
1999-
245,
2000-
246
2001-
],
2002-
"loc": {
2003-
"start": {
2004-
"line": 14,
2005-
"column": 9
2006-
},
2007-
"end": {
2008-
"line": 14,
2009-
"column": 10
2010-
}
2011-
}
2012-
},
2013-
{
2014-
"type": "Identifier",
2015-
"value": "a",
2016-
"range": [
2017-
246,
2018-
247
2019-
],
2020-
"loc": {
2021-
"start": {
2022-
"line": 14,
2023-
"column": 10
2024-
},
2025-
"end": {
2026-
"line": 14,
2027-
"column": 11
2028-
}
2029-
}
2030-
},
2031-
{
2032-
"type": "Identifier",
2033-
"value": "c",
2034-
"range": [
2035-
247,
2036-
248
2037-
],
2038-
"loc": {
2039-
"start": {
2040-
"line": 14,
2041-
"column": 11
2042-
},
2043-
"end": {
2044-
"line": 14,
2045-
"column": 12
2046-
}
2047-
}
2048-
},
2049-
{
2050-
"type": "Identifier",
2051-
"value": "h",
2052-
"range": [
2053-
248,
20541946
249
20551947
],
20561948
"loc": {
20571949
"start": {
20581950
"line": 14,
2059-
"column": 12
1951+
"column": 6
20601952
},
20611953
"end": {
20621954
"line": 14,

‎tests/fixtures/parser/ast/svelte5/attach-ts-02-output.json

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,124 +2777,16 @@
27772777
}
27782778
},
27792779
{
2780-
"type": "Punctuator",
2781-
"value": "@",
2780+
"type": "MustacheKeyword",
2781+
"value": "@attach",
27822782
"range": [
27832783
348,
2784-
349
2785-
],
2786-
"loc": {
2787-
"start": {
2788-
"line": 17,
2789-
"column": 9
2790-
},
2791-
"end": {
2792-
"line": 17,
2793-
"column": 10
2794-
}
2795-
}
2796-
},
2797-
{
2798-
"type": "Identifier",
2799-
"value": "a",
2800-
"range": [
2801-
349,
2802-
350
2803-
],
2804-
"loc": {
2805-
"start": {
2806-
"line": 17,
2807-
"column": 10
2808-
},
2809-
"end": {
2810-
"line": 17,
2811-
"column": 11
2812-
}
2813-
}
2814-
},
2815-
{
2816-
"type": "Identifier",
2817-
"value": "t",
2818-
"range": [
2819-
350,
2820-
351
2821-
],
2822-
"loc": {
2823-
"start": {
2824-
"line": 17,
2825-
"column": 11
2826-
},
2827-
"end": {
2828-
"line": 17,
2829-
"column": 12
2830-
}
2831-
}
2832-
},
2833-
{
2834-
"type": "Identifier",
2835-
"value": "t",
2836-
"range": [
2837-
351,
2838-
352
2839-
],
2840-
"loc": {
2841-
"start": {
2842-
"line": 17,
2843-
"column": 12
2844-
},
2845-
"end": {
2846-
"line": 17,
2847-
"column": 13
2848-
}
2849-
}
2850-
},
2851-
{
2852-
"type": "Identifier",
2853-
"value": "a",
2854-
"range": [
2855-
352,
2856-
353
2857-
],
2858-
"loc": {
2859-
"start": {
2860-
"line": 17,
2861-
"column": 13
2862-
},
2863-
"end": {
2864-
"line": 17,
2865-
"column": 14
2866-
}
2867-
}
2868-
},
2869-
{
2870-
"type": "Identifier",
2871-
"value": "c",
2872-
"range": [
2873-
353,
2874-
354
2875-
],
2876-
"loc": {
2877-
"start": {
2878-
"line": 17,
2879-
"column": 14
2880-
},
2881-
"end": {
2882-
"line": 17,
2883-
"column": 15
2884-
}
2885-
}
2886-
},
2887-
{
2888-
"type": "Identifier",
2889-
"value": "h",
2890-
"range": [
2891-
354,
28922784
355
28932785
],
28942786
"loc": {
28952787
"start": {
28962788
"line": 17,
2897-
"column": 15
2789+
"column": 9
28982790
},
28992791
"end": {
29002792
"line": 17,

‎tests/fixtures/parser/ast/svelte5/attach-ts-03-output.json

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,124 +1888,16 @@
18881888
}
18891889
},
18901890
{
1891-
"type": "Punctuator",
1892-
"value": "@",
1891+
"type": "MustacheKeyword",
1892+
"value": "@attach",
18931893
"range": [
18941894
105,
1895-
106
1896-
],
1897-
"loc": {
1898-
"start": {
1899-
"line": 8,
1900-
"column": 2
1901-
},
1902-
"end": {
1903-
"line": 8,
1904-
"column": 3
1905-
}
1906-
}
1907-
},
1908-
{
1909-
"type": "Identifier",
1910-
"value": "a",
1911-
"range": [
1912-
106,
1913-
107
1914-
],
1915-
"loc": {
1916-
"start": {
1917-
"line": 8,
1918-
"column": 3
1919-
},
1920-
"end": {
1921-
"line": 8,
1922-
"column": 4
1923-
}
1924-
}
1925-
},
1926-
{
1927-
"type": "Identifier",
1928-
"value": "t",
1929-
"range": [
1930-
107,
1931-
108
1932-
],
1933-
"loc": {
1934-
"start": {
1935-
"line": 8,
1936-
"column": 4
1937-
},
1938-
"end": {
1939-
"line": 8,
1940-
"column": 5
1941-
}
1942-
}
1943-
},
1944-
{
1945-
"type": "Identifier",
1946-
"value": "t",
1947-
"range": [
1948-
108,
1949-
109
1950-
],
1951-
"loc": {
1952-
"start": {
1953-
"line": 8,
1954-
"column": 5
1955-
},
1956-
"end": {
1957-
"line": 8,
1958-
"column": 6
1959-
}
1960-
}
1961-
},
1962-
{
1963-
"type": "Identifier",
1964-
"value": "a",
1965-
"range": [
1966-
109,
1967-
110
1968-
],
1969-
"loc": {
1970-
"start": {
1971-
"line": 8,
1972-
"column": 6
1973-
},
1974-
"end": {
1975-
"line": 8,
1976-
"column": 7
1977-
}
1978-
}
1979-
},
1980-
{
1981-
"type": "Identifier",
1982-
"value": "c",
1983-
"range": [
1984-
110,
1985-
111
1986-
],
1987-
"loc": {
1988-
"start": {
1989-
"line": 8,
1990-
"column": 7
1991-
},
1992-
"end": {
1993-
"line": 8,
1994-
"column": 8
1995-
}
1996-
}
1997-
},
1998-
{
1999-
"type": "Identifier",
2000-
"value": "h",
2001-
"range": [
2002-
111,
20031895
112
20041896
],
20051897
"loc": {
20061898
"start": {
20071899
"line": 8,
2008-
"column": 8
1900+
"column": 2
20091901
},
20101902
"end": {
20111903
"line": 8,

‎tests/fixtures/parser/ast/svelte5/attach01-output.json

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,124 +1393,16 @@
13931393
}
13941394
},
13951395
{
1396-
"type": "Punctuator",
1397-
"value": "@",
1396+
"type": "MustacheKeyword",
1397+
"value": "@attach",
13981398
"range": [
13991399
215,
1400-
216
1401-
],
1402-
"loc": {
1403-
"start": {
1404-
"line": 12,
1405-
"column": 6
1406-
},
1407-
"end": {
1408-
"line": 12,
1409-
"column": 7
1410-
}
1411-
}
1412-
},
1413-
{
1414-
"type": "Identifier",
1415-
"value": "a",
1416-
"range": [
1417-
216,
1418-
217
1419-
],
1420-
"loc": {
1421-
"start": {
1422-
"line": 12,
1423-
"column": 7
1424-
},
1425-
"end": {
1426-
"line": 12,
1427-
"column": 8
1428-
}
1429-
}
1430-
},
1431-
{
1432-
"type": "Identifier",
1433-
"value": "t",
1434-
"range": [
1435-
217,
1436-
218
1437-
],
1438-
"loc": {
1439-
"start": {
1440-
"line": 12,
1441-
"column": 8
1442-
},
1443-
"end": {
1444-
"line": 12,
1445-
"column": 9
1446-
}
1447-
}
1448-
},
1449-
{
1450-
"type": "Identifier",
1451-
"value": "t",
1452-
"range": [
1453-
218,
1454-
219
1455-
],
1456-
"loc": {
1457-
"start": {
1458-
"line": 12,
1459-
"column": 9
1460-
},
1461-
"end": {
1462-
"line": 12,
1463-
"column": 10
1464-
}
1465-
}
1466-
},
1467-
{
1468-
"type": "Identifier",
1469-
"value": "a",
1470-
"range": [
1471-
219,
1472-
220
1473-
],
1474-
"loc": {
1475-
"start": {
1476-
"line": 12,
1477-
"column": 10
1478-
},
1479-
"end": {
1480-
"line": 12,
1481-
"column": 11
1482-
}
1483-
}
1484-
},
1485-
{
1486-
"type": "Identifier",
1487-
"value": "c",
1488-
"range": [
1489-
220,
1490-
221
1491-
],
1492-
"loc": {
1493-
"start": {
1494-
"line": 12,
1495-
"column": 11
1496-
},
1497-
"end": {
1498-
"line": 12,
1499-
"column": 12
1500-
}
1501-
}
1502-
},
1503-
{
1504-
"type": "Identifier",
1505-
"value": "h",
1506-
"range": [
1507-
221,
15081400
222
15091401
],
15101402
"loc": {
15111403
"start": {
15121404
"line": 12,
1513-
"column": 12
1405+
"column": 6
15141406
},
15151407
"end": {
15161408
"line": 12,

‎tests/fixtures/parser/ast/svelte5/attach02-output.json

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,124 +2253,16 @@
22532253
}
22542254
},
22552255
{
2256-
"type": "Punctuator",
2257-
"value": "@",
2256+
"type": "MustacheKeyword",
2257+
"value": "@attach",
22582258
"range": [
22592259
356,
2260-
357
2261-
],
2262-
"loc": {
2263-
"start": {
2264-
"line": 20,
2265-
"column": 9
2266-
},
2267-
"end": {
2268-
"line": 20,
2269-
"column": 10
2270-
}
2271-
}
2272-
},
2273-
{
2274-
"type": "Identifier",
2275-
"value": "a",
2276-
"range": [
2277-
357,
2278-
358
2279-
],
2280-
"loc": {
2281-
"start": {
2282-
"line": 20,
2283-
"column": 10
2284-
},
2285-
"end": {
2286-
"line": 20,
2287-
"column": 11
2288-
}
2289-
}
2290-
},
2291-
{
2292-
"type": "Identifier",
2293-
"value": "t",
2294-
"range": [
2295-
358,
2296-
359
2297-
],
2298-
"loc": {
2299-
"start": {
2300-
"line": 20,
2301-
"column": 11
2302-
},
2303-
"end": {
2304-
"line": 20,
2305-
"column": 12
2306-
}
2307-
}
2308-
},
2309-
{
2310-
"type": "Identifier",
2311-
"value": "t",
2312-
"range": [
2313-
359,
2314-
360
2315-
],
2316-
"loc": {
2317-
"start": {
2318-
"line": 20,
2319-
"column": 12
2320-
},
2321-
"end": {
2322-
"line": 20,
2323-
"column": 13
2324-
}
2325-
}
2326-
},
2327-
{
2328-
"type": "Identifier",
2329-
"value": "a",
2330-
"range": [
2331-
360,
2332-
361
2333-
],
2334-
"loc": {
2335-
"start": {
2336-
"line": 20,
2337-
"column": 13
2338-
},
2339-
"end": {
2340-
"line": 20,
2341-
"column": 14
2342-
}
2343-
}
2344-
},
2345-
{
2346-
"type": "Identifier",
2347-
"value": "c",
2348-
"range": [
2349-
361,
2350-
362
2351-
],
2352-
"loc": {
2353-
"start": {
2354-
"line": 20,
2355-
"column": 14
2356-
},
2357-
"end": {
2358-
"line": 20,
2359-
"column": 15
2360-
}
2361-
}
2362-
},
2363-
{
2364-
"type": "Identifier",
2365-
"value": "h",
2366-
"range": [
2367-
362,
23682260
363
23692261
],
23702262
"loc": {
23712263
"start": {
23722264
"line": 20,
2373-
"column": 15
2265+
"column": 9
23742266
},
23752267
"end": {
23762268
"line": 20,

‎tests/fixtures/parser/ast/svelte5/attach03-output.json

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,124 +1739,16 @@
17391739
}
17401740
},
17411741
{
1742-
"type": "Punctuator",
1743-
"value": "@",
1742+
"type": "MustacheKeyword",
1743+
"value": "@attach",
17441744
"range": [
17451745
95,
1746-
96
1747-
],
1748-
"loc": {
1749-
"start": {
1750-
"line": 8,
1751-
"column": 2
1752-
},
1753-
"end": {
1754-
"line": 8,
1755-
"column": 3
1756-
}
1757-
}
1758-
},
1759-
{
1760-
"type": "Identifier",
1761-
"value": "a",
1762-
"range": [
1763-
96,
1764-
97
1765-
],
1766-
"loc": {
1767-
"start": {
1768-
"line": 8,
1769-
"column": 3
1770-
},
1771-
"end": {
1772-
"line": 8,
1773-
"column": 4
1774-
}
1775-
}
1776-
},
1777-
{
1778-
"type": "Identifier",
1779-
"value": "t",
1780-
"range": [
1781-
97,
1782-
98
1783-
],
1784-
"loc": {
1785-
"start": {
1786-
"line": 8,
1787-
"column": 4
1788-
},
1789-
"end": {
1790-
"line": 8,
1791-
"column": 5
1792-
}
1793-
}
1794-
},
1795-
{
1796-
"type": "Identifier",
1797-
"value": "t",
1798-
"range": [
1799-
98,
1800-
99
1801-
],
1802-
"loc": {
1803-
"start": {
1804-
"line": 8,
1805-
"column": 5
1806-
},
1807-
"end": {
1808-
"line": 8,
1809-
"column": 6
1810-
}
1811-
}
1812-
},
1813-
{
1814-
"type": "Identifier",
1815-
"value": "a",
1816-
"range": [
1817-
99,
1818-
100
1819-
],
1820-
"loc": {
1821-
"start": {
1822-
"line": 8,
1823-
"column": 6
1824-
},
1825-
"end": {
1826-
"line": 8,
1827-
"column": 7
1828-
}
1829-
}
1830-
},
1831-
{
1832-
"type": "Identifier",
1833-
"value": "c",
1834-
"range": [
1835-
100,
1836-
101
1837-
],
1838-
"loc": {
1839-
"start": {
1840-
"line": 8,
1841-
"column": 7
1842-
},
1843-
"end": {
1844-
"line": 8,
1845-
"column": 8
1846-
}
1847-
}
1848-
},
1849-
{
1850-
"type": "Identifier",
1851-
"value": "h",
1852-
"range": [
1853-
101,
18541746
102
18551747
],
18561748
"loc": {
18571749
"start": {
18581750
"line": 8,
1859-
"column": 8
1751+
"column": 2
18601752
},
18611753
"end": {
18621754
"line": 8,
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
<script>
2-
/** @type {import('svelte/elements').HTMLButtonAttributes} */
3-
let { children, ...props } = $props();
2+
import tippy from 'tippy.js';
3+
import Button from './Button.svelte';
4+
5+
let content = $state('Hello!');
6+
7+
/**
8+
* @param {string} content
9+
* @returns {import('svelte/attachments').Attachment}
10+
*/
11+
function tooltip(content) {
12+
return (element) => {
13+
const tooltip = tippy(element, { content });
14+
return tooltip.destroy;
15+
};
16+
}
417
</script>
518

6-
<!-- `props` includes attachments -->
7-
<button {...props}>
8-
{@render children?.()}
9-
</button>
19+
<input bind:value={content} />
20+
21+
<Button {@attach tooltip(content)}>
22+
Hover me
23+
</Button>

‎tests/fixtures/parser/ast/svelte5/attach04-output.json

Lines changed: 1951 additions & 557 deletions
Large diffs are not rendered by default.

‎tests/fixtures/parser/ast/svelte5/attach04-prefer-const-result.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎tests/fixtures/parser/ast/svelte5/attach04-scope-output.json

Lines changed: 2468 additions & 457 deletions
Large diffs are not rendered by default.
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
<script lang="ts">
2-
import type { HTMLButtonAttributes } from 'svelte/elements';
2+
import tippy from 'tippy.js';
3+
import Button from './Button.svelte';
4+
import type { Attachment } from 'svelte/attachments';
35
4-
let { children, ...props }: HTMLButtonAttributes = $props();
6+
let content = $state('Hello!');
7+
8+
function tooltip(content: string): Attachment {
9+
return (element) => {
10+
const tooltip = tippy(element, { content });
11+
return tooltip.destroy;
12+
};
13+
}
514
</script>
615

7-
<!-- `props` includes attachments -->
8-
<button {...props}>
9-
{@render children?.()}
10-
</button>
16+
<input bind:value={content} />
17+
18+
<Button {@attach tooltip(content)}>
19+
Hover me
20+
</Button>

‎tests/fixtures/parser/ast/svelte5/attach04-ts-output.json

Lines changed: 2291 additions & 826 deletions
Large diffs are not rendered by default.

‎tests/fixtures/parser/ast/svelte5/attach04-ts-prefer-const-result.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎tests/fixtures/parser/ast/svelte5/attach04-ts-scope-output.json

Lines changed: 2950 additions & 662 deletions
Large diffs are not rendered by default.

‎tests/fixtures/parser/ast/svelte5/attach05-input.svelte

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎tests/fixtures/parser/ast/svelte5/attach05-output.json

Lines changed: 0 additions & 2829 deletions
This file was deleted.

‎tests/fixtures/parser/ast/svelte5/attach05-scope-output.json

Lines changed: 0 additions & 2906 deletions
This file was deleted.

‎tests/fixtures/parser/ast/svelte5/attach05-ts-input.svelte

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎tests/fixtures/parser/ast/svelte5/attach05-ts-output.json

Lines changed: 0 additions & 3354 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.