Skip to content

Support react-anonymous in rules of hooks #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/eslint-plugin-react-hooks/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# `eslint-plugin-react-hooks`
# `@lewisl9029/eslint-plugin-react-hooks`

This is a fork of the official eslint plugin `eslint-plugin-react-hooks`, that adds support for the [react-anonymous](https://github.com/lewisl9029/react-anonymous#readme) library/pattern.

This ESLint plugin enforces the [Rules of Hooks](https://reactjs.org/docs/hooks-rules.html).

Expand All @@ -12,10 +14,10 @@ Assuming you already have ESLint installed, run:

```sh
# npm
npm install eslint-plugin-react-hooks --save-dev
npm install @lewisl9029/eslint-plugin-react-hooks --save-dev

# yarn
yarn add eslint-plugin-react-hooks --dev
yarn add @lewisl90290/eslint-plugin-react-hooks --dev
```

Then extend the recommended eslint config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,24 @@ const tests = {
const [myState, setMyState] = useState(null);
}
`,
`
// Valid because called in Anonymous component
function App(props) {
return props.isOpen
? <Anonymous>
{() => <Modal close={useCallback(() => props.setIsOpen(false), [props.setIsOpen])} />}
</Anonymous>
: null;
}
`,
`
// Valid because called in anonymous function
function App(props) {
return props.isOpen
? anonymous(() => <Modal close={useCallback(() => props.setIsOpen(false), [props.setIsOpen])} />)
: null;
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -956,6 +974,34 @@ const tests = {
`,
errors: [classError('useState')],
},
{
code: `
// Invalid because rule of hooks still need to be adhered to within Anonymous component
function App(props) {
return props.isOpen
? <Anonymous>
{() => {
return <Modal close={props.setIsOpen ? useCallback(() => props.setIsOpen(false), [props.setIsOpen]) : undefined} />;
}}
</Anonymous>
: null;
}
`,
errors: [conditionalError('useCallback')],
},
{
code: `
// Invalid because rule of hooks still need to be adhered to within anonymous function
function App(props) {
return props.isOpen
? anonymous(() => {
return <Modal close={props.setIsOpen ? useCallback(() => props.setIsOpen(false), [props.setIsOpen]) : undefined} />;
})
: null;
}
`,
errors: [conditionalError('useCallback')],
},
],
};

Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin-react-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eslint-plugin-react-hooks",
"description": "ESLint rules for React Hooks",
"version": "4.5.0",
"name": "@lewisl9029/eslint-plugin-react-hooks-for-react-anonymous",
"description": "Fork of ESLint rules for React Hooks for react-anonymous",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "https://github.com/facebook/react.git",
Expand Down
23 changes: 22 additions & 1 deletion packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ function isInsideComponentOrHook(node) {
return false;
}

function isDirectlyInsideAnonymousComponent(node) {
if (!node.parent) {
return false;
}

const isDirectlyInsideHooksComponent =
node.parent.type === 'JSXExpressionContainer' &&
node.parent.parent &&
node.parent.parent.type === 'JSXElement' &&
node.parent.parent.openingElement.name.name === 'Anonymous';
const isDirectlyInsideHooksFunction =
node.parent.type === 'CallExpression' &&
node.parent.callee &&
node.parent.callee.type === 'Identifier' &&
node.parent.callee.name === 'anonymous';

return isDirectlyInsideHooksComponent || isDirectlyInsideHooksFunction;
}

export default {
meta: {
type: 'problem',
Expand Down Expand Up @@ -352,7 +371,9 @@ export default {
const isDirectlyInsideComponentOrHook = codePathFunctionName
? isComponentName(codePathFunctionName) ||
isHook(codePathFunctionName)
: isForwardRefCallback(codePathNode) || isMemoCallback(codePathNode);
: isForwardRefCallback(codePathNode) ||
isMemoCallback(codePathNode) ||
isDirectlyInsideAnonymousComponent(codePathNode);

// Compute the earliest finalizer level using information from the
// cache. We expect all reachable final segments to have a cache entry
Expand Down