Skip to content

Commit 57be02a

Browse files
authored
Improve in-* variant migrations (#15054)
While testing the codemods on some projects, I noticed some issues with the migration to the new `in-*` variant. One such example is that we checked for `&` at the end, instead of ` &` (the whitespace is significant). This meant that `[figure>&]:my-0` was converted to `in-[figure>]:my-0` which is wrong. In this case, we want to keep it as `[figure>&]:my-0`. Additionally this PR brings back the migration from `group-[]:flex` to `in-[.group]:flex`. If you are using a prefix, then `group-[]:tw-flex` is migrated to `tw:in-[.tw\:group]:flex`. Last but not least, this does some internal refactors to group migrations logically together.
1 parent 8b098fc commit 57be02a

File tree

3 files changed

+359
-291
lines changed

3 files changed

+359
-291
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ 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+
- _Upgrade (experimental)_: Convert `group-[]:flex` to `in-[.group]:flex` ([#15054](https://github.com/tailwindlabs/tailwindcss/pull/15054))
13+
14+
### Fixed
15+
16+
- _Upgrade (experimental)_: Ensure migrating to the `in-*` requires a descendant selector ([#15054](https://github.com/tailwindlabs/tailwindcss/pull/15054))
1117

1218
## [4.0.0-alpha.35] - 2024-11-20
1319

packages/@tailwindcss-upgrade/src/template/codemods/modernize-arbitrary-values.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { __unstable__loadDesignSystem } from '@tailwindcss/node'
22
import { expect, test } from 'vitest'
33
import { modernizeArbitraryValues } from './modernize-arbitrary-values'
4+
import { prefix } from './prefix'
45

56
test.each([
67
// Arbitrary variants
@@ -22,6 +23,20 @@ test.each([
2223
['[p_&]:flex', 'in-[p]:flex'],
2324
['[.foo_&]:flex', 'in-[.foo]:flex'],
2425
['[[data-visible]_&]:flex', 'in-data-visible:flex'],
26+
// Multiple selectors, should stay as-is
27+
['[[data-foo][data-bar]_&]:flex', '[[data-foo][data-bar]_&]:flex'],
28+
// Using `>` instead of ` ` should not be transformed:
29+
['[figure>&]:my-0', '[figure>&]:my-0'],
30+
// Some extreme examples of what happens in the wild:
31+
['group-[]:flex', 'in-[.group]:flex'],
32+
['group-[]/name:flex', 'in-[.group\\/name]:flex'],
33+
34+
// These shouldn't happen in the real world (because compound variants are
35+
// new). But this could happen once we allow codemods to run in v4+ projects.
36+
['has-group-[]:flex', 'has-in-[.group]:flex'],
37+
['has-group-[]/name:flex', 'has-in-[.group\\/name]:flex'],
38+
['not-group-[]:flex', 'not-in-[.group]:flex'],
39+
['not-group-[]/name:flex', 'not-in-[.group\\/name]:flex'],
2540

2641
// nth-child
2742
['[&:nth-child(2)]:flex', 'nth-2:flex'],
@@ -77,3 +92,33 @@ test.each([
7792

7893
expect(modernizeArbitraryValues(designSystem, {}, candidate)).toEqual(result)
7994
})
95+
96+
test.each([
97+
// Should not prefix classes in arbitrary values
98+
['[.foo_&]:tw-flex', 'tw:in-[.foo]:flex'],
99+
100+
// Should migrate `.group` classes
101+
['group-[]:tw-flex', 'tw:in-[.tw\\:group]:flex'],
102+
['group-[]/name:tw-flex', 'tw:in-[.tw\\:group\\/name]:flex'],
103+
104+
// However, `.group` inside of an arbitrary variant should not be prefixed:
105+
['[.group_&]:tw-flex', 'tw:in-[.group]:flex'],
106+
107+
// These shouldn't happen in the real world (because compound variants are
108+
// new). But this could happen once we allow codemods to run in v4+ projects.
109+
['has-group-[]:tw-flex', 'tw:has-in-[.tw\\:group]:flex'],
110+
['has-group-[]/name:tw-flex', 'tw:has-in-[.tw\\:group\\/name]:flex'],
111+
['not-group-[]:tw-flex', 'tw:not-in-[.tw\\:group]:flex'],
112+
['not-group-[]/name:tw-flex', 'tw:not-in-[.tw\\:group\\/name]:flex'],
113+
])('%s => %s', async (candidate, result) => {
114+
let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss" prefix(tw);', {
115+
base: __dirname,
116+
})
117+
118+
expect(
119+
[prefix, modernizeArbitraryValues].reduce(
120+
(acc, step) => step(designSystem, { prefix: 'tw-' }, acc),
121+
candidate,
122+
),
123+
).toEqual(result)
124+
})

0 commit comments

Comments
 (0)