Home / Function/ migrateAutomaticVarInjection() — tailwindcss Function Reference

migrateAutomaticVarInjection() — tailwindcss Function Reference

Architecture documentation for the migrateAutomaticVarInjection() function in migrate-automatic-var-injection.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  aeb7589b_9d5b_312e_4558_4df754cbb905["migrateAutomaticVarInjection()"]
  d475bcf1_b109_d714_cc32_55d5655282ec["migrate-automatic-var-injection.ts"]
  aeb7589b_9d5b_312e_4558_4df754cbb905 -->|defined in| d475bcf1_b109_d714_cc32_55d5655282ec
  d4b90da0_01b5_b21d_ff05_b37798744576["parseCandidate()"]
  aeb7589b_9d5b_312e_4558_4df754cbb905 -->|calls| d4b90da0_01b5_b21d_ff05_b37798744576
  f29ee016_da0a_a564_1658_fedaaac680b6["cloneCandidate()"]
  aeb7589b_9d5b_312e_4558_4df754cbb905 -->|calls| f29ee016_da0a_a564_1658_fedaaac680b6
  2889bde1_8452_e1ef_7f54_e37d1c0b570d["isAutomaticVarInjectionException()"]
  aeb7589b_9d5b_312e_4558_4df754cbb905 -->|calls| 2889bde1_8452_e1ef_7f54_e37d1c0b570d
  343db49c_7d28_8ddb_aab2_f4cb835639a7["injectVar()"]
  aeb7589b_9d5b_312e_4558_4df754cbb905 -->|calls| 343db49c_7d28_8ddb_aab2_f4cb835639a7
  d77b4c83_759b_998e_e087_dabe3109d95c["injectVarIntoVariant()"]
  aeb7589b_9d5b_312e_4558_4df754cbb905 -->|calls| d77b4c83_759b_998e_e087_dabe3109d95c
  87ba7eab_3a52_d53e_dfd4_e507e9763b55["printCandidate()"]
  aeb7589b_9d5b_312e_4558_4df754cbb905 -->|calls| 87ba7eab_3a52_d53e_dfd4_e507e9763b55
  style aeb7589b_9d5b_312e_4558_4df754cbb905 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-upgrade/src/codemods/template/migrate-automatic-var-injection.ts lines 8–74

export function migrateAutomaticVarInjection(
  designSystem: DesignSystem,
  _userConfig: Config | null,
  rawCandidate: string,
): string {
  for (let readonlyCandidate of designSystem.parseCandidate(rawCandidate)) {
    // The below logic makes extended use of mutation. Since candidates in the
    // DesignSystem are cached, we can't mutate them directly.
    let candidate = cloneCandidate(readonlyCandidate) as Writable<Candidate>

    let didChange = false

    // Add `var(…)` in modifier position, e.g.:
    //
    // `bg-red-500/[--my-opacity]` => `bg-red-500/[var(--my-opacity)]`
    if (
      'modifier' in candidate &&
      candidate.modifier?.kind === 'arbitrary' &&
      !isAutomaticVarInjectionException(designSystem, candidate, candidate.modifier.value)
    ) {
      let { value, didChange: modifierDidChange } = injectVar(candidate.modifier.value)
      candidate.modifier.value = value
      didChange ||= modifierDidChange
    }

    // Add `var(…)` to all variants, e.g.:
    //
    // `supports-[--test]:flex'` => `supports-[var(--test)]:flex`
    for (let variant of candidate.variants) {
      let didChangeVariant = injectVarIntoVariant(designSystem, variant)
      if (didChangeVariant) {
        didChange = true
      }
    }

    // Add `var(…)` to arbitrary candidates, e.g.:
    //
    // `[color:--my-color]` => `[color:var(--my-color)]`
    if (
      candidate.kind === 'arbitrary' &&
      !isAutomaticVarInjectionException(designSystem, candidate, candidate.value)
    ) {
      let { value, didChange: valueDidChange } = injectVar(candidate.value)
      candidate.value = value
      didChange ||= valueDidChange
    }

    // Add `var(…)` to arbitrary values for functional candidates, e.g.:
    //
    // `bg-[--my-color]` => `bg-[var(--my-color)]`
    if (
      candidate.kind === 'functional' &&
      candidate.value &&
      candidate.value.kind === 'arbitrary' &&
      !isAutomaticVarInjectionException(designSystem, candidate, candidate.value.value)
    ) {
      let { value, didChange: valueDidChange } = injectVar(candidate.value.value)
      candidate.value.value = value
      didChange ||= valueDidChange
    }

    if (didChange) {
      return designSystem.printCandidate(candidate)
    }
  }
  return rawCandidate
}

Subdomains

Frequently Asked Questions

What does migrateAutomaticVarInjection() do?
migrateAutomaticVarInjection() is a function in the tailwindcss codebase, defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-automatic-var-injection.ts.
Where is migrateAutomaticVarInjection() defined?
migrateAutomaticVarInjection() is defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-automatic-var-injection.ts at line 8.
What does migrateAutomaticVarInjection() call?
migrateAutomaticVarInjection() calls 6 function(s): cloneCandidate, injectVar, injectVarIntoVariant, isAutomaticVarInjectionException, parseCandidate, printCandidate.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free