Skip to content

Commit 93af82b

Browse files
committed
migrate [[data-visible]_&] to in-data-visible
1 parent 34678d1 commit 93af82b

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ test.each([
1616
['[&:not(:first-child)]:flex', 'not-first:flex'],
1717

1818
// in-* variants
19-
['[p_&]:flex', 'in-p:flex'],
19+
['[p_&]:flex', 'in-[p]:flex'],
2020
['[.foo_&]:flex', 'in-[.foo]:flex'],
21+
['[[data-visible]_&]:flex', 'in-data-visible:flex'],
2122
// Some extreme examples of what happens in the wild:
2223
['group-[]:flex', 'in-[.group]:flex'],
2324
['group-[]/name:flex', 'in-[.group\\/name]:flex'],

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export function modernizeArbitraryValues(
1616

1717
for (let [variant, parent] of variants(clone)) {
1818
// Forward modifier from the root to the compound variant
19-
if (variant.kind === 'compound' && (variant.root === 'has' || variant.root === 'not')) {
19+
if (
20+
variant.kind === 'compound' &&
21+
(variant.root === 'has' || variant.root === 'not' || variant.root === 'in')
22+
) {
2023
if (variant.modifier !== null) {
2124
if ('modifier' in variant.variant) {
2225
variant.variant.modifier = variant.modifier
@@ -97,6 +100,30 @@ export function modernizeArbitraryValues(
97100
prefixedVariant = designSystem.parseVariant('**')
98101
}
99102

103+
// Handling a child/parent combinator. E.g.: `[[data-visible]_&]` => `in-data-visible`
104+
if (
105+
// Only top-level, so `has-[&_[data-visible]]` is not supported
106+
parent === null &&
107+
// [[data-visible]___&]:flex
108+
// ^^^^^^^^^^^^^^ ^ ^
109+
ast.nodes[0].length === 3 &&
110+
ast.nodes[0].nodes[0].type === 'attribute' &&
111+
ast.nodes[0].nodes[1].type === 'combinator' &&
112+
ast.nodes[0].nodes[1].value === ' ' &&
113+
ast.nodes[0].nodes[2].type === 'nesting' &&
114+
ast.nodes[0].nodes[2].value === '&'
115+
) {
116+
ast.nodes[0].nodes = [ast.nodes[0].nodes[0]]
117+
changed = true
118+
// When handling a compound like `in-[[data-visible]]`, we will first
119+
// handle `[[data-visible]]`, then the parent `in-*` part. This means
120+
// that we can convert `[[data-visible]_&]` to `in-[[data-visible]]`.
121+
//
122+
// Later this gets converted to `in-data-visible`.
123+
Object.assign(variant, designSystem.parseVariant(`in-[${ast.toString()}]`))
124+
continue
125+
}
126+
100127
// `in-*` variant
101128
if (
102129
// Only top-level, so `has-[p_&]` is not supported
@@ -124,12 +151,7 @@ export function modernizeArbitraryValues(
124151
}
125152

126153
changed = true
127-
if (nodes.length === 1 && nodes[0].type === 'tag') {
128-
Object.assign(variant, designSystem.parseVariant(`in-${selector.toString().trim()}`))
129-
} else {
130-
Object.assign(variant, designSystem.parseVariant(`in-[${selector.toString().trim()}]`))
131-
}
132-
154+
Object.assign(variant, designSystem.parseVariant(`in-[${selector.toString().trim()}]`))
133155
continue
134156
}
135157

0 commit comments

Comments
 (0)