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 rule no-builtin-form-components #2990

Merged
merged 3 commits into from Nov 21, 2023

Conversation

gilest
Copy link
Contributor

@gilest gilest commented Nov 8, 2023

Was surprised to find that no such rule exists.

Combined with no-mut-helper this can help prevent two-way binding being present in a codebase.

Rule docs rendered

@@ -0,0 +1,65 @@
# no-builtin-form-components

Ember's built-in form components use two-way data binding, where the property as `@value` or `@checked` is mutated by user interaction. This goes against the Data Down Actions Up principle, goes against Glimmer Components’ intention to have immutable arguments, and is [discouraged by the Ember Core team](https://www.pzuraq.com/on-mut-and-2-way-binding/).
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The pzuraq post is fairly old and mostly concerned with the mut helper. Would be nice to link to a better resource here.

<Textarea></Textarea>
```

## Migration
Copy link
Contributor

Choose a reason for hiding this comment

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

Both of these migration paths are for 'controlled inputs'

There is another migration path, too.

Which is faster, and involves less binding.

See:

Copy link
Contributor Author

@gilest gilest Nov 9, 2023

Choose a reason for hiding this comment

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

True. I've seen some of this before but never used these patterns exactly. Not sure I understand them well enough make a concise recommendation.

How would you suggest the rule doc is structured?

We might:

  1. Branch the suggestions based on their existing form type somehow
  2. Just present both (light/controlled) as options
  3. Have a "Further reading" discussion section with more details

Ideally the simplest explanation of how to fix this issue is early in this doc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Option 2 would be my preference.
You can copy from the above links 🎉

Copy link
Contributor Author

@gilest gilest Nov 9, 2023

Choose a reason for hiding this comment

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

I've made minor changes to incorporate your recommendations.

To be frank the docs on the ember primitives page are not easy to follow and don't clearly explain the light/controlled concepts so I don't really want to copy that over to this rule doc.

I'd welcome specific suggestions from you, but I can't paraphrase what you want because it's a bit conceptual IMO 😅

For many cases they will be the ideal migration but I think keeping an example of a low-level migration without addons is valuable for users to understand the underlying change that is needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

can you open an issue on ember-primitives? docs should be easy to understand, and it's a bug that they're not https://github.com/universal-ember/ember-primitives/ thanks!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NullVoxPopuli
Copy link
Contributor

I'd like to propose this be enabled by default since we're working on the next major anyway

@bmish
Copy link
Member

bmish commented Nov 9, 2023

Just to confirm, should this rule be added to recommended? Note that we shouldn't do that in this PR but we can line that up for #2712.

@NullVoxPopuli
Copy link
Contributor

@bmish yeah, i think so.
I think long term these two components are going to hopefully move out of core and in to an addon or something

@@ -51,6 +53,8 @@ You may consider composing the [set helper](https://github.com/pzuraq/ember-set-
/>
```

Depending on your requirements, consider using form management patterns like the "light" [Form component from ember-primitives](https://ember-primitives.pages.dev/7-forms/1-intro) or the "controlled" [ember-headless-form](https://ember-headless-form.pages.dev/).
Copy link
Contributor

Choose a reason for hiding this comment

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

this isn't quite what I meant about adding. 😅

This is docs, for example -- also in the tutorial

I think we should show an "uncontrolled" example before the controlled example. I think it's important that folks see how to not write JavaScript before they reach for javascript.

I linked to ember-primitives as an example of the pattern, rather than intending for it to be directly linked. Apologies for not being more clear!

Copy link
Contributor Author

@gilest gilest Nov 11, 2023

Choose a reason for hiding this comment

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

Ahh yes. That makes a lot more sense 😅

FWIW I appreciate the resources you're creating and the modern improvements.

But we should have empathy for the frustrated engineer who is on the receiving end of lint errors, and try to give them a very clear explanation of the problem and how to solve it. Ideally without overloading them with other new ideas.

That's why I'm not using cell or the new component authoring format in the examples.

Anyway I had another run at it. Let me know your thoughts

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW I appreciate the resources you're creating and the modern improvements.

<3

other new ideas.

controlling every aspect of a form is actually the new idea -- the uncontrolled way is the very old way to do forms, it builds on the foundation of the platform and allows the developer to do less work.

Additionally, React has been trying to teach uncontrolled vs controlled forms for 5+ years! 💪

That's why I'm not using cell or the new component authoring format in the examples.

that's totally fine! the details are a means to the concept, and not actually important to the concept -- I think that's probably one thing people get too hung up on when learning new things. It's important to be able to adapt a concept in to your existing code, as not all documentation can match what your existing code is doing.

Anyway I had another run at it. Let me know your thoughts

will do, thanks a ton for working on this and being willing to have the back and forth!!

Copy link
Contributor

Choose a reason for hiding this comment

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

a means to the concept, and not actually important to the concept
can match what your existing code is doing.

which you've don't exactly! yay!


Many forms may be simplified by switching to a light one-way data approach.

For example – vanilla JavaScript has everything we need to handle form data, de-sync it from our source data and collect all user input in a single object.
Copy link
Contributor

Choose a reason for hiding this comment

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

this looks great! nice work!

@gilest
Copy link
Contributor Author

gilest commented Nov 19, 2023

From my perspective this is ready @bmish

Copy link
Member

@bmish bmish left a comment

Choose a reason for hiding this comment

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

Thanks!

@bmish bmish merged commit e052fe9 into ember-template-lint:master Nov 21, 2023
11 checks passed
@bmish bmish mentioned this pull request Nov 21, 2023
@bmish bmish changed the title Add rule no-builtin-form-components Add new rule no-builtin-form-components Nov 21, 2023
@gilest gilest deleted the no-builtin-form-components branch November 21, 2023 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants