Skip to content

Commit 3fb49bb

Browse files
Fix theme(…/15%) to only apply when used on its own (#14922)
This PR fixes an issue where our codemod migrations can convert `bg-[theme(colors.white/15%)]` to `bg-[var(--color-white)]/15` where the `15%` from within the `theme(…)` is converted to a candidate modifier (at the end). The idea was that if the `theme(…)` is used with a modifier, then it can only be used with colors. If a candidate uses it, it also means that a color was used and we can use `/15` instead. However this is not true if it is used as part of a bigger value. E.g.: `shadow-[shadow:inset_0_0_0_1px_theme(colors.white/15%)]` would be converted to `shadow-[inset_0_0_0_1px_var(--color-white)]/15` which is not correct because the value isn't a color, the color is _part_ of the value. In this case, we make sure that the `theme(…)` is the only AST node in the value, and if it is we can safely do the conversion. If there are other AST nodes we keep the `theme(…)` call. --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent 586723f commit 3fb49bb

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
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939
- _Upgrade (experimental)_: Install `@tailwindcss/postcss` next to `tailwindcss` ([#14830](https://github.com/tailwindlabs/tailwindcss/pull/14830))
4040
- _Upgrade (experimental)_: Remove whitespace around `,` separator when print arbitrary values ([#14838](https://github.com/tailwindlabs/tailwindcss/pull/14838))
4141
- _Upgrade (experimental)_: Fix crash during upgrade when content globs escape root of project ([#14896](https://github.com/tailwindlabs/tailwindcss/pull/14896))
42+
- _Upgrade (experimental)_: Don't convert `theme(…/15%)` to modifier unless it is the entire arbitrary value of a utility ([#14922](https://github.com/tailwindlabs/tailwindcss/pull/14922))
4243

4344
### Changed
4445

packages/@tailwindcss-upgrade/src/template/codemods/theme-to-var.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ test.each([
7777
// Arbitrary property that already contains a modifier
7878
['[color:theme(colors.red.500/50%)]/50', '[color:theme(--color-red-500/50%)]/50'],
7979

80+
// Values that don't contain only `theme(…)` calls should not be converted to
81+
// use a modifier since the color is not the whole value.
82+
[
83+
'shadow-[shadow:inset_0px_1px_theme(colors.white/15%)]',
84+
'shadow-[shadow:inset_0px_1px_theme(--color-white/15%)]',
85+
],
86+
8087
// Arbitrary value, where the candidate already contains a modifier
8188
// This should still migrate the `theme(…)` syntax to the modern syntax.
8289
['bg-[theme(colors.red.500/50%)]/50', 'bg-[theme(--color-red-500/50%)]/50'],

packages/@tailwindcss-upgrade/src/template/codemods/theme-to-var.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ export function createConverter(designSystem: DesignSystem, { prettyPrint = fals
158158
// Currently, we are assuming that this is only being used for colors,
159159
// which means that we can typically convert them to a modifier on the
160160
// candidate itself.
161-
if (parts.length === 2 && options & Convert.MigrateModifier) {
161+
//
162+
// If there is more than one node in the AST though, `theme(…)` must not
163+
// be the whole value so it's not safe to use a modifier instead.
164+
//
165+
// E.g.: `inset 0px 1px theme(colors.red.500/50%)` is a shadow, not a color.
166+
if (ast.length === 1 && parts.length === 2 && options & Convert.MigrateModifier) {
162167
let [pathPart, modifierPart] = parts
163168

164169
// 50% -> /50

0 commit comments

Comments
 (0)