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
  0b5eaab6_9345_179e_f96e_cde6155e1ac1["migrateVariantOrder()"]
  91c7b7d7_b75e_448f_492f_b7e96c0ac0c5["isMajor()"]
  0b5eaab6_9345_179e_f96e_cde6155e1ac1 -->|calls| 91c7b7d7_b75e_448f_492f_b7e96c0ac0c5
  53cf41fe_5903_d247_3bb3_38414ba7d631["parseCandidate()"]
  0b5eaab6_9345_179e_f96e_cde6155e1ac1 -->|calls| 53cf41fe_5903_d247_3bb3_38414ba7d631
  b3948c6e_ed8b_00c4_5c29_df98ab2469d3["isAtRuleVariant()"]
  0b5eaab6_9345_179e_f96e_cde6155e1ac1 -->|calls| b3948c6e_ed8b_00c4_5c29_df98ab2469d3
  ea9b4c5e_3f0c_08c2_79fe_f685bee1afab["isEndOfSelectorPseudoElement()"]
  0b5eaab6_9345_179e_f96e_cde6155e1ac1 -->|calls| ea9b4c5e_3f0c_08c2_79fe_f685bee1afab
  91cd44e1_a9bd_4cc3_135a_48565792ef00["isCombinatorVariant()"]
  0b5eaab6_9345_179e_f96e_cde6155e1ac1 -->|calls| 91cd44e1_a9bd_4cc3_135a_48565792ef00
  fefab722_f9e9_db9c_31f2_f84c39788641["orderMatches()"]
  0b5eaab6_9345_179e_f96e_cde6155e1ac1 -->|calls| fefab722_f9e9_db9c_31f2_f84c39788641
  ec55634f_f6e4_3b8b_1267_0b251c4dade1["printCandidate()"]
  0b5eaab6_9345_179e_f96e_cde6155e1ac1 -->|calls| ec55634f_f6e4_3b8b_1267_0b251c4dade1
  style 0b5eaab6_9345_179e_f96e_cde6155e1ac1 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.
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