Skip to content

Commit aff858a

Browse files
authored
Make themeToVar reusable (#14694)
This PR makes the `themeToVar` code we already use for migrating the `theme(…)` calls to `var(…)` calls in the candidates (found in source files), reusable for the next PR where we want to migrate `theme(…)` calls in your actual CSS. (This shouldn't really require a separate PR, but I'm playing with Graphite's stacked PR feature to see how it works in practice 😅)
1 parent 2d45f5a commit aff858a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { toKeyPath } from '../../../../tailwindcss/src/utils/to-key-path'
1212
import * as ValueParser from '../../../../tailwindcss/src/value-parser'
1313
import { printCandidate } from '../candidates'
1414

15-
enum Convert {
15+
export enum Convert {
1616
All = 0,
1717
MigrateModifier = 1 << 0,
1818
MigrateThemeOnly = 1 << 1,
@@ -23,6 +23,8 @@ export function themeToVar(
2323
_userConfig: Config,
2424
rawCandidate: string,
2525
): string {
26+
let convert = createConverter(designSystem)
27+
2628
for (let candidate of parseCandidate(rawCandidate, designSystem)) {
2729
let clone = structuredClone(candidate)
2830
let changed = false
@@ -75,6 +77,10 @@ export function themeToVar(
7577
return changed ? printCandidate(designSystem, clone) : rawCandidate
7678
}
7779

80+
return rawCandidate
81+
}
82+
83+
export function createConverter(designSystem: DesignSystem) {
7884
function convert(input: string, options = Convert.All): [string, CandidateModifier | null] {
7985
let ast = ValueParser.parse(input)
8086

@@ -209,7 +215,7 @@ export function themeToVar(
209215
return fallback ? `theme(${variable}${modifier}, ${fallback})` : `theme(${variable}${modifier})`
210216
}
211217

212-
return rawCandidate
218+
return convert
213219
}
214220

215221
function substituteFunctionsInValue(

0 commit comments

Comments
 (0)