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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new no-children-prop rule (Fixes #720) #722

Merged
merged 1 commit into from
Sep 16, 2016

Conversation

benstepp
Copy link
Contributor

Prevents children being passed as props. Children should be actual children.

I've never written an eslint rule, so any suggestions are definitely appreciated.

@ljharb
Copy link
Member

ljharb commented Jul 27, 2016

LGTM


The following patterns are considered warnings:

```js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should use jsx, not js, for better syntax highlighting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been told js gives better highlighting than jsx, even for JSX content.

Copy link
Collaborator

@lencioni lencioni Jul 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does look better :-) either way's fine, but i'm pretty sure it's come up before. Maybe Github improved the highlighting since.

@lencioni
Copy link
Collaborator

@benstepp You've done a really great job with your first rule! 🎊

if (isTagName(node.parent.name.name) && node.name.name === 'children') {
context.report({
node: node,
message: 'Do not pass children as props.'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to additionally tell people what they should do. How about this error message?

Do not pass children as a prop. Instead, nest children between the opening and closing tags.

@benstepp
Copy link
Contributor Author

I went ahead and made changes to the error messages, and added the additional clarity regarding the desired behavior to the introduction of the docs.

  • Added a few tests for a <MyComponent />.
  • Switched to an early return to reduce the nesting.

if (childrenProp) {
context.report({
node: node,
message: 'Do not pass children as props. Instead, pass them as the third argument to React.createElement.'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that React.createElement can take an arbitrary number of arguments. Starting with the third argument they are considered children. This error message should probably be updated to reflect this.

@benstepp
Copy link
Contributor Author

Went ahead and added test cases with non-children props and multiple children, as well as clarified the error regarding React.createElement.

@lencioni
Copy link
Collaborator

LGTM

@lencioni
Copy link
Collaborator

@benstepp It looks like this needs a rebase.

@lencioni
Copy link
Collaborator

@benstepp any chance you can rebase this?

@benstepp benstepp force-pushed the bs-no-children-prop branch 2 times, most recently from 87cf23a to 6dc4837 Compare September 16, 2016 16:43
@benstepp
Copy link
Contributor Author

benstepp commented Sep 16, 2016

Forgot I still had this over here.

Rebased over current master. Same 13 failing tests as master that would probably be better fixed as a separate commit.

It is currently marked as recommended, but I think that should be removed until a major version. Let me know what you think.

@lencioni
Copy link
Collaborator

Agreed on all accounts! Thanks for rebasing. 💃

@@ -96,6 +97,7 @@ module.exports = {
'react/jsx-no-undef': 2,
'react/jsx-uses-react': 2,
'react/jsx-uses-vars': 2,
'react/no-danger': 2,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you unintentionally added this line, which is causing specs to fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're right. Went ahead and removed that line.

Prevents children being passed as props. Children should be actual
children.

---

* Add Component to tests for no-children-prop
* Add note in docs about recommended behavior
* Change syntax in docs for no-children-prop to jsx

---

* Clarify that multiple arguments may be passed as children to
React.createElement
* Reorganize tests to show JSX with createElement counterpart immediately
following.
* Added test cases with a non-children property
* Added test cases with multiple children

---

* Add no-children-prop to recommended config

---

* Remove recommended config for no-children-prop
@lencioni lencioni merged commit 0348173 into jsx-eslint:master Sep 16, 2016
@lencioni
Copy link
Collaborator

Thanks!

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.

4 participants