Skip to content

Possible jsx-no-allocation-in-props rule #1888

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

alexzherdev
Copy link
Contributor

cc @ljharb

So... now that I know what future prospects are for jsx-no-bind (#1633 (comment)) I actually have doubts that it makes sense to add this new rule on its own. The code here is basically the same as jsx-no-bind. Perhaps we could instead just update that rule to handle object creation. Thoughts?

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 don't think the name "no bind" makes sense for objects and arrays :-/

## Rule Options

```js
"react/jsx-no-allocation-in-props": [<enabled>, {
Copy link
Member

Choose a reason for hiding this comment

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

array literals, and object literals, should probably have separate controls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually couldn't come up with a use case where an array would be supplied as a prop to a DOM component, that probably is not a valid case?

Copy link
Member

Choose a reason for hiding this comment

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

for DOM components sure, but i meant, i should be able to configure this rule to only warn on object literals, or to only warn on array literals.

README.md Outdated
@@ -150,6 +150,7 @@ Enable the rules that you would like to use.
* [react/jsx-key](docs/rules/jsx-key.md): Validate JSX has key prop when in array or iterator
* [react/jsx-max-depth](docs/rules/jsx-max-depth.md): Validate JSX maximum depth
* [react/jsx-max-props-per-line](docs/rules/jsx-max-props-per-line.md): Limit maximum of props on a single line in JSX (fixable)
* [react/jsx-no-allocation-in-props](docs/rules/jsx-no-allocation-in-props.md): Prevent usage of `[]` and `{}` in JSX props
Copy link
Member

Choose a reason for hiding this comment

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

does this need a jsx- prefix? couldn't it also apply to React.createElement?

@alexzherdev
Copy link
Contributor Author

Oh yeah, a new rule (if we go that way) would have a different name.

@alexzherdev
Copy link
Contributor Author

Removed jsx prefix, added createElement support and controls for arrays/objects

@alexzherdev alexzherdev force-pushed the 1633-jsx-no-allocation-in-props branch from 776638b to 027fb23 Compare July 22, 2018 03:54
@ljharb
Copy link
Member

ljharb commented Jul 22, 2018

I don't think "allocations" is the best word to use in a memory-managed language, where allocations aren't user-observable.

@alexzherdev
Copy link
Contributor Author

Oh, for sure, this name leaves much to be desired. I guess this is more no-object-or-array-literals-in-props but that's quite a mouthful 😐 Although not really more than no-redundant-should-component-update 😄

@ljharb ljharb marked this pull request as draft August 9, 2021 21:00
@ljharb ljharb force-pushed the master branch 6 times, most recently from 59af733 to 865ed16 Compare November 11, 2022 02:45
@ljharb ljharb force-pushed the master branch 4 times, most recently from 069314a to 181c68f Compare November 18, 2022 17:19
@benasher44
Copy link

I see this was updated recently. Any chance it'll ship anytime soon? Would love to see this one live!

@ljharb
Copy link
Member

ljharb commented Jan 27, 2023

@benasher44 it hasn't been updated since 2018; those 4 force pushes are to master, not this PR.

If you'd like to help, please comment a link to an updated branch (NOT a new PR) and i can update this one :-)

@benasher44
Copy link

benasher44 commented Mar 5, 2023

@ljharb just pushed to no-allocation-in-props in benasher44/eslint-plugin-react. Only thing I'm having trouble with right now is this error running the test locally:

AssertionError [ERR_ASSERTION]: Parsers provided as strings to RuleTester must be absolute paths

I get this error running other tests locally though too, so I think I'm just missing some configuration in my local env/setup. Any idea what it might be?

@benasher44
Copy link

Ah wait I think I figured that out. Hold please!

@benasher44
Copy link

Sweet okay pushed again. Tests pass now!

@benasher44
Copy link

benasher44 commented Mar 5, 2023

Looks like last thing being discussed was naming. Are we good with no-object-or-array-literals-in-props? (my branch still has previous "allocation" naming)

@benasher44
Copy link

@ljharb just checking in — happy to do the work if more is needed!

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.

3 participants