Navigation Menu

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle TSTypeReference in no-unused-prop-types #3195

Merged
merged 1 commit into from Feb 1, 2022

Conversation

niik
Copy link
Contributor

@niik niik commented Feb 1, 2022

馃憢 from GitHub Desktop land!

We're currently upgrading to TypeScript 4.5 (desktop/desktop#13768) and while doing that I wanted to start using the react/no-unused-props-type rule instead of the react-unused-props-and-state from the obsolete tslint-microsoft-contrib package.

Unfortunately I couldn't get the rule to produce any errors. I put up a minimal reproduction repository at https://github.com/niik/no-unused-prop-types-repro which illustrates the problem. To test it just clone and run yarn lint

Doing so very haphazardly troubleshooting I noticed that when encountering our prop types in propTypes.js they were of type TSTypeReference which was not being handled in the switch case. Spotting that DeclarePropTypesForTSTypeAnnotation seemed to be able to handle both TSTypeAnnotation and TSTypeReference internally I simply tried adding it to the switch and lo-and-behold the rule just started working.

Now, obviously I have no idea what I'm doing here so I'm happy to adjust my approach given some guidance from someone who understands eslint/ts-parser internals but running my minimal reproduction repository with my branch (https://github.com/niik/no-unused-prop-types-repro/tree/with-ts-type-reference-fix) reports the error correctly.

Co-authored-by: Markus Olsson <j.markus.olsson@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@ljharb
Copy link
Member

ljharb commented Feb 1, 2022

The fix looks great, but if we don't have a regression test, it will guaranteed be broken again in the future :-)

Your repro looks pretty small, so it should be straightforward to adapt it into a test case. Want to give it a shot?

@niik
Copy link
Contributor Author

niik commented Feb 1, 2022

Your repro looks pretty small, so it should be straightforward to adapt it into a test case. Want to give it a shot?

I'd be happy to but I'm not sure how I should go about it. There's already plenty of tests cases that cover this scenario in the no-unused-props-type rule tests (like https://github.com/yannickcr/eslint-plugin-react/blob/74634488e8682bc74e8d93e03b5ad70cf47551a9/tests/lib/rules/no-unused-prop-types.js#L5620-L5635). These tests will presumably break once this project upgrades to TS 4.5 (or whatever dependency it is that ends up leveraging the TS compiler types). Pointers on how such a test could be written would be much appreciated.

@ljharb
Copy link
Member

ljharb commented Feb 1, 2022

ahhh gotcha, it's the TS version specifically?

What version of the TS eslint parser are you using?

@ljharb
Copy link
Member

ljharb commented Feb 1, 2022

Looks like v5.10, and we're already testing on ^5. I'd hope they didn't change the name of the parse nodes in a minor version, but if they had I'd have expected to see failures sooner.

@codecov-commenter
Copy link

codecov-commenter commented Feb 1, 2022

Codecov Report

Merging #3195 (36482a3) into master (7463448) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #3195   +/-   ##
=======================================
  Coverage   97.53%   97.53%           
=======================================
  Files         120      120           
  Lines        8301     8302    +1     
  Branches     2987     2988    +1     
=======================================
+ Hits         8096     8097    +1     
  Misses        205      205           
Impacted Files Coverage 螖
lib/util/propTypes.js 97.42% <100.00%> (+<0.01%) 猬嗭笍

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update 7463448...36482a3. Read the comment docs.

@ljharb
Copy link
Member

ljharb commented Feb 1, 2022

I reran the passing tests on master from 3 days ago, and they continued to pass: https://github.com/yannickcr/eslint-plugin-react/actions/runs/1767687068 - so we do need some kind of new coverage, or possibly, to upgrade to TS 4.5.

That would imply it's a breaking change in TS tho.

@ljharb ljharb force-pushed the no-unused-prop-types-ts-4-5 branch from 90f1bc2 to 36482a3 Compare February 1, 2022 19:05
@ljharb
Copy link
Member

ljharb commented Feb 1, 2022

So, I couldn't make this test fail locally. Then, I installed different versions of TS and the TS eslint parser, and was able to make it fail. Then, I couldn't get it to NOT fail (absent the fix).

So, I guess we'll land it, but something weird is going on.

@ljharb ljharb merged commit 36482a3 into jsx-eslint:master Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants