Skip to content

Commit 9b3e8c8

Browse files
Fix template literal whitespace collapse for adjacent quasis (#427)
* Fix template literal whitespace collapse for adjacent quasis * Update changelog --------- Co-authored-by: Jordan Pittman <jordan@cryptica.me>
1 parent f4ca38a commit 9b3e8c8

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Collapse whitespace in template literals with adjacent quasis ([#427](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/427))
1113

1214
## [0.7.2] - 2025-12-01
1315

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ function canCollapseWhitespaceIn(path: Path<import('@babel/types').Node, any>) {
630630
let nodeEnd = entry.node.end ?? null
631631

632632
for (let quasi of entry.parent.quasis) {
633-
let quasiStart = quasi.end ?? null
633+
let quasiStart = quasi.start ?? null
634634
let quasiEnd = quasi.end ?? null
635635

636636
if (nodeStart !== null && quasiEnd !== null && nodeStart - quasiEnd <= 2) {

tests/format.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ describe('whitespace', () => {
9696
expect(result).toEqual(";<div className={`header${isExtendable ? ' header-extendable' : ''}`} />")
9797
})
9898

99+
test('whitespace is not trimmed before template literal quasis without leading space', async ({ expect }) => {
100+
let result = await format(";<div className={`${foo ? 'sm:p-0 p-0 ' : ''}header`}></div>", {
101+
parser: 'babel',
102+
})
103+
104+
expect(result).toEqual(";<div className={`${foo ? 'p-0 sm:p-0 ' : ''}header`}></div>")
105+
})
106+
99107
test('duplicate classes are dropped', async ({ expect }) => {
100108
let result = await format('<div class="underline line-through underline flex"></div>')
101109

0 commit comments

Comments
 (0)