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
  155d1c0d_2551_0baf_c0ce_75204772c9a3["migrateLegacyClasses()"]
  f8237e3b_92f4_a95b_9115_6042ea9c4699["migrate-legacy-classes.ts"]
  155d1c0d_2551_0baf_c0ce_75204772c9a3 -->|defined in| f8237e3b_92f4_a95b_9115_6042ea9c4699
  8d08c9fd_45a1_f37c_a879_6fd2c9991403["isMajor()"]
  155d1c0d_2551_0baf_c0ce_75204772c9a3 -->|calls| 8d08c9fd_45a1_f37c_a879_6fd2c9991403
  5bcf4886_1230_a8ff_7302_a26cc5a9a525["get()"]
  155d1c0d_2551_0baf_c0ce_75204772c9a3 -->|calls| 5bcf4886_1230_a8ff_7302_a26cc5a9a525
  df17a03b_9c60_27a5_3fcf_5acdd9570ff9["baseCandidate()"]
  155d1c0d_2551_0baf_c0ce_75204772c9a3 -->|calls| df17a03b_9c60_27a5_3fcf_5acdd9570ff9
  87ba7eab_3a52_d53e_dfd4_e507e9763b55["printCandidate()"]
  155d1c0d_2551_0baf_c0ce_75204772c9a3 -->|calls| 87ba7eab_3a52_d53e_dfd4_e507e9763b55
  f29ee016_da0a_a564_1658_fedaaac680b6["cloneCandidate()"]
  155d1c0d_2551_0baf_c0ce_75204772c9a3 -->|calls| f29ee016_da0a_a564_1658_fedaaac680b6
  4430ae86_7b45_9a73_d25b_0bbdd1cc59ac["parseCandidate()"]
  155d1c0d_2551_0baf_c0ce_75204772c9a3 -->|calls| 4430ae86_7b45_9a73_d25b_0bbdd1cc59ac
  style 155d1c0d_2551_0baf_c0ce_75204772c9a3 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, defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-legacy-classes.ts.
Where is migrateLegacyClasses() defined?
migrateLegacyClasses() is defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-legacy-classes.ts at line 70.
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