Home / Function/ applyCleanup() — ui Function Reference

applyCleanup() — ui Function Reference

Architecture documentation for the applyCleanup() function in transform-cleanup.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  182b5198_cbf0_43bf_ab80_22341899f430["applyCleanup()"]
  e5d28271_9de7_c6c9_1240_13894663e9ec["transform-cleanup.ts"]
  182b5198_cbf0_43bf_ab80_22341899f430 -->|defined in| e5d28271_9de7_c6c9_1240_13894663e9ec
  514a1a89_b3a4_2880_543f_72365527ea8c["transformCleanup()"]
  514a1a89_b3a4_2880_543f_72365527ea8c -->|calls| 182b5198_cbf0_43bf_ab80_22341899f430
  69d323c2_c5e0_0997_8cae_9e39a6771952["cleanupMarkers()"]
  69d323c2_c5e0_0997_8cae_9e39a6771952 -->|calls| 182b5198_cbf0_43bf_ab80_22341899f430
  c780c4ec_87b3_96f7_852b_71aa98849451["stripCnMarkers()"]
  182b5198_cbf0_43bf_ab80_22341899f430 -->|calls| c780c4ec_87b3_96f7_852b_71aa98849451
  92337c41_d4af_7a39_2d78_06f4ca9b2100["processStringLiterals()"]
  182b5198_cbf0_43bf_ab80_22341899f430 -->|calls| 92337c41_d4af_7a39_2d78_06f4ca9b2100
  6bf4b1e9_4fda_58d0_5982_4738b771600c["processStringLiteral()"]
  182b5198_cbf0_43bf_ab80_22341899f430 -->|calls| 6bf4b1e9_4fda_58d0_5982_4738b771600c
  style 182b5198_cbf0_43bf_ab80_22341899f430 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/utils/transformers/transform-cleanup.ts lines 52–124

export function applyCleanup(sourceFile: SourceFile) {
  // Collect attributes to remove (can't remove while iterating).
  const attributesToRemove: ReturnType<
    typeof sourceFile.getDescendantsOfKind<typeof SyntaxKind.JsxAttribute>
  > = []

  // Process all JSX className attributes.
  for (const attr of sourceFile.getDescendantsOfKind(SyntaxKind.JsxAttribute)) {
    const attrName = attr.getNameNode().getText()
    if (attrName !== "className" && attrName !== "classNames") {
      continue
    }

    const initializer = attr.getInitializer()

    // className="..."
    if (initializer?.isKind(SyntaxKind.StringLiteral)) {
      const currentValue = initializer.getLiteralValue()
      if (CN_MARKER_REGEX.test(currentValue)) {
        const newValue = stripCnMarkers(currentValue)
        if (newValue === "") {
          // Remove the entire attribute if className becomes empty.
          attributesToRemove.push(attr)
        } else if (newValue !== currentValue) {
          initializer.setLiteralValue(newValue)
        }
      }
    }

    // className={...} or classNames={{...}}
    if (initializer?.isKind(SyntaxKind.JsxExpression)) {
      processStringLiterals(initializer)
    }
  }

  // Remove empty className attributes.
  for (const attr of attributesToRemove) {
    attr.remove()
  }

  // Process cva() calls.
  for (const call of sourceFile.getDescendantsOfKind(
    SyntaxKind.CallExpression
  )) {
    if (call.getExpression().getText() !== "cva") {
      continue
    }

    for (const arg of call.getArguments()) {
      if (arg.isKind(SyntaxKind.StringLiteral)) {
        processStringLiteral(arg)
        continue
      }
      if (arg.isKind(SyntaxKind.NoSubstitutionTemplateLiteral)) {
        processStringLiteral(arg)
        continue
      }
      // Handle object arguments (variants).
      processStringLiterals(arg)
    }
  }

  // Process mergeProps() calls.
  for (const call of sourceFile.getDescendantsOfKind(
    SyntaxKind.CallExpression
  )) {
    if (call.getExpression().getText() !== "mergeProps") {
      continue
    }

    processStringLiterals(call)
  }
}

Subdomains

Frequently Asked Questions

What does applyCleanup() do?
applyCleanup() is a function in the ui codebase, defined in packages/shadcn/src/utils/transformers/transform-cleanup.ts.
Where is applyCleanup() defined?
applyCleanup() is defined in packages/shadcn/src/utils/transformers/transform-cleanup.ts at line 52.
What does applyCleanup() call?
applyCleanup() calls 3 function(s): processStringLiteral, processStringLiterals, stripCnMarkers.
What calls applyCleanup()?
applyCleanup() is called by 2 function(s): cleanupMarkers, transformCleanup.

Analyze Your Own Codebase

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

Try Supermodel Free