jsx-eslint / eslint-plugin-react

React-specific linting rules for ESLint
MIT License
8.86k stars 2.75k forks source link

[Fix] Null check `rootNode` before calling `getScope` with it #3762

Closed crnhrv closed 1 month ago

crnhrv commented 1 month ago

I'm getting this error while linting some valid React/TS code after upgrading to the latest version (7.34.2):

Stack trace ``` Oops! Something went wrong! :( ESLint: 8.57.0 TypeError: Missing required argument: node. Occurred while linting ./OptionsTable.tsx:599 Rule: "react/display-name" at SourceCode.getScope (./node_modules/eslint/lib/source-code/source-code.js:773:19) at getScope (./node_modules/eslint-plugin-react/lib/util/eslint.js:19:23) at resolveValueForIdentifierNode (./node_modules/eslint-plugin-react/lib/util/propTypes.js:378:21) at buildReactDeclarationTypes (./node_modules/eslint-plugin-react/lib/util/propTypes.js:420:5) at ./node_modules/eslint-plugin-react/lib/util/propTypes.js:788:39 at iterateProperties (./node_modules/eslint-plugin-react/lib/util/propTypes.js:71:7) at ./node_modules/eslint-plugin-react/lib/util/propTypes.js:773:23 at Array.forEach () at DeclarePropTypesForTSTypeAnnotation.convertReturnTypeToPropTypes (./node_modules/eslint-plugin-react/lib/util/propTypes.js:762:34) at DeclarePropTypesForTSTypeAnnotation.searchDeclarationByName (./node_modules/eslint-plugin-react/lib/util/propTypes.js:667:14) ```

I don't really understand enough about the codebase to say whether this is the correct fix, or if this really should be passing in node instead of rootNode to getScope or something like that. This resolves the immediate issue for me at least and seemed reasonable to check the value is valid before it tries to call something that requires it.

ljharb commented 1 month ago

Thanks - we'd need a regression test in order to land this tho.

crnhrv commented 1 month ago

@ljharb I've attempted to add some regression tests by trying to find a minimal reproduction. I think the issue relates to using ReturnType<typeof object> for the type parameter for a component, but I don't really know the details.

I get the expected failure without the nullcheck:

ishare-1717159133

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 97.76%. Comparing base (417e1ca) to head (f877a50).

:exclamation: Current head f877a50 differs from pull request most recent head e27ef81

Please upload reports for the commit e27ef81 to get more accurate results.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #3762 +/- ## ========================================== - Coverage 97.79% 97.76% -0.04% ========================================== Files 134 134 Lines 9613 9614 +1 Branches 3486 3487 +1 ========================================== - Hits 9401 9399 -2 - Misses 212 215 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

crnhrv commented 1 month ago

I see that code coverage is failing on some indirect changes related to code beneath a node.type === 'TSParenthesizedType' conditional. Not sure why that would happen, but is this code actually necessary since TSParenthesizedType seems to no longer be generated as a node type?

ljharb commented 1 month ago

@crnhrv that PR seems to be in v5 of the parser, but we still support down to v2

crnhrv commented 1 month ago

Cool, thanks for the help. Will leave it with you then