Skip to content

Commit 9481dd0

Browse files
mcousthecrypticace
andauthored
Fix erroneous template string whitespace formatting in Svelte (#332)
* Fix erroneous template string whitespace formatting in Svelte * Update test * Update changelog --------- Co-authored-by: Jordan Pittman <[email protected]>
1 parent a9201df commit 9481dd0

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Add support for `@zackad/prettier-plugin-twig` ([#308](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/308))
1111
- Dropped support for `@zackad/prettier-plugin-twig-melody` ([#308](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/308))
1212
- Updated Prettier options types ([#325](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/325))
13+
- Don't remove whitespace inside template literals in Svelte ([#332](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/332))
1314

1415
## [0.6.9] - 2024-11-19
1516

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ function sortTemplateLiteral(
537537
// And does not end with a space
538538
ignoreLast: i < node.expressions.length && !/\s$/.test(quasi.value.raw),
539539

540-
collapseWhitespace: {
540+
collapseWhitespace: collapseWhitespace && {
541541
start: collapseWhitespace && collapseWhitespace.start && i === 0,
542542
end:
543543
collapseWhitespace &&
@@ -553,7 +553,7 @@ function sortTemplateLiteral(
553553
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.cooked),
554554
ignoreLast:
555555
i < node.expressions.length && !/\s$/.test(quasi.value.cooked),
556-
collapseWhitespace: {
556+
collapseWhitespace: collapseWhitespace && {
557557
start: collapseWhitespace && collapseWhitespace.start && i === 0,
558558
end:
559559
collapseWhitespace &&

tests/plugins.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@ import Custom from '../components/Custom.astro'
416416
`<div class="sm:p-0 p-0 {someVar}sm:block md:inline flex" />`,
417417
`<div class="p-0 sm:p-0 {someVar}sm:block flex md:inline" />`,
418418
],
419-
['<div class={`sm:p-0\np-0`} />', '<div class={`p-0 sm:p-0`} />'],
420419
t`{#await promise()} <div class="${yes}" /> {:then} <div class="${yes}" /> {/await}`,
421420
t`{#await promise() then} <div class="${yes}" /> {/await}`,
422421

@@ -436,6 +435,15 @@ import Custom from '../components/Custom.astro'
436435

437436
// Escapes
438437
t`<div class={"before:content-['\\\\2248']"}></div>`,
438+
439+
// Preserve whitespace in template strings
440+
// This test has lots of whitespace to ensure that the Svelte
441+
// parser doesn't produce invalid syntax as output since it breaks
442+
// when changing the length of the text.
443+
[
444+
`<div\n class={\`underline \n flex\`}></div>`,
445+
`<div\n class={\`flex \n underline\`}\n></div>`,
446+
],
439447
],
440448
},
441449
},

0 commit comments

Comments
 (0)