fix[eslint-plugin-react-hooks]: Fix error when callback argument is an identifier with an`as` expression
Summary
Using eslint-plugin-react-hooks with ESLint v9 and TypeScript ESLint v8 causes the following issue:
Oops! Something went wrong! :(
ESLint: 9.11.1
TypeError: Cannot read properties of null (reading 'upper')
Occurred while linting /app/src/hooks/useCustomMemo.tsx:9
Rule: "react-hooks/exhaustive-deps"
at visitFunctionWithDependencies (/app/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1106:38)
at CallExpression (/app/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1920:25)
at ruleErrorHandler (/app/node_modules/eslint/lib/linter/linter.js:1084:48)
at /app/node_modules/eslint/lib/linter/safe-emitter.js:45:58
at Array.forEach (<anonymous>)
at Object.emit (/app/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
at NodeEventGenerator.applySelector (/app/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
at NodeEventGenerator.applySelectors (/app/node_modules/eslint/lib/linter/node-event-generator.js:326:22)
at NodeEventGenerator.enterNode (/app/node_modules/eslint/lib/linter/node-event-generator.js:337:14)
at runRules (/app/node_modules/eslint/lib/linter/linter.js:1128:40)
I can reproduce this with the above mentioned versions and the following file:
function useCustomCallback(callback, deps) {
return useCallback(callback as any, deps)
}
How did you test this change?
Added a test case.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| react-compiler-playground | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Nov 18, 2024 7:05pm |
Comparing: c866d7506086140aaa79a015f4a7b2cf20c51905...3780588fd32407ac45b02a1eadabcd3c4647d6fb
Critical size changes
Includes critical production bundles, as well as any change greater than 2%:
| Name | +/- | Base | Current | +/- gzip | Base gzip | Current gzip |
|---|---|---|---|---|---|---|
| oss-stable/react-dom/cjs/react-dom.production.js | = | 6.68 kB | 6.68 kB | +0.05% | 1.83 kB | 1.83 kB |
| oss-stable/react-dom/cjs/react-dom-client.production.js | = | 509.94 kB | 509.94 kB | = | 91.24 kB | 91.24 kB |
| oss-experimental/react-dom/cjs/react-dom.production.js | = | 6.69 kB | 6.69 kB | = | 1.83 kB | 1.83 kB |
| oss-experimental/react-dom/cjs/react-dom-client.production.js | = | 514.88 kB | 514.88 kB | = | 91.95 kB | 91.95 kB |
| facebook-www/ReactDOM-prod.classic.js | = | 594.55 kB | 594.55 kB | = | 104.89 kB | 104.90 kB |
| facebook-www/ReactDOM-prod.modern.js | = | 584.81 kB | 584.81 kB | = | 103.32 kB | 103.32 kB |
Significant size changes
Includes any change greater than 0.2%:
Expand to show
Generated by :no_entry_sign: dangerJS against f67314291f4a20ec31b62f657bd6f2a1eb98ca37
@eps1lon I tried adding my code example as a test, and with the versions of eslint, typescript, and typescript-eslint in this repo, it worked fine. It's only with the versions I specified that this is an issue, and not sure exactly the best way to test that. I noticed there is configuration for testing various versions of eslint and typescript-eslint, but not typescript. I'll do some more investigation on my end and see what I can come up with.
@eps1lon I was able to build a test case that did error with the current version of the plugin which I added to this PR, and also updated the fix to properly handle this case so it does error with this code example.