Skip to content
Merged
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 @@ -190,6 +190,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-modifiers](docs/rules/template-no-action-modifiers.md) | disallow usage of {{action}} modifiers | | | |
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
| [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
64 changes: 64 additions & 0 deletions docs/rules/template-no-action-modifiers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ember/template-no-action-modifiers

<!-- 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 usage of `{{action}}` modifiers in templates.

The `{{action}}` modifier has been deprecated in favor of the `{{on}}` modifier. The `{{on}}` modifier provides a more explicit and flexible way to handle events.

## Rule Details

This rule disallows using `{{action}}` as an element modifier.

## Examples

### Incorrect ❌

```gjs
<template>
<button {{action "save"}}>Save</button>
</template>
```

```gjs
<template>
<div {{action "onClick"}}>Click me</div>
</template>
```

```gjs
<template>
<form {{action "submit" on="submit"}}>Submit</form>
</template>
```

### Correct ✅

```gjs
<template>
<button {{on "click" this.handleClick}}>Save</button>
</template>
```

```gjs
<template>
<div {{on "click" this.onClick}}>Click me</div>
</template>
```

```gjs
<template>
<form {{on "submit" this.handleSubmit}}>Submit</form>
</template>
```

## Related Rules

- [template-no-action](./template-no-action.md)

## References

- [Ember Octane Guide - Element Modifiers](https://guides.emberjs.com/release/components/template-lifecycle-dom-and-modifiers/)
- [eslint-plugin-ember template-no-action](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-action.md)
42 changes: 42 additions & 0 deletions lib/rules/template-no-action-modifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow usage of {{action}} modifiers',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-action-modifiers.md',
},
fixable: null,
schema: [],
messages: {
noActionModifier: 'Do not use action modifiers. Use on modifier with a function instead.',
},
},

create(context) {
function checkForActionModifier(node) {
// Check if this is an action modifier (not action helper in mustache)
if (
node.path &&
node.path.type === 'GlimmerPathExpression' &&
node.path.original === 'action' &&
node.path.head?.type !== 'AtHead' &&
node.path.head?.type !== 'ThisHead'
) {
context.report({
node,
messageId: 'noActionModifier',
});
}
}

return {
GlimmerElementModifierStatement(node) {
checkForActionModifier(node);
},
};
},
};
51 changes: 51 additions & 0 deletions tests/lib/rules/template-no-action-modifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const rule = require('../../../lib/rules/template-no-action-modifiers');
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-modifiers', rule, {
valid: [
'<template><button {{on "click" this.handleClick}}>Click</button></template>',
'<template><div {{on "mouseenter" this.onHover}}>Hover</div></template>',
'<template><form {{on "submit" this.handleSubmit}}>Submit</form></template>',
'<template>{{action "myAction"}}</template>',
'<template>{{this.action}}</template>',
'<template>{{@action}}</template>',
],

invalid: [
{
code: '<template><button {{action "save"}}>Save</button></template>',
output: null,
errors: [
{
message: 'Do not use action modifiers. Use on modifier with a function instead.',
type: 'GlimmerElementModifierStatement',
},
],
},
{
code: '<template><div {{action "onClick"}}>Click me</div></template>',
output: null,
errors: [
{
message: 'Do not use action modifiers. Use on modifier with a function instead.',
type: 'GlimmerElementModifierStatement',
},
],
},
{
code: '<template><form {{action "submit" on="submit"}}>Submit</form></template>',
output: null,
errors: [
{
message: 'Do not use action modifiers. Use on modifier with a function instead.',
type: 'GlimmerElementModifierStatement',
},
],
},
],
});
Loading