
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
@knighted/display-name
Advanced tools
Codemod to add a React displayName to function components.
@knighted/display-name
A codemod to add displayName
to React function components.
react/display-name
lint errors.displayName
or a named function expression.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
.
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</>
})
FAQs
Codemod to add a React displayName to function components.
We found that @knighted/display-name demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.