Skip to content

Commit

Permalink
feat: format JSON script tags (#424)
Browse files Browse the repository at this point in the history
Also silences a (harmless) error log in the tests
closes #421
closes #419
  • Loading branch information
dummdidumm committed Jan 29, 2024
1 parent 1c4bcfe commit 1884f22
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getLeadingComment,
isIgnoreDirective,
isInsideQuotedAttribute,
isJSON,
isLess,
isNodeSupportedLanguage,
isPugTemplate,
Expand Down Expand Up @@ -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 (
Expand All @@ -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) =>
Expand Down Expand Up @@ -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<Doc>,
options: ParserOptions & { pugTabWidth?: number },
) {
Expand Down
6 changes: 6 additions & 0 deletions src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/printer/samples/no-tag-snippings.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<svelte:head>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "etc..."
"@context": "https://schema.org",
"@type": "etc..."
}
</script>
{@html `<style>${getCssText()}</style>`}
Expand Down

0 comments on commit 1884f22

Please sign in to comment.