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
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582["migrateVariantOrder()"]
  e3144eb9_0666_0bdb_e31d_1094b50abdd7["migrate-variant-order.ts"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|defined in| e3144eb9_0666_0bdb_e31d_1094b50abdd7
  8d08c9fd_45a1_f37c_a879_6fd2c9991403["isMajor()"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|calls| 8d08c9fd_45a1_f37c_a879_6fd2c9991403
  d4b90da0_01b5_b21d_ff05_b37798744576["parseCandidate()"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|calls| d4b90da0_01b5_b21d_ff05_b37798744576
  7cecf5b5_1806_696f_2da0_f581e79d92cb["isAtRuleVariant()"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|calls| 7cecf5b5_1806_696f_2da0_f581e79d92cb
  51d86fd3_bd8e_9cdd_7dd5_d8545f052117["isEndOfSelectorPseudoElement()"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|calls| 51d86fd3_bd8e_9cdd_7dd5_d8545f052117
  18e32168_6dcd_bcb0_ac5b_0ca6adcdf5c4["isCombinatorVariant()"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|calls| 18e32168_6dcd_bcb0_ac5b_0ca6adcdf5c4
  2fe09378_9851_33dd_2eb1_6dba5d483963["orderMatches()"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|calls| 2fe09378_9851_33dd_2eb1_6dba5d483963
  87ba7eab_3a52_d53e_dfd4_e507e9763b55["printCandidate()"]
  4b9b4a6d_0786_22dc_60cb_a53dc01dd582 -->|calls| 87ba7eab_3a52_d53e_dfd4_e507e9763b55
  style 4b9b4a6d_0786_22dc_60cb_a53dc01dd582 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