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

Bug: eslint-plugin-react-hooks: Cannot read property 'references' of undefined #19043

Closed
Xiot opened this issue May 29, 2020 · 12 comments
Closed
Assignees
Labels
Resolution: Needs More Information Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@Xiot
Copy link

Xiot commented May 29, 2020

eslint-plugin-react-hooks is throwing a Cannot read property 'references' of undefined error on /node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1683:23

I added a console log statement before that line

var usedDep = dependencies.get(missingDep);
          console.log('usedDep', usedDep, missingDep, dependencies)
          if (usedDep.references[0].resolved !== topScopeRef) {   // error is thrown here

and this is the output.

usedDep undefined program?.slug Map {
  'program?.uuid' => { isStatic: false, references: [ [Reference] ] },
  'monitorService' => { isStatic: false, references: [ [Reference] ] },
  'navigate' => { isStatic: false, references: [ [Reference] ] },
  'program.slug' => { isStatic: false, references: [ [Reference] ] } }

I don't actually have program?.slug referenced anywhere in this file.
I am using program.slug though.

Versions:
eslint-plugin-react-hooks@4.0.4
eslint@6.8.0
babel-eslint@10.1.0

Steps To Reproduce

The code below throws the error.
I have found 2 ways to stop the error from happening.

  1. Removing the line programUuid: program?.uuid ?? '',
  2. adding program.slug to the list of dependencies.

Link to code example:

const onSave = useCallback(
    args => {
      const request: CreateMonitorV3Request = {
        programUuid: program?.uuid ?? '',        
      };

      monitorService
        .create(request)
        .then(x => {
          // $FlowFixMe - program will be available
          navigate(`/programs/${program.slug}/monitors/${x.uuid}`);
        })
        .catch(ex => {
          console.log('error', ex);
        });
    },
    [monitorService, program?.uuid]
  );

The current behavior

Error is thrown Cannot read property 'references' of undefined

The expected behavior

eslint successfully reports that I'm missing dependencies in useCallback

Full Stack trace:

TypeError: Cannot read property 'references' of undefined
Occurred while linting /project-root/src/pages/monitor/configuration-pages/new-monitor-page.js:26
    at /project-root/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1683:23
    at Set.forEach (<anonymous>)
    at visitFunctionWithDependencies (/project-root/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1674:29)
    at visitCallExpression (/project-root/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:826:11)
    at listeners.(anonymous function).forEach.listener (/project-root/node_modules/eslint/lib/linter/safe-emitter.js:45:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/project-root/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/project-root/node_modules/eslint/lib/linter/node-event-generator.js:254:26)
    at NodeEventGenerator.applySelectors (/project-root/node_modules/eslint/lib/linter/node-event-generator.js:283:22)
    at NodeEventGenerator.enterNode (/project-root/node_modules/eslint/lib/linter/node-event-generator.js:297:14)
@Xiot Xiot added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label May 29, 2020
@bvaughn
Copy link
Contributor

bvaughn commented May 29, 2020

I can't reproduce this error. Using the code example you pasted above:

const onSave = useCallback(
    args => {
      const request: CreateMonitorV3Request = {
        programUuid: program?.uuid ?? '',        
      };

      monitorService
        .create(request)
        .then(x => {
          // $FlowFixMe - program will be available
          navigate(`/programs/${program.slug}/monitors/${x.uuid}`);
        })
        .catch(ex => {
          console.log('error', ex);
        });
    },
    [monitorService, program?.uuid]
  );

I see:

Line 24:5: React Hook useCallback has missing dependencies: 'navigate' and 'program'. Either include them or remove the dependency array. If 'navigate' changes too often, find the parent component that defines it and wrap that definition in useCallback react-hooks/exhaustive-deps
Line 24:22: React Hook useCallback has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked react-hooks/exhaustive-deps

This is using both eslint-plugin-react-hooks@1.7.0 which is the version create-react-app installs and the latest version (v4.0.4) which I pulled in using a resolution:

  "resolutions": {
    "react-scripts/eslint-plugin-react-hooks": "^4.0.4"
  },

@bvaughn
Copy link
Contributor

bvaughn commented May 29, 2020

Which Node version are you using? (I'm using v12.16.2 and I also tested v10.20.1 without problem.)

@Xiot
Copy link
Author

Xiot commented May 29, 2020

10.16.3
I'm also using flow
eslint-plugin-flowtype@4.7.0

My guess is that its a mismatch between the flow and react-hooks linter.

in this instance program is resolved to the ?Program type.
So it looks like its adding program?.slug to handle the case where program == null however program?.slug isn't found as a dependency.

The issue probably comes down to the fact that I'm referencing program?.uuid and program.slug (without the optional). Changing the line with navigate to use program?.slug avoids the error.

In my case I know for fact that program will always have a value, which is why I have that $FlowFixMe line.

Also, if I change the programUuid: program?.uuid ?? '', line to programUuid: program.uuid ?? '', (removed the ?) then the plugin suggests that I add the dependencies without the ? (ie, program.uuid, program.slug)

@yanneves
Copy link
Contributor

yanneves commented May 29, 2020

adding program.slug to the list of dependencies.

from looking at your code example, you should have program.slug defined in your dependencies anyway, I believe

@Xiot
Copy link
Author

Xiot commented May 29, 2020

Ya, my dependencies were incorrect, however ideally there shouldn't be an exception when identify the missing dependencies.

@yanneves
Copy link
Contributor

Agreed, I could see some similar code used for useEffect - this should be addressable with #18926 to add support for optional chaining in useCallback

@nathggns
Copy link

nathggns commented Jun 2, 2020

I'm also getting this.

@coolkev
Copy link
Contributor

coolkev commented Jun 3, 2020

The suggested edit I made on PR #19062 (comment) will fix this issue.

@fredvollmer
Copy link
Contributor

Thanks to @coolkev I just updated #19062 to include a fix for this issue.

@tcodes0
Copy link

tcodes0 commented Jun 18, 2020

@bvaughn hey, not sure if labels matter or who takes care of them, but this looks like it has a PR open to address it, so it would be confirmed label or something like that? Just saying bc I'm having this issue and by the labels on a quick glance it looks like it's not fixed when in reality it's just pending a merge afaik

gaearon pushed a commit that referenced this issue Jun 30, 2020
…ing prototype method inside useCallback and useMemo #19061 (#19062)

* fix(eslint-plugin-react-hooks): Support optional chaining when accessing prototype method #19061

* run prettier

* Add fix for #19043
@krailler
Copy link

krailler commented Jul 1, 2020

I have this issue too on eslint-plugin-react-hooks@4.0.5

@bvaughn
Copy link
Contributor

bvaughn commented Jul 1, 2020

PR #19062 was supposed to fix this issue. It was merged and released in 4.0.5 but the issue was accidentally left open.

If folks are seeing a similar bug, please file a new issue with repro information. I'm going to close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Needs More Information Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

8 participants