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
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9["migrateAutomaticVarInjection()"]
  0064e910_a1e5_1dd9_95f1_ac33aef2f9b1["migrate-automatic-var-injection.ts"]
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9 -->|defined in| 0064e910_a1e5_1dd9_95f1_ac33aef2f9b1
  7d328a86_10c6_b5c1_6390_36f4fffe9c14["parseCandidate()"]
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9 -->|calls| 7d328a86_10c6_b5c1_6390_36f4fffe9c14
  23c6e5a0_eac4_e9dd_7fcf_1e5c71d28310["cloneCandidate()"]
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9 -->|calls| 23c6e5a0_eac4_e9dd_7fcf_1e5c71d28310
  45e706aa_1a3f_8bf4_b828_65bf65ab488e["isAutomaticVarInjectionException()"]
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9 -->|calls| 45e706aa_1a3f_8bf4_b828_65bf65ab488e
  0f7313a5_adb3_4061_aabb_d87ca01352a1["injectVar()"]
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9 -->|calls| 0f7313a5_adb3_4061_aabb_d87ca01352a1
  8d5f9b9b_87e8_82f7_232d_21a34c01e2f4["injectVarIntoVariant()"]
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9 -->|calls| 8d5f9b9b_87e8_82f7_232d_21a34c01e2f4
  2de86ba2_90a4_8c2d_db18_154bb1a1564f["printCandidate()"]
  d9972d7e_4ead_d84f_3f97_3cc5f06273b9 -->|calls| 2de86ba2_90a4_8c2d_db18_154bb1a1564f
  style d9972d7e_4ead_d84f_3f97_3cc5f06273b9 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