Skip to content

Commit

Permalink
feat: add experimental support for Svelte 5
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Nov 13, 2023
1 parent 9060efd commit dd66514
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# prettier-plugin-svelte changelog

## 3.1.0

- (feat) add experimental support for Svelte 5
- (feat) support `#snippet` and `@render`

## 3.0.3

- (fix) handle static `tag` attributes on `<svelte:element>`
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-svelte",
"version": "3.0.3",
"version": "3.1.0",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"files": [
Expand Down Expand Up @@ -51,6 +51,6 @@
},
"peerDependencies": {
"prettier": "^3.0.0",
"svelte": "^3.2.0 || ^4.0.0-next.0"
"svelte": "^3.2.0 || ^4.0.0-next.0 || ^5.0.0-next.0"
}
}
24 changes: 24 additions & 0 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ export function embed(path: FastPath, _options: Options) {
printSvelteBlockJS('expression');
printSvelteBlockJS('key');
break;
case 'SnippetBlock':
// We merge the two parts into one expression, which future-proofs this for template TS support
if (node === parent.expression) {
parent.expression.end =
options.originalText.indexOf(
')',
parent.context?.end ?? parent.expression.end,
) + 1;
parent.context = null;
printSvelteBlockJS('expression');
}
break;
case 'Element':
printJS(parent, options.svelteStrictMode ?? false, false, false, 'tag');
break;
Expand All @@ -106,6 +118,18 @@ export function embed(path: FastPath, _options: Options) {
case 'ConstTag':
printJS(parent, false, false, true, 'expression');
break;
case 'RenderTag':
// We merge the two parts into one expression, which future-proofs this for template TS support
if (node === parent.expression) {
parent.expression.end =
options.originalText.indexOf(
')',
parent.argument?.end ?? parent.expression.end,
) + 1;
parent.argument = null;
printJS(parent, false, false, false, 'expression');
}
break;
case 'EventHandler':
case 'Binding':
case 'Class':
Expand Down
11 changes: 11 additions & 0 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
case 'PendingBlock':
case 'CatchBlock':
return printSvelteBlockChildren(path, print, options);
// Svelte 5 only
case 'SnippetBlock': {
const snippet = ['{#snippet ', printJS(path, print, 'expression')];
snippet.push('}', printSvelteBlockChildren(path, print, options), '{/snippet}');
return snippet;
}
case 'EventHandler':
return [
'on:',
Expand Down Expand Up @@ -718,6 +724,11 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
return ['animate:', node.name, node.expression ? ['=', ...printJsExpression()] : ''];
case 'RawMustacheTag':
return ['{@html ', printJS(path, print, 'expression'), '}'];
// Svelte 5 only
case 'RenderTag': {
const render = ['{@render ', printJS(path, print, 'expression'), '}'];
return render;
}
case 'Spread':
return ['{...', printJS(path, print, 'expression'), '}'];
case 'ConstTag':
Expand Down
1 change: 1 addition & 0 deletions src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function isSvelteBlock(
| ThenBlockNode {
return [
'IfBlock',
'SnippetBlock',
'AwaitBlock',
'CatchBlock',
'EachBlock',
Expand Down
17 changes: 16 additions & 1 deletion src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,19 @@ export interface CommentInfo {
emptyLineAfter: boolean;
}

export interface SnippetBlock extends BaseNode {
type: 'SnippetBlock';
expression: IdentifierNode;
context: null | any;
children: Node[];
}

export interface RenderTag extends BaseNode {
type: 'RenderTag';
expression: IdentifierNode;
argument: null | any;
}

export type Node =
| FragmentNode
| ElementNode
Expand Down Expand Up @@ -335,7 +348,9 @@ export type Node =
| DocumentNode
| OptionsNode
| SlotTemplateNode
| ConstTagNode;
| ConstTagNode
| RenderTag
| SnippetBlock;

/**
* The Svelte AST root node
Expand Down
2 changes: 1 addition & 1 deletion test/formatting/samples/move-options-to-top/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p>hi</p>

<svelte:options tag="my-custom-element" />
<svelte:options immutable={true} />

<p>some stuff</p>

Expand Down
2 changes: 1 addition & 1 deletion test/formatting/samples/move-options-to-top/output.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<svelte:options tag="my-custom-element" />
<svelte:options immutable={true} />

<script></script>

Expand Down
10 changes: 10 additions & 0 deletions test/printer/samples/snippet.html.skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{#snippet foo()}
<p>foo</p>
{/snippet}

{#snippet bar({ a, b })}
<p>bar</p>
{/snippet}

{@render foo()}
{@render bar(x)}
2 changes: 1 addition & 1 deletion test/printer/samples/svelte-options-element.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<svelte:options tag="my-custom-element" />
<svelte:options immutable={true} />

0 comments on commit dd66514

Please sign in to comment.