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 TypeError: Cannot read properties of null (reading 'parent') within isInCustomHookDef #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

peterpme
Copy link

@peterpme peterpme commented Aug 6, 2024

Hello! I've discovered an issue with eslint-plugin-valtio in the following scenario:

type MyValue = any;
function MyFunction() {
  const id = useSnapshot(store) // normal usage of valtio snapshot
  const [value, setValue] = useState({});
  
  useQuery({
    queryKey: ['test'],
    queryFn: () => {
      setValue(prev) => {
        return {
          ...prevent,
          [id]: value as MyValue //breaks on this line
        }
      }
    },
  });
  
}

The error I'm receiving is:

TypeError: Cannot read properties of null (reading 'parent')
Occurred while linting MyComponent.tsx:91
Rule: "valtio/state-snapshot-rule"
    at isInCustomHookDef (MyProject/node_modules/eslint-plugin-valtio/index.js:238:60)
    at Identifier (MyProject/node_modules/eslint-plugin-valtio/index.js:346:92)

By checking if these nodes exist, I'm able to fix the issue. Thanks!

@dai-shi dai-shi requested a review from barelyhuman August 6, 2024 14:50
@barelyhuman
Copy link
Collaborator

Don’t have my laptop so this is based off what i could read on the phone screen.

  1. instead of returning false, we’d like to add a check for if it’s in a function inside an object inside a custom hook

reasoning is that we might have further cases that might come up in the future and the plugin would return false for everything

if that makes sense, please make the changes accordingly if not, i’ll pick this up once i have fixed my work machine 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants