Skip to content

fix: hooks returned by factory functions not linted #25066

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

Closed
wants to merge 5 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const tests = {
_use();
_useState();
use_hook();
// also valid because it's not matching the PascalCase namespace
// also valid because it's not matching the allow-listed namespaces
jest.useFakeTimer()
`,
`
Expand Down Expand Up @@ -406,6 +406,28 @@ const tests = {
const [myState, setMyState] = useState(null);
}
`,
{
code: `
// Valid because we can have a factory function that creates hooks

function createHooks() {
return {
foo: {
useQuery: () => {
data: 'foo.useQuery'
},
}
};
}
const myHooks = createHooks();

const MyComponent = () => {
const query = myHooks.foo.useQuery();

return <>{query.data}</>;
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -449,7 +471,7 @@ const tests = {
},
{
code: `
// This is a false positive (it's valid) that unfortunately
// This is a false positive (it's valid) that unfortunately
// we cannot avoid. Prefer to rename it to not start with "use"
class Foo extends Component {
render() {
Expand Down Expand Up @@ -956,6 +978,52 @@ const tests = {
`,
errors: [classError('useState')],
},
{
code: `
function createHooks() {
return {
useFoo: () => {
data: 'foo.useQuery'
},
};
}
const hooks = createHooks();

const MyComponent = () => {
if (a) {
return;
}
const query = hooks.useFoo();

return <>{query.data}</>;
}
`,
errors: [conditionalError('hooks.useFoo', true)],
},
Comment on lines +981 to +1002
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gotten this part to pass

{
code: `
function createHooks() {
return {
foo: {
useQuery: () => {
data: 'foo.useQuery'
},
}
};
}
const hooks = createHooks();

const MyComponent = () => {
if (a) {
return;
}
const query = hooks.foo.useQuery();

return <>{query.data}</>;
}
`,
errors: [conditionalError('hooks.useFoo.useQuery')],
},
],
};

Expand Down
13 changes: 11 additions & 2 deletions packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ function isHookName(s) {
return /^use[A-Z0-9].*$/.test(s);
}

/**
* Namespaces that are not considered hooks
*/
const nonHookNamespaces = ['jest'];

/**
* We consider hooks to be a hook name identifier or a member expression
* containing a hook name.
*/

function isHook(node) {
console.log('isHook', {
type: node.type,
name: node.name,
ishookName: isHookName(node.name),
});
if (node.type === 'Identifier') {
return isHookName(node.name);
} else if (
Expand All @@ -33,8 +43,7 @@ function isHook(node) {
isHook(node.property)
) {
const obj = node.object;
const isPascalCaseNameSpace = /^[A-Z].*/;
return obj.type === 'Identifier' && isPascalCaseNameSpace.test(obj.name);
return obj.type === 'Identifier' && !nonHookNamespaces.includes(obj.name);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I don't think the eslint rule should care if an object is PascalCased or not to decide whether the member is a hook or not.
  • I don't know if having a list of allow-listed namespaces is acceptable, nor if such an alternative then should be configurable.

} else {
return false;
}
Expand Down