Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: format JSON script tags #424

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading