Skip to content

Commit 696b55f

Browse files
committed
feat: support attach tag
1 parent 10fc353 commit 696b55f

23 files changed

+33841
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"prettier-plugin-svelte": "^3.3.3",
106106
"rimraf": "^6.0.1",
107107
"semver": "^7.7.1",
108-
"svelte": "^5.25.3",
108+
"svelte": "^5.30.1",
109109
"svelte2tsx": "^0.7.35",
110110
"tsx": "^4.19.3",
111111
"typescript": "~5.8.2",

src/ast/html.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type SvelteHTMLNode =
3131
| SvelteAttribute
3232
| SvelteShorthandAttribute
3333
| SvelteSpreadAttribute
34+
| SvelteAttachTag
3435
| SvelteDirective
3536
| SvelteStyleDirective
3637
| SvelteSpecialDirective
@@ -142,6 +143,7 @@ export interface SvelteStartTag extends BaseNode {
142143
| SvelteAttribute
143144
| SvelteShorthandAttribute
144145
| SvelteSpreadAttribute
146+
| SvelteAttachTag
145147
| SvelteDirective
146148
| SvelteStyleDirective
147149
| SvelteSpecialDirective
@@ -541,6 +543,12 @@ export interface SvelteSpreadAttribute extends BaseNode {
541543
parent: SvelteStartTag;
542544
}
543545

546+
export interface SvelteAttachTag extends BaseNode {
547+
type: "SvelteAttachTag";
548+
expression: ESTree.Expression;
549+
parent: SvelteStartTag;
550+
}
551+
544552
/** Node of directive. e.g. `<input bind:value />` */
545553
export type SvelteDirective =
546554
| SvelteActionDirective

src/parser/converts/attr.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
SvelteAnimationDirective,
44
SvelteAttribute,
55
SvelteShorthandAttribute,
6+
SvelteAttachTag,
67
SvelteBindingDirective,
78
SvelteClassDirective,
89
SvelteDirective,
@@ -56,6 +57,7 @@ export function* convertAttributes(
5657
| SvelteAttribute
5758
| SvelteShorthandAttribute
5859
| SvelteSpreadAttribute
60+
| SvelteAttachTag
5961
| SvelteDirective
6062
| SvelteStyleDirective
6163
> {
@@ -68,6 +70,10 @@ export function* convertAttributes(
6870
yield convertSpreadAttribute(attr, parent, ctx);
6971
continue;
7072
}
73+
if (attr.type === "AttachTag") {
74+
yield convertAttachTag(attr, parent, ctx);
75+
continue;
76+
}
7177
if (attr.type === "BindDirective" || attr.type === "Binding") {
7278
yield convertBindingDirective(attr, parent, ctx);
7379
continue;
@@ -344,6 +350,25 @@ function convertSpreadAttribute(
344350
return attribute;
345351
}
346352

353+
function convertAttachTag(
354+
node: SvAST.AttachTag | Compiler.AttachTag,
355+
parent: SvelteAttachTag["parent"],
356+
ctx: Context,
357+
): SvelteAttachTag {
358+
const attachTag: SvelteAttachTag = {
359+
type: "SvelteAttachTag",
360+
expression: node.expression,
361+
parent,
362+
...ctx.getConvertLocation(node),
363+
};
364+
365+
ctx.scriptLet.addExpression(node.expression, attachTag, null, (es) => {
366+
attachTag.expression = es;
367+
});
368+
369+
return attachTag;
370+
}
371+
347372
/** Convert for Binding Directive */
348373
function convertBindingDirective(
349374
node: SvAST.DirectiveForExpression | Compiler.BindDirective,

src/parser/svelte-ast-types-for-v5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type SnippetBlock = AST.SnippetBlock;
3737
export type Comment = AST.Comment;
3838
export type Attribute = AST.Attribute;
3939
export type SpreadAttribute = AST.SpreadAttribute;
40+
export type AttachTag = AST.AttachTag;
4041
export type AnimateDirective = AST.AnimateDirective;
4142
export type BindDirective = AST.BindDirective;
4243
export type ClassDirective = AST.ClassDirective;

src/parser/svelte-ast-types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,16 @@ 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+
206212
export type AttributeOrDirective =
207213
| Attribute
208214
| Spread
215+
| AttachTag
209216
| Directive
210217
| StyleDirective;
211218

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
import type { Attachment } from 'svelte/attachments';
3+
4+
5+
const myAttachment: Attachment = (element) => {
6+
console.log(element.nodeName); // 'DIV'
7+
8+
return () => {
9+
console.log('cleaning up');
10+
};
11+
};
12+
</script>
13+
14+
<div {@attach myAttachment}>...</div>

0 commit comments

Comments
 (0)