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 @@ -203,6 +203,7 @@ rules in templates can be disabled with eslint directives with mustache or html
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow redundant `this.this` in templates | | 🔧 | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
| [template-no-inline-event-handlers](docs/rules/template-no-inline-event-handlers.md) | disallow DOM event handler attributes | | | |
| [template-no-inline-styles](docs/rules/template-no-inline-styles.md) | disallow inline styles | | | |
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
Expand Down
72 changes: 72 additions & 0 deletions docs/rules/template-no-inline-event-handlers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# ember/template-no-inline-event-handlers

<!-- end auto-generated rule header -->

Disallows DOM event handler attributes in templates.

Inline event handlers like `onclick="..."` are an older pattern that should be replaced with the `{{on}}` modifier for better Ember integration and testability.

## Rule Details

This rule disallows the use of inline DOM event handler attributes like `onclick`, `onsubmit`, etc.

## Examples

Examples of **incorrect** code for this rule:

```gjs
<template>
<button onclick="alert('test')">Click</button>
</template>
```

```gjs
<template>
<div onmousedown="handleEvent()">Content</div>
</template>
```

```gjs
<template>
<form onsubmit="return false;">Form</form>
</template>
```

Examples of **correct** code for this rule:

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

```gjs
<template>
<input {{on "input" this.handleInput}} />
</template>
```

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

## Migration

Replace:

```gjs
<button onclick="alert('clicked')">
```

With:

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

## References

- [Ember.js Guides - Event Handling](https://guides.emberjs.com/release/components/component-state-and-actions/)
- [eslint-plugin-ember template-no-inline-styles](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-inline-styles.md)
58 changes: 58 additions & 0 deletions lib/rules/template-no-inline-event-handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow DOM event handler attributes',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-inline-event-handlers.md',
},
fixable: null,
schema: [],
messages: {
unexpected:
'Do not use inline event handlers like "{{name}}". Use the (on) modifier instead.',
},
},

create(context) {
const EVENT_HANDLER_ATTRS = new Set([
'onclick',
'ondblclick',
'onmousedown',
'onmouseup',
'onmousemove',
'onmouseout',
'onmouseover',
'onkeydown',
'onkeyup',
'onkeypress',
'onchange',
'oninput',
'onsubmit',
'onfocus',
'onblur',
'onload',
'onerror',
'onscroll',
]);

return {
GlimmerElementNode(node) {
if (node.attributes) {
for (const attr of node.attributes) {
if (attr.name && EVENT_HANDLER_ATTRS.has(attr.name.toLowerCase())) {
context.report({
node: attr,
messageId: 'unexpected',
data: { name: attr.name },
});
}
}
}
},
};
},
};
71 changes: 71 additions & 0 deletions tests/lib/rules/template-no-inline-event-handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/template-no-inline-event-handlers');
const RuleTester = require('eslint').RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});

ruleTester.run('template-no-inline-event-handlers', rule, {
valid: [
`<template>
<button {{on "click" this.handleClick}}>Click</button>
</template>`,
`<template>
<input {{on "input" this.handleInput}} />
</template>`,
`<template>
<div>No handlers</div>
</template>`,
],

invalid: [
{
code: `<template>
<button onclick="alert('test')">Click</button>
</template>`,
output: null,
errors: [
{
message:
'Do not use inline event handlers like "onclick". Use the (on) modifier instead.',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<div onmousedown="handleEvent()">Content</div>
</template>`,
output: null,
errors: [
{
message:
'Do not use inline event handlers like "onmousedown". Use the (on) modifier instead.',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<form onsubmit="return false;">Form</form>
</template>`,
output: null,
errors: [
{
message:
'Do not use inline event handlers like "onsubmit". Use the (on) modifier instead.',
type: 'GlimmerAttrNode',
},
],
},
],
});
Loading