-
-
Notifications
You must be signed in to change notification settings - Fork 211
Extract rule: template-no-action-on-submit-button #2400
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
Closed
NullVoxPopuli
wants to merge
1
commit into
ember-cli:master
from
NullVoxPopuli:nvp/template-lint-extract-rule-template-no-action-on-submit-button
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # ember/template-no-action-on-submit-button | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| 💼 This rule is enabled in the following [configs](https://github.com/ember-cli/eslint-plugin-ember#-configurations): `strict-gjs`, `strict-gts`. | ||
|
|
||
| Disallow `action` attribute on submit buttons. | ||
|
|
||
| Using the `action` attribute on submit buttons is a common mistake. Instead, you should use the `{{on}}` modifier to handle click events, or handle form submission at the form level. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule disallows using the `action` attribute on `<button>` elements (which default to type="submit") and `<input type="submit">` elements. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Incorrect ❌ | ||
|
|
||
| ```gjs | ||
| <template> | ||
| <button action="save">Save</button> | ||
| </template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template> | ||
| <button type="submit" action="submit">Submit</button> | ||
| </template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template> | ||
| <input type="submit" action="go" /> | ||
| </template> | ||
| ``` | ||
|
|
||
| ### Correct ✅ | ||
|
|
||
| ```gjs | ||
| <template> | ||
| <button {{on "click" this.handleClick}}>Save</button> | ||
| </template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template> | ||
| <button type="button" action="doSomething">Click</button> | ||
| </template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template> | ||
| <form {{on "submit" this.handleSubmit}}> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </template> | ||
| ``` | ||
|
|
||
| ## Related Rules | ||
|
|
||
| - [template-no-action-modifiers](./template-no-action-modifiers.md) | ||
|
|
||
| ## References | ||
|
|
||
| - [ember-template-lint: no-invalid-interactive](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-invalid-interactive.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'disallow action attribute on submit buttons', | ||
| category: 'Best Practices', | ||
| strictGjs: true, | ||
| strictGts: true, | ||
| url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-action-on-submit-button.md', | ||
| }, | ||
| fixable: null, | ||
| schema: [], | ||
| messages: { | ||
| noActionOnSubmitButton: | ||
| 'Do not use action attribute on submit buttons. Use on modifier instead or handle form submission.', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| GlimmerElementNode(node) { | ||
| // Check if this is a button element | ||
| if (node.tag !== 'button' && node.tag !== 'input') { | ||
| return; | ||
| } | ||
|
|
||
| let hasActionAttribute = false; | ||
| let isSubmitButton = false; | ||
| let hasNonSubmitType = false; | ||
|
|
||
| for (const attr of node.attributes) { | ||
| if (attr.type === 'GlimmerAttrNode') { | ||
| // Check for action attribute | ||
| if (attr.name === 'action') { | ||
| hasActionAttribute = true; | ||
| } | ||
| // Check if type="submit" or no type (defaults to submit for button) | ||
| if (attr.name === 'type') { | ||
| const value = attr.value; | ||
| if (value.type === 'GlimmerTextNode' && value.chars === 'submit') { | ||
| isSubmitButton = true; | ||
| } else if (value.type === 'GlimmerTextNode' && value.chars !== 'submit') { | ||
| hasNonSubmitType = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // For buttons, default type is submit unless explicitly set otherwise | ||
| const isDefaultSubmitButton = node.tag === 'button' && !hasNonSubmitType && !isSubmitButton; | ||
|
|
||
| if (hasActionAttribute && (isSubmitButton || isDefaultSubmitButton)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'noActionOnSubmitButton', | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| const rule = require('../../../lib/rules/template-no-action-on-submit-button'); | ||
| const RuleTester = require('eslint').RuleTester; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve('ember-eslint-parser'), | ||
| parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, | ||
| }); | ||
|
|
||
| ruleTester.run('template-no-action-on-submit-button', rule, { | ||
| valid: [ | ||
| '<template><button {{on "click" this.handleClick}}>Click</button></template>', | ||
| '<template><button type="button" action="doSomething">Click</button></template>', | ||
| '<template><input type="text" action="search" /></template>', | ||
| '<template><div action="whatever">Not a button</div></template>', | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| code: '<template><button action="save">Save</button></template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Do not use action attribute on submit buttons. Use on modifier instead or handle form submission.', | ||
| type: 'GlimmerElementNode', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<template><button type="submit" action="submit">Submit</button></template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Do not use action attribute on submit buttons. Use on modifier instead or handle form submission.', | ||
| type: 'GlimmerElementNode', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<template><input type="submit" action="go" /></template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Do not use action attribute on submit buttons. Use on modifier instead or handle form submission.', | ||
| type: 'GlimmerElementNode', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this relevant? I can find no mention of
actionas an attribute tobuttonorinputso I believe this is reference to some esoteric way of triggering actions in Ember?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, I think we can just kill this rule