Skip to content

Commit 8a9094f

Browse files
Improve handling of whitespace and duplicate class removal (#276)
* Add tests * Be more careful about whitespace removal in concat expressions * Detect liquid concat expressions in newer ASTs * Make sure string splicing handles changing string length * Add option to preserve duplicate classes This is important when using templating languages * Disable whitespace removal in Svelte We’d have to modify the start/end locations of many nodes in the AST. Technically, only AST nodes appearing _after_ the string on the same line but that might actually be sibling nodes and ancestor nodes. It’s a better option for now to disable it. * Disable whitespace removal in Liquid * Disable duplicate removal in Liquid and Svelte * Update changelog * Update readme * Update CHANGELOG.md * Update README.md --------- Co-authored-by: Jonathan Reinink <[email protected]>
1 parent afbc487 commit 8a9094f

File tree

9 files changed

+389
-95
lines changed

9 files changed

+389
-95
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Added
11+
12+
- Add new `tailwindPreserveDuplicates` option to disable removal of duplicate classes ([#276](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/276))
13+
14+
### Fixed
15+
16+
- Improve handling of whitespace removal when concatenating strings ([#276](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/276))
17+
- Fix a bug where Angular expressions may produce invalid code after sorting ([#276](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/276))
18+
- Disabled whitespace and duplicate class removal for Liquid and Svelte ([#276](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/276))
1119

1220
## [0.6.0] - 2024-05-30
1321

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,57 @@ Once added, tag your strings with the function and the plugin will sort them:
141141
const mySortedClasses = tw`bg-white p-4 dark:bg-black`
142142
```
143143

144+
## Preserving whitespace
145+
146+
This plugin automatically removes unnecessary whitespace between classes to ensure consistent formatting. If you prefer to preserve whitespace, you can use the `tailwindPreserveWhitespace` option:
147+
148+
```json5
149+
// .prettierrc
150+
{
151+
"tailwindPreserveWhitespace": true,
152+
}
153+
```
154+
155+
With this configuration, any whitespace surrounding classes will be preserved:
156+
157+
```jsx
158+
import clsx from 'clsx'
159+
160+
function MyButton({ isHovering, children }) {
161+
return (
162+
<button className=" rounded bg-blue-500 px-4 py-2 text-base text-white ">
163+
{children}
164+
</button>
165+
)
166+
}
167+
```
168+
169+
## Preserving duplicate classes
170+
171+
This plugin automatically removes duplicate classes from your class lists. However, this can cause issues in some templating languages, like Fluid or Blade, where we can't distinguish between classes and the templating syntax.
172+
173+
If removing duplicate classes is causing issues in your project, you can use the `tailwindPreserveDuplicates` option to disable this behavior:
174+
175+
```json5
176+
// .prettierrc
177+
{
178+
"tailwindPreserveDuplicates": true,
179+
}
180+
```
181+
182+
With this configuration, anything we perceive as duplicate classes will be preserved:
183+
184+
```html
185+
<div
186+
class="
187+
{f:if(condition: isCompact, then: 'grid-cols-3', else: 'grid-cols-5')}
188+
{f:if(condition: isDark, then: 'bg-black/50', else: 'bg-white/50')}
189+
grid gap-4 p-4
190+
"
191+
>
192+
</div>
193+
```
194+
144195
## Compatibility with other Prettier plugins
145196

146197
This plugin uses Prettier APIs that can only be used by one plugin at a time, making it incompatible with other Prettier plugins implemented the same way. To solve this we've added explicit per-plugin workarounds that enable compatibility with the following Prettier plugins:

0 commit comments

Comments
 (0)