Home / File/ migrate-variant-order.ts — tailwindcss Source File

migrate-variant-order.ts — tailwindcss Source File

Architecture documentation for migrate-variant-order.ts, a typescript file in the tailwindcss codebase. 9 imports, 2 dependents.

File typescript CommandLineInterface Codemods 9 imports 2 dependents 6 functions

Entity Profile

Dependency Diagram

graph LR
  1490304f_c9fd_75da_db3f_0c12d428c646["migrate-variant-order.ts"]
  b9cbffa4_c352_cf3c_268f_cbb174fb3a47["ast.ts"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> b9cbffa4_c352_cf3c_268f_cbb174fb3a47
  ba6fca27_7720_5839_0f92_bc2abb8db636["candidate.ts"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> ba6fca27_7720_5839_0f92_bc2abb8db636
  da5d1116_ab2a_437a_6b13_c1429fd546fa["plugin-api.ts"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> da5d1116_ab2a_437a_6b13_c1429fd546fa
  0255ffc0_a3d5_e883_5143_99660766448f["Config"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> 0255ffc0_a3d5_e883_5143_99660766448f
  bdedd2f6_da4b_69dc_e990_0814b59fbe6e["design-system.ts"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> bdedd2f6_da4b_69dc_e990_0814b59fbe6e
  665aa4ed_d86e_30e5_80d5_cd56b8ca8b62["DesignSystem"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> 665aa4ed_d86e_30e5_80d5_cd56b8ca8b62
  1b8f1c54_b1e9_e18d_0719_b7ad92808185["walk.ts"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> 1b8f1c54_b1e9_e18d_0719_b7ad92808185
  4982d9ce_98d4_85d9_44af_7cc47b93c482["walk"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> 4982d9ce_98d4_85d9_44af_7cc47b93c482
  472e1b98_afcf_f1f2_ad91_916d742bb731["version.ts"]
  1490304f_c9fd_75da_db3f_0c12d428c646 --> 472e1b98_afcf_f1f2_ad91_916d742bb731
  5caf09d4_232a_b06e_c5be_c10a64a683f5["migrate-variant-order.test.ts"]
  5caf09d4_232a_b06e_c5be_c10a64a683f5 --> 1490304f_c9fd_75da_db3f_0c12d428c646
  e39bed3f_bfbf_a8c6_90a0_aa2ce3be1ef4["migrate.ts"]
  e39bed3f_bfbf_a8c6_90a0_aa2ce3be1ef4 --> 1490304f_c9fd_75da_db3f_0c12d428c646
  style 1490304f_c9fd_75da_db3f_0c12d428c646 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { type AstNode } from '../../../../tailwindcss/src/ast'
import { type Variant } from '../../../../tailwindcss/src/candidate'
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { walk } from '../../../../tailwindcss/src/walk'
import * as version from '../../utils/version'

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 })
  }
// ... (76 more lines)

Subdomains

Frequently Asked Questions

What does migrate-variant-order.ts do?
migrate-variant-order.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the CommandLineInterface domain, Codemods subdomain.
What functions are defined in migrate-variant-order.ts?
migrate-variant-order.ts defines 6 function(s): getAppliedNodeStack, isAtRuleVariant, isCombinatorVariant, isEndOfSelectorPseudoElement, migrateVariantOrder, orderMatches.
What does migrate-variant-order.ts depend on?
migrate-variant-order.ts imports 9 module(s): Config, DesignSystem, ast.ts, candidate.ts, design-system.ts, plugin-api.ts, version.ts, walk, and 1 more.
What files import migrate-variant-order.ts?
migrate-variant-order.ts is imported by 2 file(s): migrate-variant-order.test.ts, migrate.ts.
Where is migrate-variant-order.ts in the architecture?
migrate-variant-order.ts is located at packages/@tailwindcss-upgrade/src/codemods/template/migrate-variant-order.ts (domain: CommandLineInterface, subdomain: Codemods, directory: packages/@tailwindcss-upgrade/src/codemods/template).

Analyze Your Own Codebase

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

Try Supermodel Free