Skip to content

Commit 3a1b27e

Browse files
authored
Pretty print variants starting with @ (#17814)
While working on another PR I noticed that some variants were re-printed in an odd way. Specifically, this PR fixes an issue where variants using the `@`-root were incorrectly printed. - `@lg` was printed as `@-lg` - `@[400px]` was printed as `@-[400px]` This is now special cased where the `-` is not inserted for `@`-root variants. ## Test plan 1. Added a test to ensure the `@`-root variants are printed correctly.
1 parent af1d4aa commit 3a1b27e

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Replace `_` with `.` in theme suggestions for `@utility` if surrounded by digits ([#17743](https://github.com/tailwindlabs/tailwindcss/pull/17743))
2121
- Upgrade: Bump all Tailwind CSS related dependencies during upgrade ([#17763](https://github.com/tailwindlabs/tailwindcss/pull/17763))
2222
- PostCSS: Ensure that errors in stylesheet dependencies are recoverable ([#17754](https://github.com/tailwindlabs/tailwindcss/pull/17754))
23+
- Upgrade: Correctly print variants starting with `@` ([#17814](https://github.com/tailwindlabs/tailwindcss/pull/17814))
2324

2425
## [4.1.4] - 2025-04-14
2526

packages/@tailwindcss-upgrade/src/codemods/template/candidates.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ const variants = [
166166
['[&:is(_p_)]:', '[p]:'],
167167
['has-[&:is(p)]:', 'has-[p]:'],
168168
['has-[&:is(_p_)]:', 'has-[p]:'],
169+
170+
// Handle special `@` variants. These shouldn't be printed as `@-`
171+
['@xl:', '@xl:'],
172+
['@[123px]:', '@[123px]:'],
169173
]
170174

171175
let combinations: [string, string][] = []

packages/@tailwindcss-upgrade/src/codemods/template/candidates.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,18 @@ function printVariant(variant: Variant) {
105105
// Handle functional variants
106106
if (variant.kind === 'functional') {
107107
base += variant.root
108+
// `@` is a special case for functional variants. We want to print: `@lg`
109+
// instead of `@-lg`
110+
let hasDash = variant.root !== '@'
108111
if (variant.value) {
109112
if (variant.value.kind === 'arbitrary') {
110113
let isVarValue = isVar(variant.value.value)
111114
let value = isVarValue ? variant.value.value.slice(4, -1) : variant.value.value
112115
let [open, close] = isVarValue ? ['(', ')'] : ['[', ']']
113116

114-
base += `-${open}${printArbitraryValue(value)}${close}`
117+
base += `${hasDash ? '-' : ''}${open}${printArbitraryValue(value)}${close}`
115118
} else if (variant.value.kind === 'named') {
116-
base += `-${variant.value.value}`
119+
base += `${hasDash ? '-' : ''}${variant.value.value}`
117120
}
118121
}
119122
}

0 commit comments

Comments
 (0)