Home / File/ transform-legacy-icons.ts — ui Source File

transform-legacy-icons.ts — ui Source File

Architecture documentation for transform-legacy-icons.ts, a typescript file in the ui codebase. 4 imports, 0 dependents.

File typescript FrameworkTooling CLICore 4 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  4573b0fc_4d11_04f0_3914_a6d333cddb17["transform-legacy-icons.ts"]
  81c8b1a1_346a_8b27_dd1e_b8bbb29008b8["api"]
  4573b0fc_4d11_04f0_3914_a6d333cddb17 --> 81c8b1a1_346a_8b27_dd1e_b8bbb29008b8
  68e3f026_76dc_2c5a_bfe1_7569dbf163b6["legacy-icon-libraries"]
  4573b0fc_4d11_04f0_3914_a6d333cddb17 --> 68e3f026_76dc_2c5a_bfe1_7569dbf163b6
  e69863ed_3e2f_ef94_648a_ef0155c386ef["transformers"]
  4573b0fc_4d11_04f0_3914_a6d333cddb17 --> e69863ed_3e2f_ef94_648a_ef0155c386ef
  4f6f7e78_23ff_4f5f_c723_474454f64c85["ts-morph"]
  4573b0fc_4d11_04f0_3914_a6d333cddb17 --> 4f6f7e78_23ff_4f5f_c723_474454f64c85
  style 4573b0fc_4d11_04f0_3914_a6d333cddb17 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { getRegistryIcons } from "@/src/registry/api"
import { LEGACY_ICON_LIBRARIES } from "@/src/utils/legacy-icon-libraries"
import { Transformer } from "@/src/utils/transformers"
import { SourceFile, SyntaxKind } from "ts-morph"

// Lucide is the default icon library in the registry.
const SOURCE_LIBRARY = "lucide"

export const transformLegacyIcons: Transformer = async ({
  sourceFile,
  config,
}) => {
  // No transform if we cannot read the icon library.
  if (!config.iconLibrary || !(config.iconLibrary in LEGACY_ICON_LIBRARIES)) {
    return sourceFile
  }

  const registryIcons = await getRegistryIcons()
  const sourceLibrary = SOURCE_LIBRARY
  const targetLibrary = config.iconLibrary

  if (sourceLibrary === targetLibrary) {
    return sourceFile
  }

  let targetedIcons: string[] = []
  for (const importDeclaration of sourceFile.getImportDeclarations() ?? []) {
    if (
      importDeclaration.getModuleSpecifier()?.getText() !==
      `"${LEGACY_ICON_LIBRARIES[SOURCE_LIBRARY].import}"`
    ) {
      continue
    }

    for (const specifier of importDeclaration.getNamedImports() ?? []) {
      const iconName = specifier.getName()

      const targetedIcon = registryIcons[iconName]?.[targetLibrary]

      if (!targetedIcon || targetedIcons.includes(targetedIcon)) {
        continue
      }

      targetedIcons.push(targetedIcon)

      // Remove the named import.
      specifier.remove()

      // Replace with the targeted icon.
      sourceFile
        .getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement)
        .filter((node) => node.getTagNameNode()?.getText() === iconName)
        .forEach((node) => node.getTagNameNode()?.replaceWithText(targetedIcon))
    }

    // If the named import is empty, remove the import declaration.
    if (importDeclaration.getNamedImports()?.length === 0) {
      importDeclaration.remove()
    }
  }

  if (targetedIcons.length > 0) {
    const iconImportDeclaration = sourceFile.addImportDeclaration({
      moduleSpecifier:
        LEGACY_ICON_LIBRARIES[
          targetLibrary as keyof typeof LEGACY_ICON_LIBRARIES
        ]?.import,
      namedImports: targetedIcons.map((icon) => ({
        name: icon,
      })),
    })

    if (!_useSemicolon(sourceFile)) {
      iconImportDeclaration.replaceWithText(
        iconImportDeclaration.getText().replace(";", "")
      )
    }
  }

  return sourceFile
}

function _useSemicolon(sourceFile: SourceFile) {
  return (
    sourceFile.getImportDeclarations()?.[0]?.getText().endsWith(";") ?? false
  )
}

Subdomains

Dependencies

  • api
  • legacy-icon-libraries
  • transformers
  • ts-morph

Frequently Asked Questions

What does transform-legacy-icons.ts do?
transform-legacy-icons.ts is a source file in the ui codebase, written in typescript. It belongs to the FrameworkTooling domain, CLICore subdomain.
What functions are defined in transform-legacy-icons.ts?
transform-legacy-icons.ts defines 2 function(s): _useSemicolon, transformLegacyIcons.
What does transform-legacy-icons.ts depend on?
transform-legacy-icons.ts imports 4 module(s): api, legacy-icon-libraries, transformers, ts-morph.
Where is transform-legacy-icons.ts in the architecture?
transform-legacy-icons.ts is located at packages/shadcn/src/utils/transformers/transform-legacy-icons.ts (domain: FrameworkTooling, subdomain: CLICore, directory: packages/shadcn/src/utils/transformers).

Analyze Your Own Codebase

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

Try Supermodel Free