Home / Function/ migrateLegacyClasses() — tailwindcss Function Reference

migrateLegacyClasses() — tailwindcss Function Reference

Architecture documentation for the migrateLegacyClasses() function in migrate-legacy-classes.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  4ed0d823_ca6a_b1cd_308d_86008a627784["migrateLegacyClasses()"]
  91c7b7d7_b75e_448f_492f_b7e96c0ac0c5["isMajor()"]
  4ed0d823_ca6a_b1cd_308d_86008a627784 -->|calls| 91c7b7d7_b75e_448f_492f_b7e96c0ac0c5
  4cd99e59_ac1e_2a1f_0946_33cc1afd2532["get()"]
  4ed0d823_ca6a_b1cd_308d_86008a627784 -->|calls| 4cd99e59_ac1e_2a1f_0946_33cc1afd2532
  8a068325_2349_1f6e_50da_da53008019d1["baseCandidate()"]
  4ed0d823_ca6a_b1cd_308d_86008a627784 -->|calls| 8a068325_2349_1f6e_50da_da53008019d1
  ec55634f_f6e4_3b8b_1267_0b251c4dade1["printCandidate()"]
  4ed0d823_ca6a_b1cd_308d_86008a627784 -->|calls| ec55634f_f6e4_3b8b_1267_0b251c4dade1
  6aa3d960_a2f2_03a4_9eb3_642a5598b141["cloneCandidate()"]
  4ed0d823_ca6a_b1cd_308d_86008a627784 -->|calls| 6aa3d960_a2f2_03a4_9eb3_642a5598b141
  style 4ed0d823_ca6a_b1cd_308d_86008a627784 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-upgrade/src/codemods/template/migrate-legacy-classes.ts lines 70–144

export async function migrateLegacyClasses(
  designSystem: DesignSystem,
  _userConfig: Config | null,
  rawCandidate: string,
): Promise<string> {
  // These migrations are only safe when migrating from v3 to v4.
  //
  // Migrating from `rounded` to `rounded-sm` once is fine (v3 -> v4). But if we
  // migrate again (v4 -> v4), then `rounded-sm` would be migrated to
  // `rounded-xs` which is incorrect because we already migrated this.
  if (!version.isMajor(3)) {
    return rawCandidate
  }

  let defaultDesignSystem = await DESIGN_SYSTEMS.get(__dirname)

  function* migrate(rawCandidate: string) {
    for (let candidate of designSystem.parseCandidate(rawCandidate)) {
      // Create a base candidate string from the candidate.
      // E.g.: `hover:blur!` -> `blur`
      let base = baseCandidate(candidate)
      let baseCandidateString = designSystem.printCandidate(base)

      // Find the new base candidate string. `blur` -> `blur-sm`
      let newBaseCandidateString = LEGACY_CLASS_MAP.get(baseCandidateString)
      if (!newBaseCandidateString) continue

      // Parse the new base candidate string into an actual candidate AST.
      let [newBaseCandidate] = designSystem.parseCandidate(newBaseCandidateString)
      if (!newBaseCandidate) continue

      // Re-apply the variants and important flag from the original candidate.
      // E.g.: `hover:blur!` -> `blur` -> `blur-sm` -> `hover:blur-sm!`
      let newCandidate = cloneCandidate(newBaseCandidate) as Candidate
      newCandidate.variants = candidate.variants
      newCandidate.important = candidate.important

      yield [
        newCandidate,
        THEME_KEYS.get(baseCandidateString),
        THEME_KEYS.get(newBaseCandidateString),
      ] as const
    }
  }

  for (let [toCandidate, fromThemeKey, toThemeKey] of migrate(rawCandidate)) {
    if (fromThemeKey && toThemeKey) {
      // Migrating something that resolves to a value in the theme.
      let customFrom = designSystem.resolveThemeValue(fromThemeKey, true)
      let defaultFrom = defaultDesignSystem.resolveThemeValue(fromThemeKey, true)
      let customTo = designSystem.resolveThemeValue(toThemeKey, true)
      let defaultTo = defaultDesignSystem.resolveThemeValue(toThemeKey)

      // The new theme value is not defined, which means we can't safely
      // migrate the utility.
      if (customTo === undefined && defaultTo !== undefined) {
        continue
      }

      // The "from" theme value changed compared to the default theme value.
      if (customFrom !== defaultFrom) {
        continue
      }

      // The "to" theme value changed compared to the default theme value.
      if (customTo !== defaultTo) {
        continue
      }
    }

    return designSystem.printCandidate(toCandidate)
  }

  return rawCandidate
}

Subdomains

Frequently Asked Questions

What does migrateLegacyClasses() do?
migrateLegacyClasses() is a function in the tailwindcss codebase.
What does migrateLegacyClasses() call?
migrateLegacyClasses() calls 5 function(s): baseCandidate, cloneCandidate, get, isMajor, printCandidate.

Analyze Your Own Codebase

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

Try Supermodel Free