Skip to content

feat: support attachments #495

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 1 commit into from
May 14, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prettier-plugin-svelte changelog

## 3.4.0

- (feat) Svelte 5: support attachments (`{@attach ...}`)

## 3.3.3

- (fix) Svelte 5: ensure bind get/set is broken up correctly when too long
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-svelte",
"version": "3.3.3",
"version": "3.4.0",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"files": [
Expand Down
3 changes: 3 additions & 0 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export function embed(path: FastPath, _options: Options) {
case 'Spread':
printJS(parent, 'expression', {});
break;
case 'AttachTag':
printJS(parent, 'expression', {});
break;
case 'ConstTag':
printJS(parent, 'expression', { removeParentheses: true });
break;
Expand Down
9 changes: 5 additions & 4 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,11 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
case 'RawMustacheTag':
return ['{@html ', printJS(path, print, 'expression'), '}'];
// Svelte 5 only
case 'RenderTag': {
const render = ['{@render ', printJS(path, print, 'expression'), '}'];
return render;
}
case 'RenderTag':
return ['{@render ', printJS(path, print, 'expression'), '}'];
// Svelte 5 only
case 'AttachTag':
return ['{@attach ', printJS(path, print, 'expression'), '}'];
case 'Spread':
return ['{...', printJS(path, print, 'expression'), '}'];
case 'ConstTag':
Expand Down
6 changes: 6 additions & 0 deletions src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ export interface RenderTag extends BaseNode {
arguments: BaseNode[] | null;
}

export interface AttachTag extends BaseNode {
type: 'AttachTag';
expression: IdentifierNode;
}

export type Node =
| FragmentNode
| ElementNode
Expand Down Expand Up @@ -375,6 +380,7 @@ export type Node =
| SvelteBoundary
| SvelteHTML
| RenderTag
| AttachTag
| SnippetBlock;

/**
Expand Down
2 changes: 2 additions & 0 deletions test/formatting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if (process.env.CI && hasOnly) {
}

for (const dir of dirs) {
if (dir.endsWith('.skip')) continue;

const input = readFileSync(`test/formatting/samples/${dir}/input.html`, 'utf-8').replace(
/\r?\n/g,
'\n',
Expand Down
3 changes: 3 additions & 0 deletions test/formatting/samples/attachments.skip/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<input {@attach x} />

<input {@attach (node) => { foo; bar; }} />
8 changes: 8 additions & 0 deletions test/formatting/samples/attachments.skip/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<input {@attach x} />

<input
{@attach (node) => {
foo;
bar;
}}
/>
5 changes: 5 additions & 0 deletions test/printer/samples/attachment.html.skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<input {@attach x} />
<input {@attach () => {}} />

<Component {@attach x} />
<Component {@attach () => {}} />