From 1884f2250b9d8458e395f153744f74b6117f0bb9 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:12:33 +0100 Subject: [PATCH 01/12] feat: format JSON script tags (#424) Also silences a (harmless) error log in the tests closes #421 closes #419 --- CHANGELOG.md | 4 ++++ src/embed.ts | 7 ++++--- src/print/node-helpers.ts | 6 ++++++ test/printer/samples/no-tag-snippings.html | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2750f541..f432b304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # prettier-plugin-svelte changelog +## 3.2.0 (Unreleased) + +- (feat) format JSON script tags + ## 3.1.2 - (fix) handle `>` tags in attributes diff --git a/src/embed.ts b/src/embed.ts index 4c433b5e..a647bac3 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -11,6 +11,7 @@ import { getLeadingComment, isIgnoreDirective, isInsideQuotedAttribute, + isJSON, isLess, isNodeSupportedLanguage, isPugTemplate, @@ -177,7 +178,7 @@ export function embed(path: FastPath, _options: Options) { const embedType = ( tag: 'script' | 'style' | 'template', - parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug', + parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug' | 'json', isTopLevel: boolean, ) => { return async ( @@ -203,7 +204,7 @@ export function embed(path: FastPath, _options: Options) { // the user could have set the default language. babel-ts will format things a little // bit different though, especially preserving parentheses around dot notation which // fixes https://github.com/sveltejs/prettier-plugin-svelte/issues/218 - isTypeScript(node) ? 'typescript' : 'babel-ts', + isTypeScript(node) ? 'typescript' : isJSON(node) ? 'json' : 'babel-ts', isTopLevel, ); const embedStyle = (isTopLevel: boolean) => @@ -260,7 +261,7 @@ function getSnippedContent(node: Node) { async function formatBodyContent( content: string, - parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug', + parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug' | 'json', textToDoc: (text: string, options: object) => Promise, options: ParserOptions & { pugTabWidth?: number }, ) { diff --git a/src/print/node-helpers.ts b/src/print/node-helpers.ts index 7ee1b632..842b713e 100644 --- a/src/print/node-helpers.ts +++ b/src/print/node-helpers.ts @@ -268,6 +268,12 @@ export function isTypeScript(node: Node) { return ['typescript', 'ts'].includes(lang); } +export function isJSON(node: Node) { + const lang = getLangAttribute(node) || ''; + // https://github.com/prettier/prettier/pull/6293 + return lang.endsWith('json') || lang.endsWith('importmap'); +} + export function isLess(node: Node) { const lang = getLangAttribute(node) || ''; return ['less'].includes(lang); diff --git a/test/printer/samples/no-tag-snippings.html b/test/printer/samples/no-tag-snippings.html index d60d00a1..9222c2fa 100644 --- a/test/printer/samples/no-tag-snippings.html +++ b/test/printer/samples/no-tag-snippings.html @@ -1,8 +1,8 @@ {@html ``} From cc856a0e5dedd56b7edb0aa1be18adaf385a247a Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:50:53 +0100 Subject: [PATCH 02/12] fix: don't duplicate comments of nested script/style tags (#425) fixes #400 --- CHANGELOG.md | 1 + src/embed.ts | 3 ++- src/print/index.ts | 1 - .../samples/script-style-inside-element-comment.html | 10 ++++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 test/printer/samples/script-style-inside-element-comment.html diff --git a/CHANGELOG.md b/CHANGELOG.md index f432b304..ef0f13dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 3.2.0 (Unreleased) - (feat) format JSON script tags +- (fix) don't duplicate comments of nested script/style tags ## 3.1.2 diff --git a/src/embed.ts b/src/embed.ts index a647bac3..e3aab56b 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -366,7 +366,8 @@ async function embedTag( // the new position. return [...comments, result, hardline]; } else { - return comments.length ? [...comments, result] : result; + // Only comments at the top level get the special "move comment" treatment. + return isTopLevel && comments.length ? [...comments, result] : result; } } diff --git a/src/print/index.ts b/src/print/index.ts index 394acc58..f525b396 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -46,7 +46,6 @@ import { import { ASTNode, AttributeNode, - CommentInfo, CommentNode, IfBlockNode, Node, diff --git a/test/printer/samples/script-style-inside-element-comment.html b/test/printer/samples/script-style-inside-element-comment.html new file mode 100644 index 00000000..2fd3e3c2 --- /dev/null +++ b/test/printer/samples/script-style-inside-element-comment.html @@ -0,0 +1,10 @@ +
+ + +
+ + + + From 99c885e5d695bd59f26f156e66fd3f333f181e31 Mon Sep 17 00:00:00 2001 From: Curran Kelleher <68416+curran@users.noreply.github.com> Date: Mon, 5 Feb 2024 03:43:35 -0500 Subject: [PATCH 03/12] chore: remove Buffer usage for browser environments (#423) Closes #418 --- src/base64-string.ts | 20 ++++++++++++++++++++ src/embed.ts | 3 ++- src/lib/snipTagContent.ts | 6 ++++-- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/base64-string.ts diff --git a/src/base64-string.ts b/src/base64-string.ts new file mode 100644 index 00000000..887084f1 --- /dev/null +++ b/src/base64-string.ts @@ -0,0 +1,20 @@ +// Base64 string encoding and decoding module. +// Uses Buffer for Node.js and btoa/atob for browser environments. +// We use TextEncoder/TextDecoder for browser environments because +// they can handle non-ASCII characters, unlike btoa/atob. + +export const stringToBase64 = + typeof Buffer !== 'undefined' + ? (str: string) => Buffer.from(str).toString('base64') + : (str: string) => + btoa( + new TextEncoder() + .encode(str) + .reduce((acc, byte) => acc + String.fromCharCode(byte), ''), + ); + +export const base64ToString = + typeof Buffer !== 'undefined' + ? (str: string) => Buffer.from(str, 'base64').toString() + : (str: string) => + new TextDecoder().decode(Uint8Array.from(atob(str), (c) => c.charCodeAt(0))); diff --git a/src/embed.ts b/src/embed.ts index e3aab56b..c04a953b 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -21,6 +21,7 @@ import { } from './print/node-helpers'; import { CommentNode, ElementNode, Node, ScriptNode, StyleNode } from './print/nodes'; import { extractAttributes } from './lib/extractAttributes'; +import { base64ToString } from './base64-string'; const { builders: { group, hardline, softline, indent, dedent, literalline }, @@ -253,7 +254,7 @@ function getSnippedContent(node: Node) { const encodedContent = getAttributeTextValue(snippedTagContentAttribute, node); if (encodedContent) { - return Buffer.from(encodedContent, 'base64').toString('utf-8'); + return base64ToString(encodedContent); } else { return ''; } diff --git a/src/lib/snipTagContent.ts b/src/lib/snipTagContent.ts index 89337266..cb02d8d3 100644 --- a/src/lib/snipTagContent.ts +++ b/src/lib/snipTagContent.ts @@ -1,3 +1,5 @@ +import { base64ToString, stringToBase64 } from '../base64-string'; + export const snippedTagContentAttribute = '✂prettier:content✂'; const scriptRegex = @@ -42,7 +44,7 @@ export function snipScriptAndStyleTagContent(source: string): string { if (match.startsWith(' + + + From b120c4de5faa6ca6fa0c6867d6048e1dd1b958ca Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 12 Apr 2024 16:34:18 +0200 Subject: [PATCH 12/12] chore: release 3.2.3 --- CHANGELOG.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63aeba45..ba77dbea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # prettier-plugin-svelte changelog +## 3.2.3 + +- (fix) don't force-self-close `` tags + ## 3.2.2 - (fix) handle updated `@render` tag AST shape diff --git a/package-lock.json b/package-lock.json index b7a87256..dec8fe93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prettier-plugin-svelte", - "version": "3.2.2", + "version": "3.2.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "prettier-plugin-svelte", - "version": "3.2.2", + "version": "3.2.3", "license": "MIT", "devDependencies": { "@rollup/plugin-alias": "^5.1.0", diff --git a/package.json b/package.json index 46ec0a87..2cb29577 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prettier-plugin-svelte", - "version": "3.2.2", + "version": "3.2.3", "description": "Svelte plugin for prettier", "main": "plugin.js", "files": [