From 29d9f944e96ba8237dfa983ee4aba2d2c3f00752 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 20 Nov 2024 16:51:12 +0100 Subject: [PATCH] feat: support upcoming `` / `` (#474) --- CHANGELOG.md | 5 +++++ package-lock.json | 4 ++-- package.json | 2 +- src/print/helpers.ts | 6 ++++++ src/print/index.ts | 20 +++++-------------- src/print/node-helpers.ts | 16 ++++++++++++++- src/print/nodes.ts | 15 ++++++++++++++ .../printer/samples/svelte-boundary.html.skip | 6 ++++++ .../samples/svelte-html-element.html.skip | 1 + 9 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 test/printer/samples/svelte-boundary.html.skip create mode 100644 test/printer/samples/svelte-html-element.html.skip diff --git a/CHANGELOG.md b/CHANGELOG.md index bcbec3b8..4bc5f0f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # prettier-plugin-svelte changelog +## 3.3.0 + +- (feat) Svelte 5: support upcoming `` +- (feat) Svelte 5: support upcoming `` + ## 3.2.8 - (chore) provide IDE tooling a way to pass Svelte compiler path diff --git a/package-lock.json b/package-lock.json index 8c601aeb..bdd78b1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prettier-plugin-svelte", - "version": "3.2.8", + "version": "3.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "prettier-plugin-svelte", - "version": "3.2.8", + "version": "3.3.0", "license": "MIT", "devDependencies": { "@rollup/plugin-alias": "^5.1.0", diff --git a/package.json b/package.json index 8a8d7b14..ce44c7e7 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/print/helpers.ts b/src/print/helpers.ts index a33130cf..d8864a52 100644 --- a/src/print/helpers.ts +++ b/src/print/helpers.ts @@ -16,6 +16,8 @@ import { SlotNode, SlotTemplateNode, StyleNode, + SvelteBoundary, + SvelteHTML, TitleNode, WindowNode, } from './nodes'; @@ -81,6 +83,8 @@ export function getAttributeLine( | BodyNode | DocumentNode | OptionsNode + | SvelteHTML + | SvelteBoundary | SlotTemplateNode, options: ParserOptions, ) { @@ -111,6 +115,8 @@ export function printWithPrependedAttributeLine( | BodyNode | DocumentNode | OptionsNode + | SvelteHTML + | SvelteBoundary | SlotTemplateNode, options: ParserOptions, print: PrintFn, diff --git a/src/print/index.ts b/src/print/index.ts index c4b81a3c..9f3cab0e 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -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) @@ -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' || @@ -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, diff --git a/src/print/node-helpers.ts b/src/print/node-helpers.ts index 7c0e92f5..99668fc9 100644 --- a/src/print/node-helpers.ts +++ b/src/print/node-helpers.ts @@ -23,6 +23,7 @@ import { StyleDirectiveNode, ASTNode, CommentInfo, + SvelteBoundary, } from './nodes'; import { blockElements, TagName } from '../lib/elements'; import { FastPath } from 'prettier'; @@ -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) ); @@ -180,6 +185,7 @@ export function printRaw( | WindowNode | HeadNode | TitleNode + | SvelteBoundary | SlotTemplateNode, originalText: string, stripLeadingAndTrailingNewline: boolean = false, @@ -400,6 +406,10 @@ export function shouldHugStart( return true; } + if (node.type === 'SvelteBoundary') { + return false; + } + if (isBlockElement(node, options)) { return false; } @@ -434,6 +444,10 @@ export function shouldHugEnd( return true; } + if (node.type === 'SvelteBoundary') { + return false; + } + if (isBlockElement(node, options)) { return false; } diff --git a/src/print/nodes.ts b/src/print/nodes.ts index 30b7094b..c14c96b0 100644 --- a/src/print/nodes.ts +++ b/src/print/nodes.ts @@ -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; @@ -357,6 +370,8 @@ export type Node = | OptionsNode | SlotTemplateNode | ConstTagNode + | SvelteBoundary + | SvelteHTML | RenderTag | SnippetBlock; diff --git a/test/printer/samples/svelte-boundary.html.skip b/test/printer/samples/svelte-boundary.html.skip new file mode 100644 index 00000000..8ed5e2a9 --- /dev/null +++ b/test/printer/samples/svelte-boundary.html.skip @@ -0,0 +1,6 @@ + + + {#snippet failed(e)} + {e} + {/snippet} + diff --git a/test/printer/samples/svelte-html-element.html.skip b/test/printer/samples/svelte-html-element.html.skip new file mode 100644 index 00000000..63a8592e --- /dev/null +++ b/test/printer/samples/svelte-html-element.html.skip @@ -0,0 +1 @@ +