Skip to content

Commit f758a0a

Browse files
Merge pull request #2399 from NullVoxPopuli/nvp/template-lint-extract-rule-template-no-accesskey-attribute
Extract rule: template-no-accesskey-attribute
2 parents ffc4ad8 + 6cb63ef commit f758a0a

4 files changed

Lines changed: 153 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ rules in templates can be disabled with eslint directives with mustache or html
176176

177177
### Accessibility
178178

179-
| Name | Description | 💼 | 🔧 | 💡 |
180-
| :--------------------------------------------------------------------------- | :-------------------------------------- | :- | :- | :- |
181-
| [template-link-href-attributes](docs/rules/template-link-href-attributes.md) | require href attribute on link elements | | | |
179+
| Name | Description | 💼 | 🔧 | 💡 |
180+
| :------------------------------------------------------------------------------- | :-------------------------------------- | :- | :- | :- |
181+
| [template-link-href-attributes](docs/rules/template-link-href-attributes.md) | require href attribute on link elements | | | |
182+
| [template-no-accesskey-attribute](docs/rules/template-no-accesskey-attribute.md) | disallow accesskey attribute | | 🔧 | |
182183

183184
### Best Practices
184185

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# ember/template-no-accesskey-attribute
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Disallows the use of `accesskey` attribute on elements.
8+
9+
The `accesskey` attribute creates inconsistencies between keyboard shortcuts and keyboard commands used by screen readers and keyboard-only users, causing accessibility issues.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
<button accesskey="s">Save</button>
18+
</template>
19+
```
20+
21+
```gjs
22+
<template>
23+
<a href="#" accesskey="h">Home</a>
24+
</template>
25+
```
26+
27+
Examples of **correct** code for this rule:
28+
29+
```gjs
30+
<template>
31+
<button>Save</button>
32+
</template>
33+
```
34+
35+
```gjs
36+
<template>
37+
<a href="#">Home</a>
38+
</template>
39+
```
40+
41+
## References
42+
43+
- [eslint-plugin-ember no-accesskey-attribute](https://github.com/eslint-plugin-ember/eslint-plugin-ember/blob/master/docs/rule/no-accesskey-attribute.md)
44+
- [WebAIM: Keyboard Accessibility - Accesskey](https://webaim.org/techniques/keyboard/accesskey)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow accesskey attribute',
7+
category: 'Accessibility',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-accesskey-attribute.md',
11+
},
12+
fixable: 'code',
13+
schema: [],
14+
messages: {
15+
noAccesskey:
16+
'No access key attribute allowed. Inconsistencies between keyboard shortcuts and keyboard commands used by screenreader and keyboard only users create accessibility complications.',
17+
},
18+
},
19+
20+
create(context) {
21+
return {
22+
GlimmerElementNode(node) {
23+
const accesskeyAttr = node.attributes?.find((attr) => attr.name === 'accesskey');
24+
25+
if (accesskeyAttr) {
26+
context.report({
27+
node: accesskeyAttr,
28+
messageId: 'noAccesskey',
29+
fix(fixer) {
30+
// Remove the attribute including preceding whitespace
31+
const sourceCode = context.sourceCode;
32+
const text = sourceCode.getText();
33+
const attrStart = accesskeyAttr.range[0];
34+
const attrEnd = accesskeyAttr.range[1];
35+
36+
let removeStart = attrStart;
37+
while (removeStart > 0 && /\s/.test(text[removeStart - 1])) {
38+
removeStart--;
39+
}
40+
41+
return fixer.removeRange([removeStart, attrEnd]);
42+
},
43+
});
44+
}
45+
},
46+
};
47+
},
48+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-accesskey-attribute');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
//------------------------------------------------------------------------------
9+
// Tests
10+
//------------------------------------------------------------------------------
11+
12+
const ruleTester = new RuleTester({
13+
parser: require.resolve('ember-eslint-parser'),
14+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
15+
});
16+
17+
ruleTester.run('template-no-accesskey-attribute', rule, {
18+
valid: [
19+
`<template>
20+
<button>Click me</button>
21+
</template>`,
22+
`<template>
23+
<div class="button">Content</div>
24+
</template>`,
25+
],
26+
27+
invalid: [
28+
{
29+
code: `<template>
30+
<button accesskey="s">Save</button>
31+
</template>`,
32+
output: `<template>
33+
<button>Save</button>
34+
</template>`,
35+
errors: [
36+
{
37+
message:
38+
'No access key attribute allowed. Inconsistencies between keyboard shortcuts and keyboard commands used by screenreader and keyboard only users create accessibility complications.',
39+
type: 'GlimmerAttrNode',
40+
},
41+
],
42+
},
43+
{
44+
code: `<template>
45+
<a href="#" accesskey="h">Home</a>
46+
</template>`,
47+
output: `<template>
48+
<a href="#">Home</a>
49+
</template>`,
50+
errors: [
51+
{
52+
type: 'GlimmerAttrNode',
53+
},
54+
],
55+
},
56+
],
57+
});

0 commit comments

Comments
 (0)