Skip to content

Commit b3fde17

Browse files
authored
Migrate negative arbitrary values to negative bare values (#18212)
This PR adds some improvements to the upgrade tool where it can now also migrate negative arbitrary values to negative bare values. We already had support for the positive version of this: ```diff - mb-[32rem] + mb-128 ``` But now it can also handle negative values: ```diff - mb-[-32rem] + -mb-128 ``` The only tricky part here is that we had to hoist the `-` sign. Before this PR, we were actually generating `mb--128` and that is invalid so it was thrown out. ## Test plan 1. Added a test to ensure that the negative values are correctly transformed.
1 parent 191195a commit b3fde17

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Upgrade: migrate arbitrary modifiers with values without percentage sign to bare values `/[0.16]` -> `/16` ([#18184](https://github.com/tailwindlabs/tailwindcss/pull/18184))
1313
- Upgrade: migrate CSS variable shorthand if fallback value contains function call ([#18184](https://github.com/tailwindlabs/tailwindcss/pull/18184))
14+
- Upgrade: Migrate negative arbitrary values to negative bare values, e.g.: `mb-[-32rem]``-mb-128` ([#18212](https://github.com/tailwindlabs/tailwindcss/pull/18212))
1415

1516
## [4.1.8] - 2025-05-27
1617

packages/@tailwindcss-upgrade/src/codemods/template/migrate-arbitrary-utilities.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,21 @@ export function migrateArbitraryUtilities(
159159
candidate.kind === 'arbitrary' ? candidate.value : (candidate.value?.value ?? null)
160160
if (value === null) return
161161

162-
let spacingMultiplier = spacing.get(designSystem)?.get(value)
162+
let spacingMultiplier = spacing.get(designSystem)?.get(value) ?? null
163+
let rootPrefix = ''
164+
if (spacingMultiplier !== null && spacingMultiplier < 0) {
165+
rootPrefix = '-'
166+
spacingMultiplier = Math.abs(spacingMultiplier)
167+
}
163168

164169
for (let root of Array.from(designSystem.utilities.keys('functional')).sort(
165170
// Sort negative roots after positive roots so that we can try
166171
// `mt-*` before `-mt-*`. This is especially useful in situations where
167172
// `-mt-[0px]` can be translated to `mt-[0px]`.
168173
(a, z) => Number(a[0] === '-') - Number(z[0] === '-'),
169174
)) {
175+
if (rootPrefix) root = `${rootPrefix}${root}`
176+
170177
// Try as bare value
171178
for (let replacementCandidate of parseCandidate(designSystem, `${root}-${value}`)) {
172179
yield replacementCandidate

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
6969
// the fallback contains functions. The fallback should also be migrated to
7070
// the newest syntax.
7171
['bg-[--my-color,theme(colors.red.500)]', 'bg-(--my-color,var(--color-red-500))'],
72+
73+
// Both the positive and negative versions of arbitrary utilities should be
74+
// converted to the bare value version.
75+
['mb-[32rem]', 'mb-128'],
76+
['mb-[-32rem]', '-mb-128'],
7277
])(testName, async (candidate, result) => {
7378
if (strategy === 'with-variant') {
7479
candidate = `focus:${candidate}`

0 commit comments

Comments
 (0)