Bbackstage

backstage/humanize-entity-ref-to-presentation

Backstage 1.50.0: Replace deprecated humanizeEntityRef/humanizeEntity with Catalog Presentation API

backstagemigrationcatalog-reacthumanizeEntityRefEntityDisplayName1.50.0
Public
3 executions

Run locally

npx codemod @backstage/humanize-entity-ref-to-presentation

@backstage/humanize-entity-ref-to-presentation

Backstage 1.50.0 migration codemod that replaces deprecated humanizeEntityRef and humanizeEntity from @backstage/plugin-catalog-react with the Catalog Presentation API.

What it does

Context-dependent replacement

The codemod detects the call-site context and applies the appropriate replacement:

ContextBeforeAfter
JSX expression{humanizeEntityRef(ref)}<EntityDisplayName entityRef={ref} />
React component bodyconst name = humanizeEntityRef(ref)const name = useEntityPresentation(ref).primaryTitle
Non-React / utilityhumanizeEntityRef(ref)entityPresentationSnapshot(ref).primaryTitle

Options mapping

  • defaultKind and defaultNamespace options are mapped to the context parameter for useEntityPresentation and entityPresentationSnapshot, or to JSX props for EntityDisplayName.

humanizeEntity

humanizeEntity(entity, fallbackName) follows the same context-dependent replacement. The fallback parameter is dropped since the Presentation API handles display name resolution automatically.

Import handling

  • Deprecated import specifiers are removed from the import statement
  • New imports (EntityDisplayName, useEntityPresentation, entityPresentationSnapshot) are added as needed based on the replacements used
  • Other specifiers from the same import statement are preserved
  • Aliased imports (e.g., import { humanizeEntityRef as formatRef }) are handled correctly

Re-exports

Re-exports like export { humanizeEntityRef } from '@backstage/plugin-catalog-react' are replaced with the new API exports.

Known limitations

  • Namespace imports (import * as CatalogReact from '...') are detected but not transformed — a warning is emitted for manual migration.
  • humanizeEntityRef returns string while EntityDisplayName returns JSX.Element. If the return value is used where only a string is accepted (e.g., passed to a function expecting string), the JSX replacement may cause type errors that need manual correction.
  • useEntityPresentation is a React hook and cannot be used outside React components or in conditional branches. The codemod only uses it inside functions that contain JSX.
  • Complex options expressions: If the options argument is a variable reference or computed expression rather than an inline object literal, the JSX props conversion for EntityDisplayName will not parse it.

Optional: AI fixup step

Enable the AI-powered fixup step with --param aiFixup=true to address edge cases:

  • Rewrites namespace imports
  • Fixes type compatibility issues where string was expected but JSX was substituted
  • Corrects mixed-context files where the wrong replacement was chosen

Installation

bash

Usage (from this repo)

bash

Development

From the repo root:

bash

Or from this package directory:

bash

License

MIT

Before

This is one example from the codemod's test cases. The codemod may handle many more cases.

Ready to contribute?

Build your own codemod and share it with the community.