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

[New] no-children-prop: Add allowFunctions option #1903

Merged

Conversation

alexzherdev
Copy link
Contributor

Resolves #1803

@alexzherdev alexzherdev force-pushed the 1803-no-children-prop-allow-functions branch 2 times, most recently from 5a65b24 to 71513a1 Compare July 26, 2018 03:46
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'd also like a way to forbid functions from being children at all - ie, maybe an option functions that can be an enum: child, prop, forbid, or allow?

@alexzherdev
Copy link
Contributor Author

alexzherdev commented Jul 26, 2018

Sounds good 👍
Although I'm curious, what would be the use case for not allowing functions at all?

@ljharb
Copy link
Member

ljharb commented Jul 26, 2018

My personal belief is that function children are an abomination that destroys the entire purpose for using jsx, and I never want them to exist in code I maintain.

@alexzherdev
Copy link
Contributor Author

Ok, I'll have to do my research 😃 But immediately, forbid would not even allow a children prop, I'm just not sure how that is different from e.g. onClick={() => {}} (which I know the downsides of, but that's already handled by a different rule)

@ljharb
Copy link
Member

ljharb commented Jul 26, 2018

Oh sure, I just want an enum value that's equivalent to "not specifying the option"

@alexzherdev
Copy link
Contributor Author

Ok, I don't think I'm on the same page. Let me go over it once again.

Current rule: children prop is a warning no matter what.
My PR:

  • allowFunctions: false (or not specified): no change to current rule
  • allowFunctions: true: functions in the children prop are not a warning. Functions in the child position are a warning - isn't that what you were going for originally?

And then extending that, the enum would be the 4 possible combinations of either allowing or forbidding functions in a prop or in the child position. Not specifying the enum value retain the current behaviour of the rule, which would be equal to the child option: functions are allowed in the child position but not as a prop. prop would be what allowFunctions: true does in my PR.

@ljharb
Copy link
Member

ljharb commented Jul 26, 2018

ahh i see what you mean - ok, fair. then true/false is probably fine.

altho i can also see people asking for a third option, which is requiring that functions be in the child position.

@alexzherdev
Copy link
Contributor Author

But don't they already have it via the current rule. As soon as you do children={() => {}}, it will warn, thereby forcing you to move the function into the child position.

@ljharb
Copy link
Member

ljharb commented Jul 26, 2018

Right - but what if they want all non-function children in the prop position, and function children in the child position?

Or, all non-function children in one position or the other, but function children disallowed?

@alexzherdev
Copy link
Contributor Author

alexzherdev commented Jul 26, 2018

That sounds like a complicated schema, so maybe we start small to make sure those options are actually in demand? Also the rule being called no-children-prop kinda implies we should be discouraging the children prop 🤔

@ljharb
Copy link
Member

ljharb commented Jul 26, 2018

ha, true. the main purpose is to force child position.

@alexzherdev
Copy link
Contributor Author

Ok, let me know if any other changes are necessary.


### `allowFunctions`

When `true`, passing a function in the children prop is preferred.
Copy link
Member

Choose a reason for hiding this comment

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

let's say this instead: when true, and passing a function as children, it must be in prop position and not child position.

lib/rules/no-children-prop.js Outdated Show resolved Hide resolved
@alexzherdev alexzherdev force-pushed the 1803-no-children-prop-allow-functions branch from 6f0d6f5 to a8702ce Compare July 27, 2018 06:40
@G-Rath
Copy link
Contributor

G-Rath commented May 19, 2019

Any update on this? I'd love to see this feature land, since libraries like react-router seem to recommend this pattern.

Copy link
Collaborator

@EvHaus EvHaus left a comment

Choose a reason for hiding this comment

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

LGTM. Fans of FaCCs will be screaming in horror at the idea that this rule exists. :)

@ljharb ljharb force-pushed the 1803-no-children-prop-allow-functions branch from 9fda347 to e509b9c Compare August 12, 2021 17:13
@ljharb ljharb changed the title [New] Add allowFunctions option to no-children-prop [New] no-children-prop: Add allowFunctions option Aug 12, 2021
@codecov-commenter
Copy link

codecov-commenter commented Aug 12, 2021

Codecov Report

Merging #1903 (241c38b) into master (e0c0812) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1903   +/-   ##
=======================================
  Coverage   97.40%   97.40%           
=======================================
  Files         111      111           
  Lines        7428     7444   +16     
  Branches     2715     2723    +8     
=======================================
+ Hits         7235     7251   +16     
  Misses        193      193           
Impacted Files Coverage Δ
lib/rules/no-children-prop.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e0c0812...241c38b. Read the comment docs.

@ljharb ljharb force-pushed the 1803-no-children-prop-allow-functions branch from e509b9c to 241c38b Compare August 12, 2021 17:30
@ljharb ljharb merged commit 241c38b into jsx-eslint:master Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Proposal: alter no-children-prop to allow functions
5 participants