Home / Function/ transformLegacyIcons() — ui Function Reference

transformLegacyIcons() — ui Function Reference

Architecture documentation for the transformLegacyIcons() function in transform-legacy-icons.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  464dfa3f_21f3_f62f_75f8_c684b40ec7e9["transformLegacyIcons()"]
  4573b0fc_4d11_04f0_3914_a6d333cddb17["transform-legacy-icons.ts"]
  464dfa3f_21f3_f62f_75f8_c684b40ec7e9 -->|defined in| 4573b0fc_4d11_04f0_3914_a6d333cddb17
  8bae7fed_5a2e_4965_2852_5cd6295f8109["_useSemicolon()"]
  464dfa3f_21f3_f62f_75f8_c684b40ec7e9 -->|calls| 8bae7fed_5a2e_4965_2852_5cd6295f8109
  style 464dfa3f_21f3_f62f_75f8_c684b40ec7e9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/utils/transformers/transform-legacy-icons.ts lines 9–81

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
}

Subdomains

Frequently Asked Questions

What does transformLegacyIcons() do?
transformLegacyIcons() is a function in the ui codebase, defined in packages/shadcn/src/utils/transformers/transform-legacy-icons.ts.
Where is transformLegacyIcons() defined?
transformLegacyIcons() is defined in packages/shadcn/src/utils/transformers/transform-legacy-icons.ts at line 9.
What does transformLegacyIcons() call?
transformLegacyIcons() calls 1 function(s): _useSemicolon.

Analyze Your Own Codebase

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

Try Supermodel Free