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

Rule proposal: jsx-no-undef-props #635

Closed
wants to merge 4 commits into from
Closed

Rule proposal: jsx-no-undef-props #635

wants to merge 4 commits into from

Conversation

beefancohen
Copy link
Contributor

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 馃槤


```js
<Hello name={undefined} />;
<Hello name={foo ? undefined : bar} />;
Copy link
Member

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?

Copy link
Contributor Author

@beefancohen beefancohen Jun 16, 2016

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} />;
}

@ljharb
Copy link
Member

ljharb commented Jun 16, 2016

This seems like it adds a little value when the prop value is solely the literal undefined - but it wouldn't be able to catch any variables whose contents were undefined, and the conditional case seems like a useful way to conditionally specify a prop.

@beefancohen
Copy link
Contributor Author

Yeah that was kind of the point to check for literally undefined - this was more for newcomers to React to say you don't need to pass down an undefined prop that you've maybe defined in propTypes. I can see, also, that this conflicts with #611 which I think is more valuable, anyway.

@ljharb
Copy link
Member

ljharb commented Jun 16, 2016

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 true, so I think it's helpful to have one for literal undefined as well.

@beefancohen
Copy link
Contributor Author

Ah you're right I misunderstood - it would only conflict if someone defined a prop in propTypes and then weirdly just passed it down with undefined as a value, but I don't think that's much to worry about. I'll update to just check for the literal undefined.

@ljharb
Copy link
Member

ljharb commented Jun 16, 2016

LGTM

// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function(context) {
Copy link
Contributor

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

@ljharb
Copy link
Member

ljharb commented Jul 28, 2016

@evcohen this seems like a bad merge - can you force push to the branch and update the PR in-place?

@ljharb ljharb reopened this Jul 28, 2016
@ljharb
Copy link
Member

ljharb commented Jul 28, 2016

@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

@beefancohen
Copy link
Contributor Author

@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
Copy link
Member

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 :-)

Copy link
Contributor Author

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?!? 馃槢

beefancohen and others added 3 commits November 7, 2016 09:52
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.
Copy link
Member

@ljharb ljharb left a 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.'
Copy link
Member

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?

Copy link
Contributor Author

@beefancohen beefancohen Nov 8, 2016

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

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

4 participants