Skip to content

Commit afd1342

Browse files
committed
Sync with ember-template-lint
1 parent cff1c83 commit afd1342

2 files changed

Lines changed: 45 additions & 74 deletions

File tree

docs/rules/template-no-passed-in-event-handlers.md

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,68 @@
22

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

5-
Disallows passing Ember event handler names directly as component arguments.
5+
It is possible to pass e.g. `@click` to an Ember component to override the default `click` event handler. For tagless components this will trigger an assertion though and can't be used as legitimate API, and for Glimmer components it will not work out of the box, like in Ember components, either.
66

7-
Passing Ember DOM event names as component arguments (e.g., `@click`, `@submit`, `@keyDown`) is discouraged. Use the `{{on}}` modifier on the element instead for explicit event binding.
8-
9-
## Rule Details
10-
11-
This rule detects component arguments whose name (without the `@` prefix) matches an Ember DOM event name. Only PascalCase component invocations are checked (built-in `Input` and `Textarea` are excluded).
12-
13-
The checked event names are:
14-
15-
`touchStart`, `touchMove`, `touchEnd`, `touchCancel`, `keyDown`, `keyUp`, `keyPress`, `mouseDown`, `mouseUp`, `contextMenu`, `click`, `doubleClick`, `mouseMove`, `mouseEnter`, `mouseLeave`, `focusIn`, `focusOut`, `submit`, `change`, `input`, `dragStart`, `drag`, `dragEnter`, `dragLeave`, `dragOver`, `dragEnd`, `drop`
7+
This rule scans potential component invocations for these patterns and flags them as issues.
168

179
## Examples
1810

19-
Examples of **incorrect** code for this rule:
11+
This rule **forbids** the following:
2012

21-
```gjs
22-
<template>
23-
<MyComponent @click={{this.handleClick}} />
24-
</template>
13+
```hbs
14+
<Foo @click={{this.handleClick}} />
2515
```
2616

27-
```gjs
28-
<template>
29-
<MyComponent @submit={{this.handleSubmit}} />
30-
</template>
17+
```hbs
18+
<Foo @keyPress={{this.handleClick}} />
3119
```
3220

33-
```gjs
34-
<template>
35-
<CustomButton @mouseEnter={{this.handleHover}} />
36-
</template>
21+
```hbs
22+
{{foo click=this.handleClick}}
3723
```
3824

39-
Examples of **correct** code for this rule:
25+
This rule **allows** the following:
4026

41-
```gjs
42-
<template>
43-
<MyComponent @action={{this.handleAction}} />
44-
</template>
27+
```hbs
28+
<Foo @onClick={{this.handleClick}} />
4529
```
4630

47-
```gjs
48-
<template>
49-
<button {{on "click" this.handleClick}}>Click</button>
50-
</template>
31+
```hbs
32+
<Foo @myCustomClickHandler={{this.handleClick}} />
5133
```
5234

53-
```gjs
54-
<template>
55-
<MyComponent @value={{this.value}} @onChange={{this.updateValue}} />
56-
</template>
35+
```hbs
36+
<Foo @onKeyPress={{this.handleClick}} />
5737
```
5838

59-
## Options
39+
```hbs
40+
{{foo onClick=this.handleClick}}
41+
```
6042

61-
| Name | Type | Default | Description |
62-
| -------- | -------- | ------- | -------------------------------------------------------------------------------------------------- |
63-
| `ignore` | `object` | `{}` | Per-component exceptions. Keys are component names, values are arrays of argument names to ignore. |
43+
## Configuration
6444

65-
```js
66-
module.exports = {
67-
rules: {
68-
'ember/template-no-passed-in-event-handlers': [
69-
'error',
70-
{
71-
ignore: {
72-
MyComponent: ['@click', '@submit'],
73-
},
74-
},
75-
],
76-
},
77-
};
78-
```
45+
- boolean - `true` to enable / `false` to disable
46+
- object -- An object with the following keys:
47+
- `ignore` -- An object with the following keys/values:
48+
- key: string -- The name of the element or mustache statement to ignore event handlers for
49+
- value: array -- Event handler names. Event handler names should exclude the @ when specifying those intended for named arguments. This rule will ensure both non-named arguments and named arguments are both ignored appropriately.
7950

80-
## Migration
51+
eg.
8152

82-
Replace:
53+
Given the following configuration:
8354

84-
```gjs
85-
<MyComponent @click={{this.handleClick}} />
86-
```
55+
```json
56+
{
57+
"ignore": {
58+
"MyButton": ["click"]
59+
}
60+
}
61+
```
8762

88-
With:
63+
## Migration
8964

90-
```gjs
91-
<MyComponent>
92-
<button {{on "click" this.handleClick}}>Click</button>
93-
</MyComponent>
94-
```
65+
- create explicit component APIs for these events (e.g. `@click` -> `@onClick`)
9566

9667
## References
9768

98-
- [eslint-plugin-ember template-no-passed-in-event-handlers](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-passed-in-event-handlers.md)
69+
- <https://api.emberjs.com/ember/release/classes/Component#event-handler-methods>

tests/lib/rules/template-no-passed-in-event-handlers.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,29 @@ ruleTester.run('template-no-passed-in-event-handlers', rule, {
123123
// ignore option — only ignores specified component (angle bracket)
124124
{
125125
code: '<template><Bar @click={{this.handleClick}} /></template>',
126-
options: [{ ignore: { Foo: ['@click'] } }],
127126
output: null,
127+
options: [{ ignore: { Foo: ['@click'] } }],
128128
errors: [{ messageId: 'unexpected' }],
129129
},
130130
// ignore option — only ignores specified attrs (angle bracket)
131131
{
132132
code: '<template><Foo @submit={{this.handleSubmit}} /></template>',
133-
options: [{ ignore: { Foo: ['@click'] } }],
134133
output: null,
134+
options: [{ ignore: { Foo: ['@click'] } }],
135135
errors: [{ messageId: 'unexpected' }],
136136
},
137137
// ignore option — only ignores specified component (curly)
138138
{
139139
code: '<template>{{bar click=this.handleClick}}</template>',
140-
options: [{ ignore: { foo: ['click'] } }],
141140
output: null,
141+
options: [{ ignore: { foo: ['click'] } }],
142142
errors: [{ messageId: 'unexpected' }],
143143
},
144144
// ignore option — only ignores specified attrs (curly)
145145
{
146146
code: '<template>{{foo submit=this.handleSubmit}}</template>',
147-
options: [{ ignore: { foo: ['click'] } }],
148147
output: null,
148+
options: [{ ignore: { foo: ['click'] } }],
149149
errors: [{ messageId: 'unexpected' }],
150150
},
151151
],
@@ -265,29 +265,29 @@ hbsRuleTester.run('template-no-passed-in-event-handlers', rule, {
265265
// ignore option — only ignores specified component (angle bracket)
266266
{
267267
code: '<Bar @click={{this.handleClick}} />',
268-
options: [{ ignore: { Foo: ['@click'] } }],
269268
output: null,
269+
options: [{ ignore: { Foo: ['@click'] } }],
270270
errors: [{ messageId: 'unexpected' }],
271271
},
272272
// ignore option — only ignores specified attrs (angle bracket)
273273
{
274274
code: '<Foo @submit={{this.handleSubmit}} />',
275-
options: [{ ignore: { Foo: ['@click'] } }],
276275
output: null,
276+
options: [{ ignore: { Foo: ['@click'] } }],
277277
errors: [{ messageId: 'unexpected' }],
278278
},
279279
// ignore option — only ignores specified component (curly)
280280
{
281281
code: '{{bar click=this.handleClick}}',
282-
options: [{ ignore: { foo: ['click'] } }],
283282
output: null,
283+
options: [{ ignore: { foo: ['click'] } }],
284284
errors: [{ messageId: 'unexpected' }],
285285
},
286286
// ignore option — only ignores specified attrs (curly)
287287
{
288288
code: '{{foo submit=this.handleSubmit}}',
289-
options: [{ ignore: { foo: ['click'] } }],
290289
output: null,
290+
options: [{ ignore: { foo: ['click'] } }],
291291
errors: [{ messageId: 'unexpected' }],
292292
},
293293
],

0 commit comments

Comments
 (0)