diff --git a/CHANGELOG.md b/CHANGELOG.md index 2750f54..f432b30 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 4c433b5..a647bac 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 7ee1b63..842b713 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 d60d00a..9222c2f 100644 --- a/test/printer/samples/no-tag-snippings.html +++ b/test/printer/samples/no-tag-snippings.html @@ -1,8 +1,8 @@ {@html ``}