From bf2dad5063f0fffef5146f84a8f8b8538d86b62b Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 29 Jan 2024 11:07:00 +0100 Subject: [PATCH 1/2] feat: format JSON script tags silences a (harmless) error log in the tests --- CHANGELOG.md | 4 ++++ src/embed.ts | 7 ++++--- src/print/node-helpers.ts | 5 +++++ test/printer/samples/no-tag-snippings.html | 4 ++-- 4 files changed, 15 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..2c8d88e6 100644 --- a/src/print/node-helpers.ts +++ b/src/print/node-helpers.ts @@ -268,6 +268,11 @@ export function isTypeScript(node: Node) { return ['typescript', 'ts'].includes(lang); } +export function isJSON(node: Node) { + const lang = getLangAttribute(node) || ''; + return ['application/json', 'application/ld+json'].includes(lang); +} + 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 a2bb2030f787fee0af1e051443435ca90f78600d Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 29 Jan 2024 11:10:24 +0100 Subject: [PATCH 2/2] handle more cases --- src/print/node-helpers.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/print/node-helpers.ts b/src/print/node-helpers.ts index 2c8d88e6..842b713e 100644 --- a/src/print/node-helpers.ts +++ b/src/print/node-helpers.ts @@ -270,7 +270,8 @@ export function isTypeScript(node: Node) { export function isJSON(node: Node) { const lang = getLangAttribute(node) || ''; - return ['application/json', 'application/ld+json'].includes(lang); + // https://github.com/prettier/prettier/pull/6293 + return lang.endsWith('json') || lang.endsWith('importmap'); } export function isLess(node: Node) {