Socket
Book a DemoInstallSign in
Socket

@knighted/display-name

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@knighted/display-name

Codemod to add a React displayName to function components.

1.0.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

@knighted/display-name

CI codecov NPM version

A codemod to add displayName to React function components.

  • Works with TypeScript or JavaScript source code.
  • Quickly fix react/display-name lint errors.
  • Use displayName or a named function expression.
  • Pass a path to a file, or a string.

Example

Given

import React, { memo } from 'react'

const Foo = memo(({ a }) => {
  return <>{a}</>
})
const Bar = React.forwardRef((props, ref) => <p ref={ref}>bar</p>)
const Baz = memo(
  React.forwardRef((props, ref) => {
    return <p ref={ref}>baz</p>
  }),
)

Then running modify on the source code (or modifyFile on the file path) results in

import React, { memo } from 'react'

const Foo = memo(({ a }) => {
  return <>{a}</>
})
Foo.displayName = 'Foo'
const Bar = React.forwardRef((props, ref) => <p ref={ref}>bar</p>)
Bar.displayName = 'Bar'
const Baz = memo(
  React.forwardRef((props, ref) => {
    return <p ref={ref}>baz</p>
  }),
)

If running the codemod against a codebase that has recently added eslint-plugin-react you can write a script.

import { globSync } from 'node:fs'
import { writeFile } from 'node:fs/promises'
import { modifyFile } from '@knighted/display-name'

const doCodeMod = async () => {
  for (const file of globSync('**/*.tsx', {
    exclude: ['**/node_modules/**', '**/dist/**', '**/build/**'],
  })) {
    await writeFile(file, await modifyFile(file))
  }
}

await doCodeMod()

Then optionally run the results through a formatter like prettier.

Options

There are some options, none are required. Most notably you can choose a style for adding the display name. The default is displayName which adds a displayName property to the function component, or you can choose namedFuncExpr to use a named function expression instead.

type Options = {
  requirePascal?: boolean
  insertSemicolon?: boolean
  modifyNestedForwardRef?: boolean
  style?: 'displayName' | 'namedFuncExpr'
}

For example, using namedFuncExpr

const Foo = memo(() => <>foo</>)

becomes

const Foo = memo(function Foo() {
  return <>foo</>
})

Keywords

codemod

FAQs

Package last updated on 11 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.