-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins/x): add 'no-forward-ref' (#870)
- Loading branch information
Showing
10 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
packages/plugins/eslint-plugin-react-x/src/rules/no-forward-ref.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { ruleTester } from "../../../../../test"; | ||
import rule, { RULE_NAME } from "./no-forward-ref"; | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
invalid: [ | ||
{ | ||
code: /* tsx */ ` | ||
import { forwardRef } from 'react' | ||
forwardRef((props) => { | ||
return null; | ||
}); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import { forwardRef } from 'react' | ||
forwardRef((props) => null); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import { forwardRef } from 'react' | ||
forwardRef(function (props) { | ||
return null; | ||
}); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import { forwardRef } from 'react' | ||
forwardRef(function Component(props) { | ||
return null; | ||
}); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import * as React from 'react' | ||
React.forwardRef((props) => { | ||
return null; | ||
}); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import * as React from 'react' | ||
React.forwardRef((props) => null); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import * as React from 'react' | ||
React.forwardRef(function (props) { | ||
return null; | ||
}); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import * as React from 'react' | ||
React.forwardRef(function Component(props) { | ||
return null; | ||
}); | ||
`, | ||
errors: [{ messageId: "noForwardRef" }], | ||
settings: { | ||
"react-x": { | ||
version: "19.0.0", | ||
}, | ||
}, | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: /* tsx */ ` | ||
import * as React from 'react' | ||
React.forwardRef(function Component(props) { | ||
return null; | ||
}); | ||
`, | ||
settings: { | ||
"react-x": { | ||
version: "18.3.1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
import * as React from 'react' | ||
const Component = React.forwardRef((props, ref) => { | ||
return null; | ||
}); | ||
`, | ||
settings: { | ||
"react-x": { | ||
version: "18.3.1", | ||
}, | ||
}, | ||
}, | ||
/* tsx */ ` | ||
import * as React from 'react' | ||
const Component = ({ ref }) => { | ||
return null; | ||
}; | ||
`, | ||
/* tsx */ ` | ||
import * as React from 'react' | ||
const Component = ({ ref, ...props }) => { | ||
return null; | ||
}; | ||
`, | ||
], | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/plugins/eslint-plugin-react-x/src/rules/no-forward-ref.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { isForwardRefCall } from "@eslint-react/core"; | ||
import { decodeSettings, normalizeSettings } from "@eslint-react/shared"; | ||
import { compare } from "compare-versions"; | ||
import type { CamelCase } from "string-ts"; | ||
|
||
import { createRule } from "../utils"; | ||
|
||
export const RULE_NAME = "no-forward-ref"; | ||
|
||
export type MessageID = CamelCase<typeof RULE_NAME>; | ||
|
||
export default createRule<[], MessageID>({ | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "disallow the use of 'forwardRef'", | ||
}, | ||
messages: { | ||
noForwardRef: "In React 19, 'forwardRef' is no longer necessary. Pass 'ref' as a prop instead.", | ||
}, | ||
schema: [], | ||
}, | ||
name: RULE_NAME, | ||
create(context) { | ||
const { version } = normalizeSettings(decodeSettings(context.settings)); | ||
if (compare(version, "19.0.0", "<")) return {}; | ||
return { | ||
CallExpression(node) { | ||
if (!isForwardRefCall(node, context)) return; | ||
context.report({ | ||
messageId: "noForwardRef", | ||
node, | ||
}); | ||
}, | ||
}; | ||
}, | ||
defaultOptions: [], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# no-forward-ref | ||
|
||
## Rule category | ||
|
||
Restriction. | ||
|
||
## What it does | ||
|
||
Disallows using `React.forwardRef`. | ||
|
||
## Why is this bad? | ||
|
||
In React 19, `forwardRef` is no longer necessary. Pass `ref` as a prop instead. | ||
|
||
`forwardRef` will deprecated in a future release. Learn more [here](https://react.dev/blog/2024/12/05/react-19#ref-as-a-prop). | ||
|
||
## Examples | ||
|
||
### Failing | ||
|
||
```tsx | ||
import { forwardRef } from 'react'; | ||
|
||
const MyInput = forwardRef(function MyInput(props, ref) { | ||
// ... | ||
}); | ||
``` | ||
|
||
### Passing | ||
|
||
```tsx | ||
function MyInput({ ref, ...props }) { | ||
// ... | ||
} | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [React: APIs forwardRef](https://react.dev/reference/react/forwardRef) | ||
- [React: ref as a prop](https://react.dev/blog/2024/12/05/react-19#ref-as-a-prop) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters