Skip to content

Commit 28af27b

Browse files
authored
fix(no-forward-ref): loose fix (#925)
1 parent 4ff5dfb commit 28af27b

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-forward-ref.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ ruleTester.run(RULE_NAME, rule, {
191191
interface ComponentProps {
192192
foo: string;
193193
}
194-
const Component = function Component({ ref, ...props }: ComponentProps & { ref: React.RefObject<HTMLElement> }) {
194+
const Component = function Component({ ref, ...props }: ComponentProps & { ref?: React.RefObject<HTMLElement | null> }) {
195195
return <div ref={ref} />;
196196
};
197197
`,
@@ -211,7 +211,7 @@ ruleTester.run(RULE_NAME, rule, {
211211
errors: [{ messageId: "noForwardRef" }],
212212
output: /* tsx */ `
213213
import * as React from 'react'
214-
const Component = function Component({ ref, ...props }: { foo: string } & { ref: React.RefObject<HTMLElement> }) {
214+
const Component = function Component({ ref, ...props }: { foo: string } & { ref?: React.RefObject<HTMLElement | null> }) {
215215
return <div ref={ref}>{props.foo}</div>;
216216
};
217217
`,
@@ -231,7 +231,7 @@ ruleTester.run(RULE_NAME, rule, {
231231
errors: [{ messageId: "noForwardRef" }],
232232
output: /* tsx */ `
233233
import * as React from 'react'
234-
const Component = function Component({ ref, foo }: { foo: string } & { ref: React.RefObject<HTMLElement> }) {
234+
const Component = function Component({ ref, foo }: { foo: string } & { ref?: React.RefObject<HTMLElement | null> }) {
235235
return <div ref={ref}>{foo}</div>;
236236
};
237237
`,
@@ -251,7 +251,7 @@ ruleTester.run(RULE_NAME, rule, {
251251
errors: [{ messageId: "noForwardRef" }],
252252
output: /* tsx */ `
253253
import * as React from 'react'
254-
const Component = function Component({ ref: r, foo }: { foo: string } & { ref: React.RefObject<HTMLElement> }) {
254+
const Component = function Component({ ref: r, foo }: { foo: string } & { ref?: React.RefObject<HTMLElement | null> }) {
255255
return <div ref={r}>{foo}</div>;
256256
};
257257
`,
@@ -271,7 +271,7 @@ ruleTester.run(RULE_NAME, rule, {
271271
errors: [{ messageId: "noForwardRef" }],
272272
output: /* tsx */ `
273273
import * as React from 'react'
274-
const Component = function Component({ ref: r, foo, ...rest }: { foo: string, bar: number } & { ref: React.RefObject<HTMLElement> }) {
274+
const Component = function Component({ ref: r, foo, ...rest }: { foo: string, bar: number } & { ref?: React.RefObject<HTMLElement | null> }) {
275275
return <div ref={r}>{foo}</div>;
276276
};
277277
`,

packages/plugins/eslint-plugin-react-x/src/rules/no-forward-ref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ function getComponentPropsFixes(
131131
getText(typeArg1),
132132
"&",
133133
"{",
134-
`ref:`,
135-
`React.RefObject<${getText(typeArg0)}>`,
134+
`ref?:`,
135+
`React.RefObject<${getText(typeArg0)} | null>`,
136136
"}",
137137
].join(" "),
138138
),

0 commit comments

Comments
 (0)