Skip to content

Commit ca9741c

Browse files
Merge pull request #2395 from NullVoxPopuli/nvp/template-lint-extract-rule-template-link-href-attributes
Extract rule: template-link-href-attributes
2 parents a120029 + 90d0bd0 commit ca9741c

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ rules in templates can be disabled with eslint directives with mustache or html
174174
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
175175
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
176176

177+
### Accessibility
178+
179+
| Name | Description | 💼 | 🔧 | 💡 |
180+
| :--------------------------------------------------------------------------- | :-------------------------------------- | :- | :- | :- |
181+
| [template-link-href-attributes](docs/rules/template-link-href-attributes.md) | require href attribute on link elements | | | |
182+
177183
### Best Practices
178184

179185
| Name | Description | 💼 | 🔧 | 💡 |
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ember/template-link-href-attributes
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Requires `href` attribute on `<a>` elements.
6+
7+
Anchor elements should have an `href` attribute to be properly recognized as links by browsers and assistive technologies. If an element is meant to be interactive but not navigate, use a `<button>` instead.
8+
9+
## Rule Details
10+
11+
This rule ensures that all `<a>` elements have an `href` attribute.
12+
13+
## Examples
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```gjs
18+
<template>
19+
<a>Link</a>
20+
</template>
21+
```
22+
23+
```gjs
24+
<template>
25+
<a onclick={{this.handleClick}}>Click me</a>
26+
</template>
27+
```
28+
29+
```gjs
30+
<template>
31+
<a role="button">Action</a>
32+
</template>
33+
```
34+
35+
Examples of **correct** code for this rule:
36+
37+
```gjs
38+
<template>
39+
<a href="/about">About Us</a>
40+
</template>
41+
```
42+
43+
```gjs
44+
<template>
45+
<a href="https://example.com">External Link</a>
46+
</template>
47+
```
48+
49+
```gjs
50+
<template>
51+
<button {{on "click" this.handleClick}}>Click me</button>
52+
</template>
53+
```
54+
55+
## References
56+
57+
- [MDN: The Anchor element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)
58+
- [WebAIM: Links and Hypertext](https://webaim.org/techniques/hypertext/)
59+
- [ember-template-lint link-href-attributes](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/link-href-attributes.md)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'problem',
5+
docs: {
6+
description: 'require href attribute on link elements',
7+
category: 'Accessibility',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-link-href-attributes.md',
11+
},
12+
fixable: null,
13+
schema: [],
14+
messages: {
15+
missingHref:
16+
'<a> elements must have an href attribute. Use <button> for clickable elements that are not links.',
17+
},
18+
},
19+
20+
create(context) {
21+
return {
22+
GlimmerElementNode(node) {
23+
if (node.tag !== 'a') {
24+
return;
25+
}
26+
27+
const hasHref = node.attributes?.some((attr) => attr.name === 'href');
28+
29+
if (!hasHref) {
30+
context.report({
31+
node,
32+
messageId: 'missingHref',
33+
});
34+
}
35+
},
36+
};
37+
},
38+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const rule = require('../../../lib/rules/template-link-href-attributes');
2+
const RuleTester = require('eslint').RuleTester;
3+
4+
const ruleTester = new RuleTester({
5+
parser: require.resolve('ember-eslint-parser'),
6+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
7+
});
8+
9+
ruleTester.run('template-link-href-attributes', rule, {
10+
valid: [
11+
'<template><a href="/about">About</a></template>',
12+
'<template><a href="https://example.com">External</a></template>',
13+
'<template><button>Click me</button></template>',
14+
],
15+
16+
invalid: [
17+
{
18+
code: '<template><a>Link</a></template>',
19+
output: null,
20+
errors: [{ messageId: 'missingHref' }],
21+
},
22+
{
23+
code: '<template><a onclick="doSomething()">Click</a></template>',
24+
output: null,
25+
errors: [{ messageId: 'missingHref' }],
26+
},
27+
{
28+
code: '<template><a role="button">Action</a></template>',
29+
output: null,
30+
errors: [{ messageId: 'missingHref' }],
31+
},
32+
],
33+
});

0 commit comments

Comments
 (0)