Home / Function/ migrateVariantOrder() — tailwindcss Function Reference

migrateVariantOrder() — tailwindcss Function Reference

Architecture documentation for the migrateVariantOrder() function in migrate-variant-order.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  d6f03aba_eecb_9486_a7b9_25fc0f87915d["migrateVariantOrder()"]
  1490304f_c9fd_75da_db3f_0c12d428c646["migrate-variant-order.ts"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|defined in| 1490304f_c9fd_75da_db3f_0c12d428c646
  6cf757d4_aaad_b18d_cf21_ade24c6e2a77["isMajor()"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|calls| 6cf757d4_aaad_b18d_cf21_ade24c6e2a77
  7d328a86_10c6_b5c1_6390_36f4fffe9c14["parseCandidate()"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|calls| 7d328a86_10c6_b5c1_6390_36f4fffe9c14
  215be44d_0a2d_decd_9cd9_3660e45dda28["isAtRuleVariant()"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|calls| 215be44d_0a2d_decd_9cd9_3660e45dda28
  4a69e7f1_11b8_a258_87f6_836ffe93411f["isEndOfSelectorPseudoElement()"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|calls| 4a69e7f1_11b8_a258_87f6_836ffe93411f
  35bdfef4_ec01_e84b_794d_56bafb521d13["isCombinatorVariant()"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|calls| 35bdfef4_ec01_e84b_794d_56bafb521d13
  8c83c9e0_d0ae_7991_7e76_8c78f63aa77e["orderMatches()"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|calls| 8c83c9e0_d0ae_7991_7e76_8c78f63aa77e
  2de86ba2_90a4_8c2d_db18_154bb1a1564f["printCandidate()"]
  d6f03aba_eecb_9486_a7b9_25fc0f87915d -->|calls| 2de86ba2_90a4_8c2d_db18_154bb1a1564f
  style d6f03aba_eecb_9486_a7b9_25fc0f87915d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-upgrade/src/codemods/template/migrate-variant-order.ts lines 8–62

export function migrateVariantOrder(
  designSystem: DesignSystem,
  _userConfig: Config | null,
  rawCandidate: string,
): string {
  // This migration is only needed for Tailwind CSS v3
  //
  // Changing the variant order when migrating from v3 to v4 is fine, but
  // migrating v4 to v4 would make it unsafe because the variant order would
  // flip-flop every time you run the migration.
  if (!version.isMajor(3)) {
    return rawCandidate
  }

  for (let candidate of designSystem.parseCandidate(rawCandidate)) {
    if (candidate.variants.length <= 1) {
      continue
    }

    let atRuleVariants = []
    let regularVariants = []
    let pseudoElementVariants = []

    let originalOrder = candidate.variants

    for (let variant of candidate.variants) {
      if (isAtRuleVariant(designSystem, variant)) {
        atRuleVariants.push(variant)
      } else if (isEndOfSelectorPseudoElement(designSystem, variant)) {
        pseudoElementVariants.push(variant)
      } else {
        regularVariants.push(variant)
      }
    }

    // We only need to reorder regular variants if order is important
    let regularVariantsNeedReordering = regularVariants.some((v) =>
      isCombinatorVariant(designSystem, v),
    )

    // The candidate list in the AST need to be in reverse order
    let newOrder = [
      ...pseudoElementVariants,
      ...(regularVariantsNeedReordering ? regularVariants.reverse() : regularVariants),
      ...atRuleVariants,
    ]

    if (orderMatches(originalOrder, newOrder)) {
      continue
    }

    return designSystem.printCandidate({ ...candidate, variants: newOrder })
  }
  return rawCandidate
}

Subdomains

Frequently Asked Questions

What does migrateVariantOrder() do?
migrateVariantOrder() is a function in the tailwindcss codebase, defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-variant-order.ts.
Where is migrateVariantOrder() defined?
migrateVariantOrder() is defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-variant-order.ts at line 8.
What does migrateVariantOrder() call?
migrateVariantOrder() calls 7 function(s): isAtRuleVariant, isCombinatorVariant, isEndOfSelectorPseudoElement, isMajor, orderMatches, parseCandidate, printCandidate.

Analyze Your Own Codebase

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

Try Supermodel Free