Skip to content
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

feat: support upcoming <svelte:boundary> / <svelte:html> #474

Merged
merged 1 commit into from
Nov 20, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# prettier-plugin-svelte changelog

## 3.3.0

- (feat) Svelte 5: support upcoming `<svelte:boundary>`
- (feat) Svelte 5: support upcoming `<svelte:html>`

## 3.2.8

- (chore) provide IDE tooling a way to pass Svelte compiler path
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.2.8",
"version": "3.3.0",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"files": [
Expand Down
6 changes: 6 additions & 0 deletions src/print/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
SlotNode,
SlotTemplateNode,
StyleNode,
SvelteBoundary,
SvelteHTML,
TitleNode,
WindowNode,
} from './nodes';
Expand Down Expand Up @@ -81,6 +83,8 @@ export function getAttributeLine(
| BodyNode
| DocumentNode
| OptionsNode
| SvelteHTML
| SvelteBoundary
| SlotTemplateNode,
options: ParserOptions,
) {
Expand Down Expand Up @@ -111,6 +115,8 @@ export function printWithPrependedAttributeLine(
| BodyNode
| DocumentNode
| OptionsNode
| SvelteHTML
| SvelteBoundary
| SlotTemplateNode,
options: ParserOptions,
print: PrintFn,
Expand Down
20 changes: 5 additions & 15 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
case 'SlotTemplate':
case 'Window':
case 'Head':
// Svelte 5 only
case 'SvelteBoundary':
case 'Title': {
const isSupportedLanguage = !(
node.name === 'template' && !isNodeSupportedLanguage(node)
Expand All @@ -217,6 +219,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
node.type === 'InlineComponent' ||
node.type === 'Slot' ||
node.type === 'SlotTemplate' ||
node.type === 'SvelteBoundary' ||
node.type === 'Title') &&
didSelfClose) ||
node.type === 'Window' ||
Expand Down Expand Up @@ -400,21 +403,8 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
// else fall through to Body
case 'Body':
case 'Document':
return group([
'<',
node.name,
indent(
group([
...path.map(
printWithPrependedAttributeLine(node, options, print),
'attributes',
),
bracketSameLine ? '' : dedent(line),
]),
),
...[bracketSameLine ? ' ' : '', '/>'],
]);
case 'Document':
// Svelte 5 only
case 'SvelteHTML':
return group([
'<',
node.name,
Expand Down
16 changes: 15 additions & 1 deletion src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
StyleDirectiveNode,
ASTNode,
CommentInfo,
SvelteBoundary,
} from './nodes';
import { blockElements, TagName } from '../lib/elements';
import { FastPath } from 'prettier';
Expand All @@ -31,7 +32,11 @@ import { ParserOptions, isBracketSameLine } from '../options';

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

export function isInlineElement(path: FastPath, options: ParserOptions, node: Node) {
export function isInlineElement(
path: FastPath,
options: ParserOptions,
node: Node,
): node is ElementNode {
return (
node && node.type === 'Element' && !isBlockElement(node, options) && !isPreTagContent(path)
);
Expand Down Expand Up @@ -180,6 +185,7 @@ export function printRaw(
| WindowNode
| HeadNode
| TitleNode
| SvelteBoundary
| SlotTemplateNode,
originalText: string,
stripLeadingAndTrailingNewline: boolean = false,
Expand Down Expand Up @@ -400,6 +406,10 @@ export function shouldHugStart(
return true;
}

if (node.type === 'SvelteBoundary') {
return false;
}

if (isBlockElement(node, options)) {
return false;
}
Expand Down Expand Up @@ -434,6 +444,10 @@ export function shouldHugEnd(
return true;
}

if (node.type === 'SvelteBoundary') {
return false;
}

if (isBlockElement(node, options)) {
return false;
}
Expand Down
15 changes: 15 additions & 0 deletions src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ export interface BodyNode extends BaseNode {
attributes: Node[];
}

export interface SvelteHTML extends BaseNode {
type: 'SvelteHTML';
name: string;
attributes: Node[];
}

export interface SvelteBoundary extends BaseNode {
type: 'SvelteBoundary';
name: string;
attributes: Node[];
children: Node[];
}

export interface DocumentNode extends BaseNode {
type: 'Document';
name: string;
Expand Down Expand Up @@ -357,6 +370,8 @@ export type Node =
| OptionsNode
| SlotTemplateNode
| ConstTagNode
| SvelteBoundary
| SvelteHTML
| RenderTag
| SnippetBlock;

Expand Down
6 changes: 6 additions & 0 deletions test/printer/samples/svelte-boundary.html.skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<svelte:boundary onerror={handle}>
<ComponentThatFails />
{#snippet failed(e)}
{e}
{/snippet}
</svelte:boundary>
1 change: 1 addition & 0 deletions test/printer/samples/svelte-html-element.html.skip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:html lang={language} />
Loading