Skip to content

feat: support {@attach ...} #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/public-sheep-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": minor
---

feat: support `{@attach ...}`
13 changes: 13 additions & 0 deletions docs/AST.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ interface SvelteStartTag extends Node {
| SvelteAttribute
| SvelteShorthandAttribute
| SvelteSpreadAttribute
| SvelteAttachTag
| SvelteDirective
| SvelteStyleDirective
| SvelteSpecialDirective
Expand Down Expand Up @@ -451,6 +452,18 @@ interface SvelteRenderTag extends Node {
}
```

### SvelteAttachTag

This is `{@attach}` tag node.

```ts
export interface SvelteAttachTag extends BaseNode {
type: "SvelteAttachTag";
expression: ESTree.Expression;
parent: SvelteStartTag;
}
```

### SvelteIfBlock

This is the `{#if}` tag node. `{:else if}` is also included in this node.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"prettier-plugin-svelte": "^3.3.3",
"rimraf": "^6.0.1",
"semver": "^7.7.1",
"svelte": "^5.25.3",
"svelte": "^5.30.1",
"svelte2tsx": "^0.7.35",
"tsx": "^4.19.3",
"typescript": "~5.8.2",
Expand Down
8 changes: 8 additions & 0 deletions src/ast/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
| SvelteAttribute
| SvelteShorthandAttribute
| SvelteSpreadAttribute
| SvelteAttachTag
| SvelteDirective
| SvelteStyleDirective
| SvelteSpecialDirective
Expand Down Expand Up @@ -142,6 +143,7 @@
| SvelteAttribute
| SvelteShorthandAttribute
| SvelteSpreadAttribute
| SvelteAttachTag
| SvelteDirective
| SvelteStyleDirective
| SvelteSpecialDirective
Expand Down Expand Up @@ -266,7 +268,7 @@
/**
* @deprecated Use `declarations` instead.
*/
declaration: ESTree.VariableDeclarator; // TODO Remove in v2 and later.

Check warning on line 271 in src/ast/html.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO Remove in v2 and later.'
declarations: [ESTree.VariableDeclarator];
parent:
| SvelteProgram
Expand Down Expand Up @@ -541,6 +543,12 @@
parent: SvelteStartTag;
}

export interface SvelteAttachTag extends BaseNode {
type: "SvelteAttachTag";
expression: ESTree.Expression;
parent: SvelteStartTag;
}

/** Node of directive. e.g. `<input bind:value />` */
export type SvelteDirective =
| SvelteActionDirective
Expand Down
32 changes: 32 additions & 0 deletions src/parser/converts/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SvelteAnimationDirective,
SvelteAttribute,
SvelteShorthandAttribute,
SvelteAttachTag,
SvelteBindingDirective,
SvelteClassDirective,
SvelteDirective,
Expand Down Expand Up @@ -48,6 +49,7 @@
| SvAST.AttributeOrDirective
| Compiler.Attribute
| Compiler.SpreadAttribute
| Compiler.AttachTag
| Compiler.Directive
)[],
parent: SvelteStartTag,
Expand All @@ -56,6 +58,7 @@
| SvelteAttribute
| SvelteShorthandAttribute
| SvelteSpreadAttribute
| SvelteAttachTag
| SvelteDirective
| SvelteStyleDirective
> {
Expand All @@ -68,6 +71,10 @@
yield convertSpreadAttribute(attr, parent, ctx);
continue;
}
if (attr.type === "AttachTag") {
yield convertAttachTag(attr, parent, ctx);
continue;
}
if (attr.type === "BindDirective" || attr.type === "Binding") {
yield convertBindingDirective(attr, parent, ctx);
continue;
Expand Down Expand Up @@ -171,7 +178,7 @@
(key as any).parent = sAttr;
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => {
if (
// FIXME: Older parsers may use the same node. In that case, do not replace.

Check warning on line 181 in src/parser/converts/attr.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Older parsers may use the same...'
// We will drop support for ESLint v7 in the next major version and remove this branch.
es.key !== es.value
) {
Expand Down Expand Up @@ -344,6 +351,31 @@
return attribute;
}

function convertAttachTag(
node: Compiler.AttachTag,
parent: SvelteAttachTag["parent"],
ctx: Context,
): SvelteAttachTag {
const attachTag: SvelteAttachTag = {
type: "SvelteAttachTag",
expression: node.expression,
parent,
...ctx.getConvertLocation(node),
};

ctx.scriptLet.addExpression(node.expression, attachTag, null, (es) => {
attachTag.expression = es;
});

const atAttachStart = ctx.code.indexOf("@attach", attachTag.range[0]);
ctx.addToken("MustacheKeyword", {
start: atAttachStart,
end: atAttachStart + 7,
});

return attachTag;
}

/** Convert for Binding Directive */
function convertBindingDirective(
node: SvAST.DirectiveForExpression | Compiler.BindDirective,
Expand Down
9 changes: 8 additions & 1 deletion src/parser/converts/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,19 @@ export function* convertChildren(
function extractLetDirectives(fragment: {
attributes:
| SvAST.AttributeOrDirective[]
| (Compiler.Attribute | Compiler.SpreadAttribute | Compiler.Directive)[];
| (
| Compiler.Attribute
| Compiler.SpreadAttribute
| Compiler.AttachTag
| Compiler.Directive
)[];
}): {
letDirectives: (SvAST.LetDirective | Compiler.LetDirective)[];
attributes: Exclude<
| SvAST.AttributeOrDirective
| Compiler.Attribute
| Compiler.SpreadAttribute
| Compiler.AttachTag
| Compiler.Directive,
SvAST.LetDirective | Compiler.LetDirective
>[];
Expand All @@ -239,6 +245,7 @@ function extractLetDirectives(fragment: {
| SvAST.AttributeOrDirective
| Compiler.Attribute
| Compiler.SpreadAttribute
| Compiler.AttachTag
| Compiler.Directive,
SvAST.LetDirective | Compiler.LetDirective
>[] = [];
Expand Down
1 change: 1 addition & 0 deletions src/parser/svelte-ast-types-for-v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type SnippetBlock = AST.SnippetBlock;
export type Comment = AST.Comment;
export type Attribute = AST.Attribute;
export type SpreadAttribute = AST.SpreadAttribute;
export type AttachTag = AST.AttachTag;
export type AnimateDirective = AST.AnimateDirective;
export type BindDirective = AST.BindDirective;
export type ClassDirective = AST.ClassDirective;
Expand Down
1 change: 1 addition & 0 deletions src/visitor-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const svelteKeys: SvelteKeysType = {
SvelteAttribute: ["key", "value"],
SvelteShorthandAttribute: ["key", "value"],
SvelteSpreadAttribute: ["argument"],
SvelteAttachTag: ["expression"],
SvelteDirective: ["key", "expression"],
SvelteStyleDirective: ["key", "value"],
SvelteSpecialDirective: ["key", "expression"],
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/parser/ast/svelte5/attach-ts-01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import type { Attachment } from 'svelte/attachments';


const myAttachment: Attachment = (element) => {
console.log(element.nodeName); // 'DIV'

return () => {
console.log('cleaning up');
};
};
</script>

<div {@attach myAttachment}>...</div>
Loading
Loading