-
-
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule proposal: jsx-no-undef-props #635
Conversation
docs/rules/jsx-no-undef-props.md
Outdated
|
||
```js | ||
<Hello name={undefined} />; | ||
<Hello name={foo ? undefined : bar} />; |
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.
How else would you make a prop conditionally present?
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.
True - you can move the logic outside of the prop value and just pass a var, but should probably just special case this to ignore. In this case, the rule is kinda pointless :)
render() {
const prop = foo ? undefined : bar;
return <Hello name={prop} />;
}
This seems like it adds a little value when the prop value is solely the literal |
Yeah that was kind of the point to check for literally |
I don't see how it conflicts with #611 - that's checking for unused propTypes within the same component file, at the definition site, while this one checks for undefined props at the rendering site. We already have a rule that checks for literal |
Ah you're right I misunderstood - it would only conflict if someone defined a prop in |
LGTM |
lib/rules/jsx-no-undef-props.js
Outdated
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { |
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 should use the new eslint rule format
@evcohen this seems like a bad merge - can you force push to the branch and update the PR in-place? |
@evcohen I think it needs a rebase, NOT a merge, so as to reduce the 56 commits included in this PR to a more manageable number |
@ljharb as soon as i get a few mins i will rebase and squash |
CHANGELOG.md
Outdated
@@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. | |||
This project adheres to [Semantic Versioning](http://semver.org/). | |||
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com). | |||
|
|||
<<<<<<< HEAD |
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 think you need to fix some merge conflicts :-)
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.
that's not supposed to be there?!? 馃槢
This rule enforces that no props have the value `undefined`. In React, these props will not be passed down to the instance of the component, so just suggest to the user to leave off the prop instead of passing undefined as its value.
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 think this rule should NOT warn when a null
/undefined
prop follows a spread prop - I often do <div {...obj} override={null} />
to do an "omit".
|
||
context.report({ | ||
node: node, | ||
message: 'Leave off the prop `' + name + '` instead of passing undefined as its value.' |
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.
should this check for null
also, since React ignores them both?
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 didnt think react ignored null? i was under the impression that when passing down null, if you have a defaultProp set, it won't be set; whereas if a prop is undefined, the defaultProp is applied.
for reference: http://stackoverflow.com/questions/34809178/react-js-default-prop-is-not-used-with-null-is-passed
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.
also re: following spread operator- if the above is true and you want to omit a value, we should probably ignore in the case that you want the defaultProp to be applied. or maybe this renders this rule futile because of that distinction.
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.
ah, good to know, thanks
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.
right, i'm suggesting always ignoring cases or either that appear after a spread prop.
This rule enforces that no props have the value
undefined
. In React, these props will not be passed down to the instance of the component, so just suggest to the user to leave off the prop instead of passing undefined as its value.Not sure if this is wanted or needed, but I thought it could be useful! Don't be afraid to say you think it's stupid though 馃槤