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 UpgradeToolkit Codemods 4 imports 10 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  e7639b6d_2132_7e05_9498_c732c7517772["format-nodes.ts"]
  200c8408_4d17_9364_423b_0ce2d40b1e09["walk.ts"]
  e7639b6d_2132_7e05_9498_c732c7517772 --> 200c8408_4d17_9364_423b_0ce2d40b1e09
  fb009bc2_68d9_5c0f_7fa7_fa364488e50a["walk"]
  e7639b6d_2132_7e05_9498_c732c7517772 --> fb009bc2_68d9_5c0f_7fa7_fa364488e50a
  ba54c7c3_7b1e_9984_bfef_a693a3df2d84["postcss"]
  e7639b6d_2132_7e05_9498_c732c7517772 --> ba54c7c3_7b1e_9984_bfef_a693a3df2d84
  314f842b_c991_fa3a_70e2_6e0554f5789b["prettier"]
  e7639b6d_2132_7e05_9498_c732c7517772 --> 314f842b_c991_fa3a_70e2_6e0554f5789b
  fb30d75d_33fe_1bdc_7044_fd6d2cc1b187["format-nodes.test.ts"]
  fb30d75d_33fe_1bdc_7044_fd6d2cc1b187 --> e7639b6d_2132_7e05_9498_c732c7517772
  bf639994_959a_cded_6f3a_163595545a18["migrate-at-layer-utilities.test.ts"]
  bf639994_959a_cded_6f3a_163595545a18 --> e7639b6d_2132_7e05_9498_c732c7517772
  2a2fbf55_42db_0140_9db5_094f5369e747["migrate-media-screen.test.ts"]
  2a2fbf55_42db_0140_9db5_094f5369e747 --> e7639b6d_2132_7e05_9498_c732c7517772
  eb33b378_40f9_8aa4_2ce0_91f69294a359["migrate-missing-layers.test.ts"]
  eb33b378_40f9_8aa4_2ce0_91f69294a359 --> e7639b6d_2132_7e05_9498_c732c7517772
  48369d9b_74af_14f1_33c7_7d263509d02f["migrate-preflight.test.ts"]
  48369d9b_74af_14f1_33c7_7d263509d02f --> e7639b6d_2132_7e05_9498_c732c7517772
  8c7e8a4d_b48e_dd10_cef8_38f31c518cee["migrate-tailwind-directives.test.ts"]
  8c7e8a4d_b48e_dd10_cef8_38f31c518cee --> e7639b6d_2132_7e05_9498_c732c7517772
  0c13c6dd_2cb9_954d_8cc6_359b1ce5d612["migrate-theme-to-var.test.ts"]
  0c13c6dd_2cb9_954d_8cc6_359b1ce5d612 --> e7639b6d_2132_7e05_9498_c732c7517772
  10853aef_1b75_87ab_4c90_e8e234d21371["migrate-variants-directive.test.ts"]
  10853aef_1b75_87ab_4c90_e8e234d21371 --> e7639b6d_2132_7e05_9498_c732c7517772
  9c587449_9592_da71_37c3_2f37510d84c5["index.test.ts"]
  9c587449_9592_da71_37c3_2f37510d84c5 --> e7639b6d_2132_7e05_9498_c732c7517772
  b2eb6cbf_d28d_9ec7_61c1_8992d8f4efb8["index.ts"]
  b2eb6cbf_d28d_9ec7_61c1_8992d8f4efb8 --> e7639b6d_2132_7e05_9498_c732c7517772
  style e7639b6d_2132_7e05_9498_c732c7517772 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 UpgradeToolkit 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: UpgradeToolkit, 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