Skip to content

Commit 4687777

Browse files
Migrate [&>*] to * variant, and [&_*] to ** variant (#15022)
This PR adds a migration to convert the `[&>*]` variant to the `*` variant. Additionally this PR also converts the `[&_*]` variant to the `**` variant. We use this variant in Catalyst for example, and now that the specificity is the same as `*`, we can use the more modern syntax instead. # Test plan: Running this on Catalyst results in a diff like: <img width="615" alt="image" src="https://github.com/user-attachments/assets/f384885e-cae1-4b6b-80ab-85f76fa89a33"> <img width="833" alt="image" src="https://github.com/user-attachments/assets/8a185e1d-0f1b-4fe6-9e06-ca7597534398"> Note: the swapped order of variants is another codemod at work --------- Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
1 parent 3dc3bad commit 4687777

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.md

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

1212
- Reintroduce `max-w-screen-*` utilities that read from the `--breakpoint` namespace as deprecated utilities ([#15013](https://github.com/tailwindlabs/tailwindcss/pull/15013))
1313
- Support using CSS variables as arbitrary values without `var(…)` by using parentheses instead of square brackets (e.g. `bg-(--my-color)`) ([#15020](https://github.com/tailwindlabs/tailwindcss/pull/15020))
14+
- _Upgrade (experimental)_: Migrate `[&>*]` to the `*` variant ([#15022](https://github.com/tailwindlabs/tailwindcss/pull/15022))
15+
- _Upgrade (experimental)_: Migrate `[&_*]` to the `**` variant ([#15022](https://github.com/tailwindlabs/tailwindcss/pull/15022))
1416

1517
### Fixed
1618

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ test.each([
99
['[[data-visible]&]:flex', 'data-visible:flex'],
1010
['[&>[data-visible]]:flex', '*:data-visible:flex'],
1111
['[&_>_[data-visible]]:flex', '*:data-visible:flex'],
12+
['[&>*]:flex', '*:flex'],
13+
['[&_>_*]:flex', '*:flex'],
1214

1315
['[&_[data-visible]]:flex', '**:data-visible:flex'],
16+
['[&_*]:flex', '**:flex'],
1417

1518
['[&:first-child]:flex', 'first:flex'],
1619
['[&:not(:first-child)]:flex', 'not-first:flex'],

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,41 @@ export function modernizeArbitraryValues(
2828

2929
let prefixedVariant: Variant | null = null
3030

31-
// Track whether we need to add a `**:` variant
32-
let addStarStarVariant = false
31+
// `[&>*]` can be replaced with `*`
32+
if (
33+
// Only top-level, so `has-[&>*]` is not supported
34+
parent === null &&
35+
// [&_>_*]:flex
36+
// ^ ^ ^
37+
ast.nodes[0].length === 3 &&
38+
ast.nodes[0].nodes[0].type === 'nesting' &&
39+
ast.nodes[0].nodes[0].value === '&' &&
40+
ast.nodes[0].nodes[1].type === 'combinator' &&
41+
ast.nodes[0].nodes[1].value === '>' &&
42+
ast.nodes[0].nodes[2].type === 'universal'
43+
) {
44+
changed = true
45+
Object.assign(variant, designSystem.parseVariant('*'))
46+
continue
47+
}
48+
49+
// `[&_*]` can be replaced with `**`
50+
if (
51+
// Only top-level, so `has-[&_*]` is not supported
52+
parent === null &&
53+
// [&_*]:flex
54+
// ^ ^
55+
ast.nodes[0].length === 3 &&
56+
ast.nodes[0].nodes[0].type === 'nesting' &&
57+
ast.nodes[0].nodes[0].value === '&' &&
58+
ast.nodes[0].nodes[1].type === 'combinator' &&
59+
ast.nodes[0].nodes[1].value === ' ' &&
60+
ast.nodes[0].nodes[2].type === 'universal'
61+
) {
62+
changed = true
63+
Object.assign(variant, designSystem.parseVariant('**'))
64+
continue
65+
}
3366

3467
// Handling a child combinator. E.g.: `[&>[data-visible]]` => `*:data-visible`
3568
if (

0 commit comments

Comments
 (0)