-
Notifications
You must be signed in to change notification settings - Fork 199
[react-unused-props-and-state] add support for traversing function components #824
[react-unused-props-and-state] add support for traversing function components #824
Conversation
Haven't chance to look at PR in details yet. |
@IllusionMH thanks for the tip - I'll put that on my to-do list |
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.
This is a solid start to one of the most frequently requested improvements in this repo, thanks @shiftkey! 🙌
I think it can be generalized a bit to handle a few more cases.
This seems like a pretty reasonable approach. This rule is an unusually crusty one, which seems inevitable given how vague the definition of what a "component" is. We're very fortunate to have a precedent of props interfaces being named I think it could later be improved by also considering any arrow function, function declaration, or function expression that takes in a single object and returns anything assignable to
Yeah the build process for this package is a little wonky. 7.0 will, as a breaking change, remove a lot of older packaging that is no longer relevant, so it'll be possible to switch to in-place file compiles (in other words: no longer need to cd ~/Code/repos/tslint-microsoft-contrib/src
tsc --outDir ~/Code/repos/some-other-project/node_modules/tslint-microsoft-contrib ...but you might have to fiddle around with compiler settings to get that to work. Personally I just hand-edit what's in |
I think this is ready for another round of review |
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.
Looks great! Just waiting on test cases for the other type names.
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.
💯, this is excellent. Thanks @shiftkey!
We'll be cutting a release of tslint-microsoft-contrib this week to get this & some other changes out.
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.
PR is really great! I like updated and detailed test suite 👍
Looks like there only are couple of outdated comments in current code (1 has inline mention, and one in inside cb
function) otherwise code looks good.
However there are two cases that I see regularly and are not supported now:
- Props destructuring inside function
- "Renames" in named parameters (see example below)
@shiftkey would you like to look at these cases in scope this PR or should they be handled in separate PRs?
Code example: "Renames" in named parameters for render components
In this case I have to "rename" labelComponent
to Label
, otherwise React will treat it as regular HTML tag.
import React from 'react';
type MetaFields = {
label: string;
value: string;
};
interface Props {
items: ReadonlyArray<MetaFields>;
labelComponent: React.ComponentType<{ text: string }>; // incorrectly marked as unused
}
export const Meta: React.FC<Props> = ({ items, labelComponent: Label }: Props) => (
<ul>
{items.map(({ label, value }) => (
<li key={label}>
<Label text={label} />
<span>{value}</span>
</li>
))}
</ul>
);
@IllusionMH lets open a fresh issue or two to capture these other gaps - I'd rather not hold up getting this into an update for the new cases it currently covers. |
@shiftkey OK, I'll create separate issues for mentioned cases. Thanks you for this contribution! |
…mponents (microsoft#824) * added suite of tests to cover function components * traverse found arrow functions, function declarations and expressions * add helper functions to extract type information * wireup logic to traverse arrow functions and function components * reorganized tests to help with understanding coverage * check shorthand types for React function components * update comment
PR checklist
Overview of change:
This is an attempt to support finding unused props in function components, based on the usage I've seen in the wild and figuring out how to traverse them properly. Fair warning: this is my first real contribution to anything related using the TypeScript AST in anger, so I want to caveat this with:
Jokes aside, I focused on these two general cases reported in #339.
First, an arrow function that returns a stateless function component. We can check more cases here, like a type literal being used rather than a type reference, because of the context that the presence of the
React.SFC
provides:Second, function declarations which have a parameter matching the regex defined in config:
I avoided being as flexible as possible because it might incorrectly traverse all functions in a module which have one parameter.
These changes should be additive to how class components are traversed. I didn't want to touch that flow despite the decent suite of tests covering it. Because I wanted these changes to be additive I introduced the
callbackForFunctionComponentNodes
to traverse the interesting parts of these arrow functions and function declarations and find any props usage.TODO:
React.FunctionComponent
as well asReact.SFC
(SFC
is deprecated in the latest@types/react
package)Is there anything you'd like reviewers to focus on?
Three things from the reviewers:
tslint
cp dist/build/*.js ../other-project/node_modules/tslint-microsoft-contrib/
worked for me after b1e907a - is there a better way to do this?the core of this change was about how property expression worked, and I'd like to mergefixed in 9977484callbackForFunctionComponentNodes
back into the maincb
callback to be consistent but haven't quite got the traversal right. Let me know how strongly you'd like for this all to be incb
.One thing from the community: