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 3 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 ...}`
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
26 changes: 26 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,25 @@
return attribute;
}

function convertAttachTag(
node: SvAST.AttachTag | 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;
});

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
6 changes: 6 additions & 0 deletions src/parser/svelte-ast-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ export interface AttributeShorthand extends BaseNode {
type: "AttributeShorthand";
expression: ESTree.Identifier;
}

export interface AttachTag extends BaseNode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is an AST type for Svelte v4, so there is no need to add it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see!

type: "AttachTag";
expression: ESTree.Expression;
}

export type AttributeOrDirective =
| Attribute
| Spread
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