Skip to content

Commit

Permalink
fix(eslint-plugin-template): [click-events-have-key-events]: handle a…
Browse files Browse the repository at this point in the history
…dditional outputs (#1101)
  • Loading branch information
sandikbarr committed Sep 18, 2022
1 parent e25ef92 commit c608cdb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ The rule does not have any configuration options.

#### ❌ Invalid Code

```html
<div (click)="onClick()" (handleSomething)="handleSomething()"></div>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/template/click-events-have-key-events": [
"error"
]
}
}
```

<br>

#### ❌ Invalid Code

```html
<header (click)="onClick()"></header>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -333,6 +360,8 @@ The rule does not have any configuration options.

```html
<div (click)="onClick()" (keyup)="onKeyup()"></div>
<div (keyup)="onKeyup()" (click)="onClick()"></div>
<div (click)="onClick()" (keyup)="onKeyup()" (handleSomething)="handleSomething()"></div>
```

<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export default createESLintRule<Options, MessageIds>({
hasKeyEvent = false;

for (const output of node.outputs) {
hasClick = output.name === 'click';
hasClick = hasClick || output.name === 'click';
hasKeyEvent =
hasKeyEvent ||
output.name.startsWith('keyup') ||
output.name.startsWith('keydown') ||
output.name.startsWith('keypress');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const messageId: MessageIds = 'clickEventsHaveKeyEvents';
export const valid = [
{
// It should work when click events are associated with key events.
code: '<div (click)="onClick()" (keyup)="onKeyup()"></div>',
code: `
<div (click)="onClick()" (keyup)="onKeyup()"></div>
<div (keyup)="onKeyup()" (click)="onClick()"></div>
<div (click)="onClick()" (keyup)="onKeyup()" (handleSomething)="handleSomething()"></div>
`,
},
{
// It should work when click events are associated with key pseudo events.
Expand Down Expand Up @@ -57,6 +61,15 @@ export const invalid = [
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`,
}),
convertAnnotatedSourceToFailureCase({
messageId,
description:
'should fail when click is not accompanied with key events and there are additional outputs',
annotatedSource: `
<div (click)="onClick()" (handleSomething)="handleSomething()"></div>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`,
}),
convertAnnotatedSourceToFailureCase({
messageId,
description:
Expand Down

0 comments on commit c608cdb

Please sign in to comment.