-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
no-unused-prop-types false positives #811
Comments
I'm also facing a false positive error, although in a bit different case, when using destructuring: EDIT: Actually, there's already an issue for that: #816 |
One more example of false positive: const Button = React.createClass({
displayName: 'Button',
propTypes: {
name: React.PropTypes.string.isRequired,
isEnabled: React.PropTypes.bool.isRequired
},
render() {
const item = this.props;
const disabled = !this.props.isEnabled;
return (
<div>
<button type="button" disabled={disabled}>{item.name}</button>
</div>
);
}
}); Here |
I'm using props in the constructor and getting a false positive too: class Foo extends React.Component {
static propTypes = {
foo: PropTypes.func.isRequired,
}
constructor(props) {
super(props);
const { foo } = props;
this.message = foo('blablabla');
}
render() {
return <div>{this.message}</div>;
}
} This avoids multiple unnecessary calls of |
@perrin4869 fwiw, your example will break if new props are passed; you'd need to put the same constructor code in |
@ljharb I should have mentioned I guess, that in my specific case, |
Just because you currently always pass the same one doesn't mean that's a good assumption to depend on. |
I get false positives in this scenario: class MyComponent extends Component {
static propTypes = {
error: PropTypes.string,
};
componentWillReceiveProps(nextProps) {
if (nextProps.error) {
alert(nextProps.error);
}
}
}
|
I've a legit usecase for that example. We've a HOC that handles validation and gives a |
Counting used |
We had to choose between disabling react/prop-types or react/no-unused-prop-types. Since react/no-unused-prop-types has a bug (see jsx-eslint/eslint-plugin-react#811), we're disabling it for now.
We had to choose between disabling react/prop-types or react/no-unused-prop-types. Since react/no-unused-prop-types has a bug (see jsx-eslint/eslint-plugin-react#811), we're disabling it for now.
We had to choose between disabling react/prop-types or react/no-unused-prop-types. Since react/no-unused-prop-types has a bug (see jsx-eslint/eslint-plugin-react#811), we're disabling it for now.
Similar to the componentWillReceiveProps example,
@ljharb - do you think counting this can be considered? |
However, wouldn't a prop only used in |
It is a pity that is the unique working way to fix that :( . |
@ljharb - ^^ why do you say that? |
Its a grand ambition to have a rule like this. JavaScript is a highly dynamic language and there is no way a static checker can detect unused props reliably. Turning it off may be the only sane and time-practical recourse. My 2 cents of course. |
@prithsharma map state to props is meant to take wrapper props and state, and use them to construct a replacement props object. If the wrapped component does not need a prop, that prop should not remain in the replacement props - no other words, if the only use is in mapStateToProps, why would the wrapped component need it at all? |
The React will also only check props that are actually passed on to the component itself and not those passed to |
@ljharb, @ivarni - I agree with your point. What I was trying to achieve by keeping the "only being used in mapStateToProps" prop in Having said that, the comments above are missing a fine detail here, the object returned by Thank you for the fruitful discussion. I'll just disable the rule with inline comments. |
By now all of the issues reported here have been fixed. |
I get false positive in this case:
I get false positive for |
tbh that seems like just a poor design for |
@ljharb it may be a weird code (I think |
I think it's a bit of both; a linter can't really determine everything - in this case, your code is branching between "sometimes props, and sometimes a random object" - this plugin shouldn't be paying attention to properties of a random object. |
IMO it should be still possible to do via static analysis. |
I don't think it is; In general, passing the props object around itself is an antipattern; partly because it inhibits linting, but partly because passing around bags of things is less clear than passing around specific things. |
it doesn't matter when as long getLocation is a method of Locations class and "this.props" is used inside. and again - it's not about something being an antipattern, but possibility of doing static analysis in this particular case |
I'm telling you that as a maintainer of this plugin, it's not possible to do it reliably, because it's not always pulling off of |
It's not my case :) I just joined the conversation. |
That sounds familiar. Did someone say so earlier? 😛 As much as I hate getting slapped around by a linter, I agree with @ljharb here. I like linting as much as the next person but in this case, the use case itself is not compelling. The authors do listen if there is a good use case. And linting is purely an opt-in thingy. You lint because you want your code to really look good, not because you want some figure head to give a seal of OK. That said, there is no point in spamming a closed thread. If there are valid concerns, then a new issue should be opened. |
It wasn't meant to be a spam, as I typed it up here:
|
Didn't mean you specifically. We're all replying to a closed thread (including myself).
Yes and as @ljharb pointed it out, its not a legit case. What I am saying is the maintainers will be more inclined to take up something that is not being debated in a closed thread with a use case that can be argued as not the most compelling example of a valid use case. Didn't mean anything personally. |
I still have that kind of issue with the following code, that might be poor design, but I don't think so, feel free to tell me if it is just that: static propTypes = {
// That next line needs eslint-disable-line react/no-unused-prop-types
formValues: PropTypes.shape(),
};
static defaultProps = {
formValues: {},
};
// componentWillMont instead of constructor for this.setState to be available, matching componentWillReceiveProps call
componentWillMount() {
// I could pass `this.props` here but that would lead to the same eslint error
this.setDisabledSteps();
}
componentWillReceiveProps(nextProps) {
this.setDisabledSteps(nextProps);
}
// The bug is here, object destructuring in parmeters
setDisabledSteps({ formValues } = this.props) {
const disabledSteps = [];
[...]
this.setState({ disabledSteps });
} The reason I think this should be something allowed is that this is explicitly I realize that this is quite the same case as above. But I don't get why this would be bad design. |
Because sometimes that function will receive props, and sometimes it will be another object. There’s no way for the reader - or a linter - to know for sure that the props object goes down that codepath. Instead, always destructure the props object unconditionally where you need it. |
Is there really no way the linter can read Because here it's one key but I have an other project where there was like 15 of them needed for the function call. And "your" solution would then be: const {a,b,c,d,e,f,...} = this.props
functionCall({a,b,c,d,e,f,...}); Which is kinda messy ... :/ |
It can read it, but what it means is that the value of the object will only be this.props when the function is called with the first argument as missing or undefined - and “how the function is called from everywhere in your program” isn’t something a linter or a human can determine from one file :-) |
Sure I totally understand the fact that we can't know if it will be called with no argument. But I mean, "argument could be this.props because it's the default value" should be enought for linter to say ok it might be it, let's not raise a blocking error. |
I don’t think it is. The goal is to ensure that, if your code paths are executed, the props are used. with your code, i have no idea if the prop name is a prop (and been validated) or a random object property. |
If some function call is within conditions, code paths might not be executed. (Never, or just not in some configuration cases) But well I understand that this might be only my opinion. A linter is there to help, not to bridle. (That might not be the right word) |
The linter is here to actively obstruct potential bugs - if there’s any chance something is a bug, it should not be merged as-is. |
Well, to me linters are to avoid messy code (where here, it's doing the opposite) and testing is to avoid bugs but well... I guess that's again my opinion. |
The
no-unused-prop-types
rule reports false positives for me.Here's my code:
The
no-unused-prop-types
-Rule shows an error for both theendFriendship
and thehandleFriendrequest
props but both are used in my component.I use
eslint
3.4.0 andeslint-plugin-react
6.2.0.I use the
no-unused-prop-types
-Rule as part ofeslint-config-airbnb
11.0.0.In the config it is set like this:
The text was updated successfully, but these errors were encountered: