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
feat(eslint-plugin): add rule no-unused-type-properties
#1936
feat(eslint-plugin): add rule no-unused-type-properties
#1936
Conversation
Thanks for the PR, @eltonio450! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
Codecov Report
@@ Coverage Diff @@
## master #1936 +/- ##
==========================================
+ Coverage 94.36% 94.39% +0.03%
==========================================
Files 166 167 +1
Lines 7608 7657 +49
Branches 2185 2198 +13
==========================================
+ Hits 7179 7228 +49
Misses 183 183
Partials 246 246
|
Hello, @bradzacher what is the next step ? I think a good first one would be you to tell me if we should include interfaces in this rule : interface MyProps {
a: string,
b: string //forgotten and now unused props
}
// Error: Destructuring does not use all of the properties of the interface MyProps, you should either remove the properties from the type or use Omit<MyProps, ...> to explicitly exclude the properties.',
const MyComp = ({ a } : MyProps) => {
return <div>{a}</div>
} I lack a bit of experience to really see if it fits in the spirit of a typescript rule. If yes, I need a few more days to adapt the rule and messages Thank you |
oopsi, sorry, I did not mean to be pushy or so 😢 , the question was real, and since the last time it took me two months to address your advice, I was like, ok, let's ask in advance this time 😄 if you have time in advance to give a piece of advice on this interface thing, great, if not, it will wait for the complete review 👍 thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this rule belongs inside this plugin.
It's very specific to react
- whilst there are non-react use cases that where this would apply, I think the vast majority would be react components.
I think it would be better as a rule in eslint-plugin-react
:
- it would reach more people that would be specifically interested in the rule (the react plugin has 6m weekly downloads that are obviously react users, whereas we have 5m weekly downloads that are a mixture of front-end/backend and frameworks).
- it could then be generalised to handle
flow
as well (the two aren't so different), further increasing its reach.
That being said, answering your questions:
about the requiresTypechecking options : I am not really sure if I have to enable it or not...
You only need to enable it if you're using type information.
If you have to ask, you're not using it 😄
I could not really test my rule by linking @typescript-eslint/eslint-plugin package in some of my projects, did I miss something ? Should it work as any other package ?
I've never linked it, but it should just work after a build.
I saw that many rules don't use arrow function : is there a reason ? Should I comply to this or it does not matter ?
In this codebase, we tend to prefer function declarations where possible.
are there some shared AST_NODE_TYPES typecheckers somewhere in the package ?
What do you mean by this?
I am not sure which case should I handle. For example, should I handle Omit or not ? Does this require to enable the requiresTypeChecking option ?
Omit
(well, any utility type) would require type information to handle correctly.
Any imported type would require type information to handle correctly.
I did not include the case where the type is exported, not sure if it makes sense
I think it still makes sense to report on this case.
The properties are for this component, and you don't want someone to provide extra properties to the component unless they're used.
Exporting the type just means others will consume the type, but they shouldn't dictate whether or not the component declares unused props.
should I include interface declaration in the check ?
Definitely - the rule should handle both type declarations and interface declarations.
Good guess for the eslint react repo, thank you :) : I did not know we could write typescript specific rules, but yes, it's possible ! And even better, a rule for this use case exists: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-unused-prop-types.md (I thought it was specific to dynamic propTypes 😢, but the doc has been clarified lately ) Turns it it does not work (jsx-eslint/eslint-plugin-react#2569) but that's another story I guess As for the PR, is it worth finalizing it based on your remarks ? I think the path is pretty clear now, thank you for the information, but does it add value ? If yes, I'll try do finish asap For the type checkers, I have three functions in my PR :
And I was wondering if I missed some shared utilities somewhere Thank you ! |
Oh I get you, no we don't have ones for those. All utilities are available via the We can always add more to https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/experimental-utils/src/ast-utils/predicates.ts if we want those.
I don't think it does make sense to progress with this PR. Chances are you could probably also discuss with the maintainers and consume type information in that rule to make it more accurate (after first fixing the bug) |
ok, closing then :) thank you for your help along the process 👍 |
Hello everyone,
I tried to implement a rule to disallow unused type properties (#1529)
Our use case is to catch forgotten props when refactoring a React component, but I guess there are plenty of others. I included the example in the doc, but I copy paste it here:
This is my first rule, so this PR is a draft (but working !).
I have a few questions :
Config:
Coding style:
The rule itself
I hope this rule makes sense and will be merged someday, feel free to share your remarks, I'll be glad to make the corrections !
All the best
[EDIT : deleting some questions, as I have found the answer while trying to increase coverage :)]