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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ rules in templates can be disabled with eslint directives with mustache or html
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

### Accessibility

| Name | Description | 💼 | 🔧 | 💡 |
| :--------------------------------------------------------------------------- | :-------------------------------------- | :- | :- | :- |
| [template-link-href-attributes](docs/rules/template-link-href-attributes.md) | require href attribute on link elements | | | |

### Best Practices

| Name | Description | 💼 | 🔧 | 💡 |
Expand Down
59 changes: 59 additions & 0 deletions docs/rules/template-link-href-attributes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# ember/template-link-href-attributes

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

Requires `href` attribute on `<a>` elements.

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.

## Rule Details

This rule ensures that all `<a>` elements have an `href` attribute.

## Examples

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

```gjs
<template>
<a>Link</a>
</template>
```

```gjs
<template>
<a onclick={{this.handleClick}}>Click me</a>
</template>
```

```gjs
<template>
<a role="button">Action</a>
</template>
```

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

```gjs
<template>
<a href="/about">About Us</a>
</template>
```

```gjs
<template>
<a href="https://example.com">External Link</a>
</template>
```

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

## References

- [MDN: The Anchor element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)
- [WebAIM: Links and Hypertext](https://webaim.org/techniques/hypertext/)
- [ember-template-lint link-href-attributes](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/link-href-attributes.md)
38 changes: 38 additions & 0 deletions lib/rules/template-link-href-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require href attribute on link elements',
category: 'Accessibility',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-link-href-attributes.md',
},
fixable: null,
schema: [],
messages: {
missingHref:
'<a> elements must have an href attribute. Use <button> for clickable elements that are not links.',
},
},

create(context) {
return {
GlimmerElementNode(node) {
if (node.tag !== 'a') {
return;
}

const hasHref = node.attributes?.some((attr) => attr.name === 'href');

if (!hasHref) {
context.report({
node,
messageId: 'missingHref',
});
}
},
};
},
};
33 changes: 33 additions & 0 deletions tests/lib/rules/template-link-href-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const rule = require('../../../lib/rules/template-link-href-attributes');
const RuleTester = require('eslint').RuleTester;

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

ruleTester.run('template-link-href-attributes', rule, {
valid: [
'<template><a href="/about">About</a></template>',
'<template><a href="https://example.com">External</a></template>',
'<template><button>Click me</button></template>',
],

invalid: [
{
code: '<template><a>Link</a></template>',
output: null,
errors: [{ messageId: 'missingHref' }],
},
{
code: '<template><a onclick="doSomething()">Click</a></template>',
output: null,
errors: [{ messageId: 'missingHref' }],
},
{
code: '<template><a role="button">Action</a></template>',
output: null,
errors: [{ messageId: 'missingHref' }],
},
],
});
Loading