@@ -8,53 +8,88 @@ A codemod to add `displayName` to React function components.
88
99- Works with TypeScript or JavaScript source code.
1010- Quickly fix [ ` react/display-name ` ] ( https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/display-name.md ) lint errors.
11+ - Use ` displayName ` or a named function expression.
1112- Pass a path to a file, or a string.
1213
1314## Example
1415
15- ``` ts
16- import { modify } from ' @knighted/display-name'
16+ Given
1717
18- const codemod = await modify ( `
19- import { memo } from 'react'
18+ ``` tsx
19+ import React , { memo } from ' react'
2020
21- const Foo = () => {
22- return <div>foo</div>
23- }
21+ const Foo = memo (({ a }) => {
22+ return <>{ a } </>
23+ })
24+ const Bar = React .forwardRef ((props , ref ) => <p ref = { ref } >bar</p >)
25+ const Baz = memo (
26+ React .forwardRef ((props , ref ) => {
27+ return <p ref = { ref } >baz</p >
28+ }),
29+ )
30+ ```
2431
25- const Bar = memo(function Bar(props) {
26- return <span>stuff</span>
27- })
32+ Then running ` modify ` on the source code (or ` modifyFile ` on the file path) results in
2833
29- const Baz = memo(() => {
30- return <p>baz</p>
31- })
32- ` )
34+ ``` tsx
35+ import React , { memo } from ' react'
3336
34- console .log (codemod )
37+ const Foo = memo (({ a }) => {
38+ return <>{ a } </>
39+ })
40+ Foo .displayName = ' Foo'
41+ const Bar = React .forwardRef ((props , ref ) => <p ref = { ref } >bar</p >)
42+ Bar .displayName = ' Bar'
43+ const Baz = memo (
44+ React .forwardRef ((props , ref ) => {
45+ return <p ref = { ref } >baz</p >
46+ }),
47+ )
48+ ```
3549
36- /*
37- import { memo } from 'react'
50+ If running the codemod against a codebase that has recently added ` eslint-plugin-react ` you can write a script.
3851
39- const Foo = () => {
40- return <div>foo</div>
41- }
52+ ``` js
53+ import { globSync } from ' node:fs'
54+ import { writeFile } from ' node:fs/promises'
55+ import { modifyFile } from ' @knighted/display-name'
4256
43- const Bar = memo(function Bar(props) {
44- return <span>bar</span>
45- })
57+ const doCodeMod = async () => {
58+ for (const file of globSync (' **/*.tsx' , {
59+ exclude: [' **/node_modules/**' , ' **/dist/**' , ' **/build/**' ],
60+ })) {
61+ await writeFile (file, await modifyFile (file))
62+ }
63+ }
4664
47- const Baz = memo(() => {
48- return <p>baz</p>
49- })
50- Baz.displayName = 'Baz';
51- */
65+ await doCodeMod ()
5266```
5367
54- You can also pass a filepath instead of a string:
68+ Then optionally run the results through a formatter like ` prettier ` .
69+
70+ ## Options
71+
72+ 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.
5573
5674``` ts
57- import { modifyFile } from ' @knighted/display-name'
75+ type Options = {
76+ requirePascal? : boolean
77+ insertSemicolon? : boolean
78+ modifyNestedForwardRef? : boolean
79+ style? : ' displayName' | ' namedFuncExpr'
80+ }
81+ ` ` `
5882
59- await modifyFile (' /path/to/file.tsx' )
83+ For example, using ` namedFuncExpr `
84+
85+ ` ` ` tsx
86+ const Foo = memo (() => <>foo < / > )
87+ ```
88+
89+ becomes
90+
91+ ``` tsx
92+ const Foo = memo (function Foo() {
93+ return <>foo</>
94+ })
6095```
0 commit comments