Home / File/ format-nodes.ts — tailwindcss Source File

format-nodes.ts — tailwindcss Source File

Architecture documentation for format-nodes.ts, a typescript file in the tailwindcss codebase. 4 imports, 10 dependents.

File typescript CommandLineInterface Codemods 4 imports 10 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  a088502e_f5da_0531_fbd0_c586a964c369["format-nodes.ts"]
  8c8c71d8_c9b0_02ac_73ec_950510adcf7a["walk.ts"]
  a088502e_f5da_0531_fbd0_c586a964c369 --> 8c8c71d8_c9b0_02ac_73ec_950510adcf7a
  dd7577d7_bdb0_88c5_438f_51bb090ec42a["walk"]
  a088502e_f5da_0531_fbd0_c586a964c369 --> dd7577d7_bdb0_88c5_438f_51bb090ec42a
  7c3c22f8_be1a_4490_9f3e_622280887fe1["postcss"]
  a088502e_f5da_0531_fbd0_c586a964c369 --> 7c3c22f8_be1a_4490_9f3e_622280887fe1
  10ecf419_277e_3620_6d06_e109dd18d553["prettier"]
  a088502e_f5da_0531_fbd0_c586a964c369 --> 10ecf419_277e_3620_6d06_e109dd18d553
  d416162c_9943_db17_ed57_8be427000ef6["format-nodes.test.ts"]
  d416162c_9943_db17_ed57_8be427000ef6 --> a088502e_f5da_0531_fbd0_c586a964c369
  2cb5ef1d_43c0_4377_fe5a_941d800f83c7["migrate-at-layer-utilities.test.ts"]
  2cb5ef1d_43c0_4377_fe5a_941d800f83c7 --> a088502e_f5da_0531_fbd0_c586a964c369
  ee07edaa_f575_f9a5_6581_1200f759b32c["migrate-media-screen.test.ts"]
  ee07edaa_f575_f9a5_6581_1200f759b32c --> a088502e_f5da_0531_fbd0_c586a964c369
  961d377c_755d_e488_5737_dfddc91c3ee6["migrate-missing-layers.test.ts"]
  961d377c_755d_e488_5737_dfddc91c3ee6 --> a088502e_f5da_0531_fbd0_c586a964c369
  45ec79c2_d411_4cc2_0b36_0b2e19cf72e6["migrate-preflight.test.ts"]
  45ec79c2_d411_4cc2_0b36_0b2e19cf72e6 --> a088502e_f5da_0531_fbd0_c586a964c369
  320d991a_2688_e5d4_fc07_c946ad430e00["migrate-tailwind-directives.test.ts"]
  320d991a_2688_e5d4_fc07_c946ad430e00 --> a088502e_f5da_0531_fbd0_c586a964c369
  503d103e_c7cf_7d9c_7027_bc94b8da09d3["migrate-theme-to-var.test.ts"]
  503d103e_c7cf_7d9c_7027_bc94b8da09d3 --> a088502e_f5da_0531_fbd0_c586a964c369
  abead924_2390_edac_c530_1a639293bbdf["migrate-variants-directive.test.ts"]
  abead924_2390_edac_c530_1a639293bbdf --> a088502e_f5da_0531_fbd0_c586a964c369
  150ae0b2_d69c_0142_62f5_7237ef3b13df["index.test.ts"]
  150ae0b2_d69c_0142_62f5_7237ef3b13df --> a088502e_f5da_0531_fbd0_c586a964c369
  f3e20782_7a7a_6d07_0472_a959db30007c["index.ts"]
  f3e20782_7a7a_6d07_0472_a959db30007c --> a088502e_f5da_0531_fbd0_c586a964c369
  style a088502e_f5da_0531_fbd0_c586a964c369 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import postcss, { type ChildNode, type Plugin, type Root } from 'postcss'
import { format, type Options } from 'prettier'
import { walk } from '../../utils/walk'

const FORMAT_OPTIONS: Options = {
  parser: 'css',
  semi: true,
  singleQuote: true,
}

// Prettier is used to generate cleaner output, but it's only used on the nodes
// that were marked as `pretty` during the migration.
export function formatNodes(): Plugin {
  async function migrate(root: Root) {
    // Find the nodes to format
    let nodesToFormat: ChildNode[] = []
    walk(root, (child, _idx, parent) => {
      // Always print semicolons after at-rules
      if (child.type === 'atrule') {
        child.raws.semicolon = true
      }

      if (child.type === 'atrule' && child.name === 'tw-bucket') {
        nodesToFormat.push(child)
      } else if (child.raws.tailwind_pretty) {
        // @ts-expect-error We might not have a parent
        child.parent ??= parent
        nodesToFormat.unshift(child)
      }
    })

    let output: string[] = []

    // Format the nodes
    for (let node of nodesToFormat) {
      let contents = (() => {
        if (node.type === 'atrule' && node.name === 'tw-bucket') {
          // Remove the `@tw-bucket` wrapping, and use the contents directly.
          return node
            .toString()
            .trim()
            .replace(/@tw-bucket(.*?){([\s\S]*)}/, '$2')
        }

        return node.toString()
      })()

      // Do not format the user bucket to ensure we keep the user's formatting
      // intact.
      if (node.type === 'atrule' && node.name === 'tw-bucket' && node.params === 'user') {
        output.push(contents)
        continue
      }

      // Format buckets
      if (node.type === 'atrule' && node.name === 'tw-bucket') {
        output.push(await format(contents, FORMAT_OPTIONS))
        continue
      }

      // Format any other nodes
      node.replaceWith(
        postcss.parse(
          `${node.raws.before ?? ''}${(await format(contents, FORMAT_OPTIONS)).trim()}`,
        ),
      )
    }

    root.removeAll()
    root.append(
      postcss.parse(
        output
          .map((bucket) => bucket.trim())
          .filter(Boolean)
          .join('\n\n'),
      ),
    )
  }

  return {
    postcssPlugin: '@tailwindcss/upgrade/format-nodes',
    OnceExit: migrate,
  }
}

Subdomains

Functions

Dependencies

Frequently Asked Questions

What does format-nodes.ts do?
format-nodes.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 format-nodes.ts?
format-nodes.ts defines 1 function(s): formatNodes.
What does format-nodes.ts depend on?
format-nodes.ts imports 4 module(s): postcss, prettier, walk, walk.ts.
What files import format-nodes.ts?
format-nodes.ts is imported by 10 file(s): format-nodes.test.ts, index.test.ts, index.ts, migrate-at-layer-utilities.test.ts, migrate-media-screen.test.ts, migrate-missing-layers.test.ts, migrate-preflight.test.ts, migrate-tailwind-directives.test.ts, and 2 more.
Where is format-nodes.ts in the architecture?
format-nodes.ts is located at packages/@tailwindcss-upgrade/src/codemods/css/format-nodes.ts (domain: CommandLineInterface, subdomain: Codemods, directory: packages/@tailwindcss-upgrade/src/codemods/css).

Analyze Your Own Codebase

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

Try Supermodel Free