Skip to content

Commit

Permalink
fix: support reacts' new "use" hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Geron committed Jan 6, 2024
1 parent 4d4ac25 commit 98a5610
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 8 additions & 1 deletion __tests__/require-usememo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,14 @@ describe('Rule - Require-usememo', () => {
const x = {};
return useData(x);
}`,
options: [{ checkHookReturnObject: true, ignoredHookCallsNames: {"useData": true} }],
options: [{ checkHookReturnObject: true, checkHookCalls: false }],
},
{
code: `
function useTesty() {
const x = {};
return use(x);
}`,
},
{
code: `
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/require-usememo.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Here is what each option means:

- `{ignoredHookCallsNames: Record<string, boolean>}`: This allows you to add specific hook names, thereby individually disabling or enabling them to be checked when used. Matching names with a `true` value will cause the checks to be ignored.
You can use strict 1:1 comparisons (e.g., `"useCustomHook"`) or employ Minimatch's Glob Pattern (e.g., `"useState*"`).
> React's [use](https://react.dev/reference/react/use) hook is ignored here by default, while triggering async calls dinamically losely within a component's render cycle is bad, it does not affect the overall "performance" or behavior of the hook itself. This can be disabled with a `"use": true` entry.
> For more information on Minimatch, refer to its README [here](https://www.npmjs.com/package/minimatch). You may also find this [cheatsheet](https://github.com/motemen/minimatch-cheat-sheet) useful.
> If no strict names match and you have entries with Glob syntax, the algorithm will stop at the first match.
Expand Down
9 changes: 8 additions & 1 deletion src/require-usememo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ export function shouldIgnoreNode(node: ESNode, ignoredNames: Record<string, bool

const matchedValue = {...ignoredNames, ...defaultReactHookNames}[nameToCheck];

// Checking for 1:1 matches
if (matchedValue != undefined) {
return matchedValue;
}

// This rule ignores React's "use" hook by default, more info in the rule's README
if (nameToCheck === 'use') {
return true;
}

// Checking via patterns
const shouldIgnore = Object.keys(ignoredNames).find(key => {
const value = ignoredNames[key];
const miniMatch = new Minimatch(key);
Expand Down Expand Up @@ -123,7 +130,7 @@ function addReactImports(context: Rule.RuleContext, kind: 'useMemo' | 'useCallba
export function getIsHook(node: TSESTree.Node | TSESTree.Identifier) {
if (node.type === "Identifier") {
const { name } = node;
return (name?.length ?? 0) >= 4 && name[0] === 'u' && name[1] === 's' && name[2] === 'e' && name[3] === name[3].toUpperCase();
return name === 'use' || ((name?.length ?? 0) >= 4 && name[0] === 'u' && name[1] === 's' && name[2] === 'e' && name[3] === name[3]?.toUpperCase?.());
} else if (
node.type === "MemberExpression" &&
!node.computed &&
Expand Down

0 comments on commit 98a5610

Please sign in to comment.