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-action-on-submit-button #1931

Merged

Conversation

JoaoDsv
Copy link
Contributor

@JoaoDsv JoaoDsv commented May 4, 2021

Fixes #44.

It implements the rule proposal of no-action-on-submit-button.

no-action-on-submit-button

This rule requires all <button> elements with a type="submit" attribute to not have any action.

When the type attribute of <button> elements is submit, the action should be on the <form> element instead of directly on the button.

By default, the type attribute of <button> elements is submit.

Examples

This rule forbids the following:

<form>
  <button type='submit' {{on 'click' this.handleClick}} />
  <button type='submit' {{action 'handleClick'}} />
  <button {{on 'click' this.handleClick}} />
  <button {{action 'handleClick'}} />
</form>

This rule allows the following:

// In a <form>
<form>
  <button type='button' {{on 'click' this.handleClick}} />
  <button type='button' {{action 'handleClick'}} />
  <button type='submit' />
  <button />
</form>

// Outside a <form>
<button type='submit' {{on 'click' this.handleClick}} />
<button type='submit' {{action 'handleClick'}} />
<button {{on 'click' this.handleClick}} />
<button {{action 'handleClick'}} />

Copy link
Collaborator

@bertdeblock bertdeblock left a comment

Choose a reason for hiding this comment

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

Should we also check if the parent form already has an action defined and if so, assume that the action on the submit button is an arbitrary event? E.g. for tracking purposes. Or would this be to far-fetched?

docs/rule/no-action-on-submit-button.md Outdated Show resolved Hide resolved
docs/rule/no-action-on-submit-button.md Outdated Show resolved Hide resolved
lib/rules/no-action-on-submit-button.js Outdated Show resolved Hide resolved
lib/rules/no-action-on-submit-button.js Outdated Show resolved Hide resolved
lib/rules/no-action-on-submit-button.js Outdated Show resolved Hide resolved
test/unit/rules/no-action-on-submit-button-test.js Outdated Show resolved Hide resolved
test/unit/rules/no-action-on-submit-button-test.js Outdated Show resolved Hide resolved
@JoaoDsv JoaoDsv force-pushed the no-action-on-submit-button branch 2 times, most recently from b425d07 to 0bbe945 Compare August 13, 2021 14:00
@bmish bmish changed the title Add "no-action-on-submit-button" rule Add new rule no-action-on-submit-button Oct 4, 2021
@bmish
Copy link
Member

bmish commented Oct 4, 2021

@JoaoDsv there are conflicts. Is this close to being ready to merge?

@bmish
Copy link
Member

bmish commented Oct 4, 2021

I fixed the conflicts, tests, and many (but not all) of the comments.

There's another comment here that may need to be addressed still: #44 (comment)

@bertdeblock
Copy link
Collaborator

If we need to check if the button has a form parent, we could make this function a shared helper and use that here as well? Though, if this rule specifically checks for type="submit" and buttons without a type (we also have the require-button-type rule to prevent this) that already seems like a good start?

@bmish
Copy link
Member

bmish commented Oct 5, 2021

@JoaoDsv if you'd like to finish this PR up, let us know. Otherwise, I think @bertdeblock and I will try to finish it up. @bertdeblock, I'll give you access to push updates to this PR, so we can finish addressing comments including reducing false positives and improving consistency.

@bmish
Copy link
Member

bmish commented Oct 5, 2021

To be clear, I do think we should address this comment to reduce false positives: #44 (comment)

I'm not going to work on this now so @bertdeblock you can take over if you're interested.

@JoaoDsv
Copy link
Contributor Author

JoaoDsv commented Oct 7, 2021

Thanks for your feedbacks @bmish @bertdeblock !
Sorry for the late answer, I'm happy to try my best to address your comments

Feel free to commit at any time to help me to get this PR merged, it will be very welcome 🙂

@JoaoDsv
Copy link
Contributor Author

JoaoDsv commented Oct 7, 2021

I addressed the remaining feedbacks ✅

Except reducing the false positives by checking the presence a parent form element. I'll handle it ASAP and let you know!

<button type="button" {{on "click" this.handleClick}} />
<button type="button" {{action "handleClick"}} />
<button type="submit" />
<button />
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be disallowed by the require-button-type rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your comment @MelSumner. Sure, but aren't rules independent from each other?

My point is: should we take an other rule into account in the unit test of this current rule?

If users decide to disable the require-button-type rule, then this syntax will be allowed by the current rule, because it's not its role to prevent the missing type.

What is your opinion about it?

Copy link
Contributor

Choose a reason for hiding this comment

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

So I try to write new rules with existing rules in mind; I don't want a rule I create to fail another existing rule.

But @rwjblue might have some more clarity here.

(PS overall I am excited about this rule!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @MelSumner

I'm back at work on this rule, which I'd forgotten for a long time 🙇

This rule is not encouraging <button /> without (explicit) type. It's essentially a syntax that won't make this rule fail, so I listed it in the "valid" scenarios.

Would you prefer me to remove <button /> from these scenarios?

  • The downside is that this scenario won't be clearly mentioned while likely to happen.
  • The advantage is that it won't encourage people using this syntax (not allowed by require-button-type dedicated rule)

@JoaoDsv JoaoDsv force-pushed the no-action-on-submit-button branch 3 times, most recently from 45ad1b9 to c0ebc5e Compare January 6, 2022 16:52
@bmish
Copy link
Member

bmish commented Jan 7, 2022

Looks good except we still need the parent <form> check.

@JoaoDsv JoaoDsv force-pushed the no-action-on-submit-button branch 2 times, most recently from a834abb to e2eade7 Compare August 28, 2022 16:13
@bmish
Copy link
Member

bmish commented Aug 28, 2022

@JoaoDsv thanks for your continued work on this. Let us know when it's ready (with my last comment addressed).

@JoaoDsv JoaoDsv force-pushed the no-action-on-submit-button branch 4 times, most recently from 0aa3b13 to 24567e2 Compare September 18, 2023 21:22
@JoaoDsv
Copy link
Contributor Author

JoaoDsv commented Sep 18, 2023

Hi @bmish @bertdeblock

I have belatedly addressed your request about the parent <form> in this last commit: 24567e2. Is it what you had in mind?

ℹ️ Nicely spotted, as this also seems to solve @rwjblue's related concern: #44 (comment).

Would you mind having a last review and if everything looks ready, approve it?

'<button type="submit" {{on "click" (fn this.addNumber 123)}} />',
],

bad: [
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we add a test for a deeply-nested button like this to make sure we aren't only checking the direct parent?

  • <form><div><button {{action this.handleClick}} /></div></form>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! I added it as last scenario ✅

@bmish bmish mentioned this pull request Nov 4, 2023
@bmish
Copy link
Member

bmish commented Nov 4, 2023

@JoaoDsv just to confirm, we should enable this as recommended in the next major release, right?

@JoaoDsv
Copy link
Contributor Author

JoaoDsv commented Nov 4, 2023

@JoaoDsv just to confirm, we should enable this as recommended in the next major release, right?

I'd indeed suggest to:

  • add the rule in the next minor release
  • flag the rule as recommended in the next major release

By doing so, we additionally have some time to implement a --fix option to make the upgrade to the next major smoother. Wdyt @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.

Sounds good, thanks for your work to get this finished!

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.

Rule proposal: no-action-on-submit-button
5 participants