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
  83b8a939_5e80_0880_4db2_753e6d6e279c["migrateLegacyClasses()"]
  db7731f5_f3e0_5e90_b5d1_de4eda8ce9d2["isMajor()"]
  83b8a939_5e80_0880_4db2_753e6d6e279c -->|calls| db7731f5_f3e0_5e90_b5d1_de4eda8ce9d2
  0aa64a1c_efd8_a69d_48ed_649b7a86c854["get()"]
  83b8a939_5e80_0880_4db2_753e6d6e279c -->|calls| 0aa64a1c_efd8_a69d_48ed_649b7a86c854
  484442b1_10ce_2528_7bf3_09152637486d["baseCandidate()"]
  83b8a939_5e80_0880_4db2_753e6d6e279c -->|calls| 484442b1_10ce_2528_7bf3_09152637486d
  4c29981d_a61a_00fb_b3b5_5a69228c4fff["printCandidate()"]
  83b8a939_5e80_0880_4db2_753e6d6e279c -->|calls| 4c29981d_a61a_00fb_b3b5_5a69228c4fff
  d36b0f78_79bb_ae15_f4f6_4a1b7f01d29f["cloneCandidate()"]
  83b8a939_5e80_0880_4db2_753e6d6e279c -->|calls| d36b0f78_79bb_ae15_f4f6_4a1b7f01d29f
  5f08487b_feab_b37d_a44f_5e13d0aaf1a6["parseCandidate()"]
  83b8a939_5e80_0880_4db2_753e6d6e279c -->|calls| 5f08487b_feab_b37d_a44f_5e13d0aaf1a6
  style 83b8a939_5e80_0880_4db2_753e6d6e279c 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
}

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free