Skip to content
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

Fix: simplify check for supported hooks #48

Merged
merged 3 commits into from
Jun 10, 2024
Merged
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
15 changes: 7 additions & 8 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,16 @@ export function isInReactHooks(node, returnHook = false) {
return hookDef ? true : false
}

function isReactPrimitive(node) {
if (
node.type === 'Identifier' &&
(node.name === 'useEffect' || node.name === 'useCallback')
) {
return true
function isInSupportedReactPrimitives(node) {
const supportedPrimitives = ['useEffect', 'useCallback', 'useMemo']

if (node.type === 'Identifier') {
return supportedPrimitives.includes(node.name)
}

if (node.type === 'MemberExpression') {
const flatExpr = flattenMemberExpression(node)
return flatExpr.endsWith('useEffect') || flatExpr.endsWith('useCallback')
return supportedPrimitives.some((d) => flatExpr.endsWith(d))
}

return false
Expand All @@ -191,7 +190,7 @@ export function getNearestHook(node) {
return false
}

if (!isReactPrimitive(parentCaller.callee)) {
if (!isInSupportedReactPrimitives(parentCaller.callee)) {
return getNearestHook(parentCaller)
}

Expand Down
29 changes: 29 additions & 0 deletions tests/StateSnapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,35 @@ ruleTester.run('state-snapshot-rule', rule, {
</>
);
})`,
`
function useExample2(s) {
const {b: {c} } = useSnapshot(s.a1);
const val = useMemo(()=>{
return s.a1.b.c
},[s.a1.b.c])
}`,
Comment on lines +302 to +307
Copy link
Member

Choose a reason for hiding this comment

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

That's actually not good.
The deps should come from snapshots.

  function useExample2(s) {
    const {b: {c} } = useSnapshot(s.a1);
    const val = useMemo(()=>{
      return c
    },[c])
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

`
function useExample3(s) {
const {b: {c} } = useSnapshot(s.a1);
const val = useMemo(()=>{
return c
},[c])
}`,
`
const foo = proxy({ foo: 123 });
function useExample2(s) {
const val = useMemo(()=>{
return foo.foo
},[foo.foo])
}`,
Comment on lines +316 to +321
Copy link
Member

Choose a reason for hiding this comment

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

In addition to that, this is valid too.

  const foo = proxy({ foo: 123 });
  function useExample2(s) {
    const val = useMemo(()=>{
      return foo.foo
    },[foo])
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

`
const foo = proxy({ foo: 123 });
function useExample2(s) {
const val = useMemo(()=>{
return foo.foo
},[foo])
}
`,
],
invalid: [
{
Expand Down
Loading