Skip to content

Commit 29d9f94

Browse files
authored
feat: support upcoming <svelte:boundary> / <svelte:html> (#474)
1 parent c496666 commit 29d9f94

9 files changed

+56
-19
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# prettier-plugin-svelte changelog
22

3+
## 3.3.0
4+
5+
- (feat) Svelte 5: support upcoming `<svelte:boundary>`
6+
- (feat) Svelte 5: support upcoming `<svelte:html>`
7+
38
## 3.2.8
49

510
- (chore) provide IDE tooling a way to pass Svelte compiler path

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prettier-plugin-svelte",
3-
"version": "3.2.8",
3+
"version": "3.3.0",
44
"description": "Svelte plugin for prettier",
55
"main": "plugin.js",
66
"files": [

src/print/helpers.ts

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
SlotNode,
1717
SlotTemplateNode,
1818
StyleNode,
19+
SvelteBoundary,
20+
SvelteHTML,
1921
TitleNode,
2022
WindowNode,
2123
} from './nodes';
@@ -81,6 +83,8 @@ export function getAttributeLine(
8183
| BodyNode
8284
| DocumentNode
8385
| OptionsNode
86+
| SvelteHTML
87+
| SvelteBoundary
8488
| SlotTemplateNode,
8589
options: ParserOptions,
8690
) {
@@ -111,6 +115,8 @@ export function printWithPrependedAttributeLine(
111115
| BodyNode
112116
| DocumentNode
113117
| OptionsNode
118+
| SvelteHTML
119+
| SvelteBoundary
114120
| SlotTemplateNode,
115121
options: ParserOptions,
116122
print: PrintFn,

src/print/index.ts

+5-15
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
202202
case 'SlotTemplate':
203203
case 'Window':
204204
case 'Head':
205+
// Svelte 5 only
206+
case 'SvelteBoundary':
205207
case 'Title': {
206208
const isSupportedLanguage = !(
207209
node.name === 'template' && !isNodeSupportedLanguage(node)
@@ -217,6 +219,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
217219
node.type === 'InlineComponent' ||
218220
node.type === 'Slot' ||
219221
node.type === 'SlotTemplate' ||
222+
node.type === 'SvelteBoundary' ||
220223
node.type === 'Title') &&
221224
didSelfClose) ||
222225
node.type === 'Window' ||
@@ -400,21 +403,8 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
400403
// else fall through to Body
401404
case 'Body':
402405
case 'Document':
403-
return group([
404-
'<',
405-
node.name,
406-
indent(
407-
group([
408-
...path.map(
409-
printWithPrependedAttributeLine(node, options, print),
410-
'attributes',
411-
),
412-
bracketSameLine ? '' : dedent(line),
413-
]),
414-
),
415-
...[bracketSameLine ? ' ' : '', '/>'],
416-
]);
417-
case 'Document':
406+
// Svelte 5 only
407+
case 'SvelteHTML':
418408
return group([
419409
'<',
420410
node.name,

src/print/node-helpers.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
StyleDirectiveNode,
2424
ASTNode,
2525
CommentInfo,
26+
SvelteBoundary,
2627
} from './nodes';
2728
import { blockElements, TagName } from '../lib/elements';
2829
import { FastPath } from 'prettier';
@@ -31,7 +32,11 @@ import { ParserOptions, isBracketSameLine } from '../options';
3132

3233
const unsupportedLanguages = ['coffee', 'coffeescript', 'styl', 'stylus', 'sass'];
3334

34-
export function isInlineElement(path: FastPath, options: ParserOptions, node: Node) {
35+
export function isInlineElement(
36+
path: FastPath,
37+
options: ParserOptions,
38+
node: Node,
39+
): node is ElementNode {
3540
return (
3641
node && node.type === 'Element' && !isBlockElement(node, options) && !isPreTagContent(path)
3742
);
@@ -180,6 +185,7 @@ export function printRaw(
180185
| WindowNode
181186
| HeadNode
182187
| TitleNode
188+
| SvelteBoundary
183189
| SlotTemplateNode,
184190
originalText: string,
185191
stripLeadingAndTrailingNewline: boolean = false,
@@ -400,6 +406,10 @@ export function shouldHugStart(
400406
return true;
401407
}
402408

409+
if (node.type === 'SvelteBoundary') {
410+
return false;
411+
}
412+
403413
if (isBlockElement(node, options)) {
404414
return false;
405415
}
@@ -434,6 +444,10 @@ export function shouldHugEnd(
434444
return true;
435445
}
436446

447+
if (node.type === 'SvelteBoundary') {
448+
return false;
449+
}
450+
437451
if (isBlockElement(node, options)) {
438452
return false;
439453
}

src/print/nodes.ts

+15
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ export interface BodyNode extends BaseNode {
268268
attributes: Node[];
269269
}
270270

271+
export interface SvelteHTML extends BaseNode {
272+
type: 'SvelteHTML';
273+
name: string;
274+
attributes: Node[];
275+
}
276+
277+
export interface SvelteBoundary extends BaseNode {
278+
type: 'SvelteBoundary';
279+
name: string;
280+
attributes: Node[];
281+
children: Node[];
282+
}
283+
271284
export interface DocumentNode extends BaseNode {
272285
type: 'Document';
273286
name: string;
@@ -357,6 +370,8 @@ export type Node =
357370
| OptionsNode
358371
| SlotTemplateNode
359372
| ConstTagNode
373+
| SvelteBoundary
374+
| SvelteHTML
360375
| RenderTag
361376
| SnippetBlock;
362377

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<svelte:boundary onerror={handle}>
2+
<ComponentThatFails />
3+
{#snippet failed(e)}
4+
{e}
5+
{/snippet}
6+
</svelte:boundary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<svelte:html lang={language} />

0 commit comments

Comments
 (0)