Skip to content

Commit 1884f22

Browse files
authored
feat: format JSON script tags (#424)
Also silences a (harmless) error log in the tests closes #421 closes #419
1 parent 1c4bcfe commit 1884f22

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# prettier-plugin-svelte changelog
22

3+
## 3.2.0 (Unreleased)
4+
5+
- (feat) format JSON script tags
6+
37
## 3.1.2
48

59
- (fix) handle `>` tags in attributes

src/embed.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getLeadingComment,
1212
isIgnoreDirective,
1313
isInsideQuotedAttribute,
14+
isJSON,
1415
isLess,
1516
isNodeSupportedLanguage,
1617
isPugTemplate,
@@ -177,7 +178,7 @@ export function embed(path: FastPath, _options: Options) {
177178

178179
const embedType = (
179180
tag: 'script' | 'style' | 'template',
180-
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug',
181+
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug' | 'json',
181182
isTopLevel: boolean,
182183
) => {
183184
return async (
@@ -203,7 +204,7 @@ export function embed(path: FastPath, _options: Options) {
203204
// the user could have set the default language. babel-ts will format things a little
204205
// bit different though, especially preserving parentheses around dot notation which
205206
// fixes https://github.com/sveltejs/prettier-plugin-svelte/issues/218
206-
isTypeScript(node) ? 'typescript' : 'babel-ts',
207+
isTypeScript(node) ? 'typescript' : isJSON(node) ? 'json' : 'babel-ts',
207208
isTopLevel,
208209
);
209210
const embedStyle = (isTopLevel: boolean) =>
@@ -260,7 +261,7 @@ function getSnippedContent(node: Node) {
260261

261262
async function formatBodyContent(
262263
content: string,
263-
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug',
264+
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug' | 'json',
264265
textToDoc: (text: string, options: object) => Promise<Doc>,
265266
options: ParserOptions & { pugTabWidth?: number },
266267
) {

src/print/node-helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ export function isTypeScript(node: Node) {
268268
return ['typescript', 'ts'].includes(lang);
269269
}
270270

271+
export function isJSON(node: Node) {
272+
const lang = getLangAttribute(node) || '';
273+
// https://github.com/prettier/prettier/pull/6293
274+
return lang.endsWith('json') || lang.endsWith('importmap');
275+
}
276+
271277
export function isLess(node: Node) {
272278
const lang = getLangAttribute(node) || '';
273279
return ['less'].includes(lang);

test/printer/samples/no-tag-snippings.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<svelte:head>
22
<script type="application/ld+json">
33
{
4-
"@context": "https://schema.org",
5-
"@type": "etc..."
4+
"@context": "https://schema.org",
5+
"@type": "etc..."
66
}
77
</script>
88
{@html `<style>${getCssText()}</style>`}

0 commit comments

Comments
 (0)