Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ rules in templates can be disabled with eslint directives with mustache or html
| Name | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
| [template-no-action-on-submit-button](docs/rules/template-no-action-on-submit-button.md) | disallow action attribute on submit buttons | | | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |

Expand Down
65 changes: 65 additions & 0 deletions docs/rules/template-no-action-on-submit-button.md
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.
Copy link
Copy Markdown
Member

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 action as an attribute to button or input so I believe this is reference to some esoteric way of triggering actions in Ember?

Copy link
Copy Markdown
Contributor Author

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


## 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)
62 changes: 62 additions & 0 deletions lib/rules/template-no-action-on-submit-button.js
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',
});
}
},
};
},
};
52 changes: 52 additions & 0 deletions tests/lib/rules/template-no-action-on-submit-button.js
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',
},
],
},
],
});
Loading