Skip to content
Closed
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-no-autofocus-attribute](docs/rules/template-no-autofocus-attribute.md) | disallow autofocus attribute | | 🔧 | |

### Best Practices

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

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

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

Disallows the use of `autofocus` attribute on elements.

The `autofocus` attribute can cause usability issues for both sighted and non-sighted users by disrupting expected behavior and screen reader announcements.

## Examples

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

```gjs
<template>
<input type="text" autofocus />
</template>
```

```gjs
<template>
<textarea autofocus></textarea>
</template>
```

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

```gjs
<template>
<input type="text" />
</template>
```

```gjs
<template>
<textarea></textarea>
</template>
```

## When Not To Use It

If you need to autofocus for specific accessibility or UX requirements and have thoroughly tested with assistive technologies, you may disable this rule for those specific cases.

## References

- [ember-template-lint no-autofocus-attribute](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-autofocus-attribute.md)
- [MDN autofocus attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus)
46 changes: 46 additions & 0 deletions lib/rules/template-no-autofocus-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow autofocus attribute',
category: 'Accessibility',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-autofocus-attribute.md',
},
fixable: 'code',
schema: [],
messages: {
noAutofocus:
'Avoid using autofocus attribute. Autofocusing elements can cause usability issues for sighted and non-sighted users.',
},
},

create(context) {
return {
GlimmerElementNode(node) {
const autofocusAttr = node.attributes?.find((attr) => attr.name === 'autofocus');

if (autofocusAttr) {
context.report({
node: autofocusAttr,
messageId: 'noAutofocus',
fix(fixer) {
// Remove the attribute including preceding whitespace
const sourceCode = context.sourceCode;
const text = sourceCode.getText();
const attrStart = autofocusAttr.range[0];
const attrEnd = autofocusAttr.range[1];

let removeStart = attrStart;
while (removeStart > 0 && /\s/.test(text[removeStart - 1])) {
removeStart--;
}

return fixer.removeRange([removeStart, attrEnd]);
},
});
}
},
};
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
},
"dependencies": {
"@ember-data/rfc395-data": "^0.0.4",
"@glimmer/env": "^0.1.7",
"css-tree": "^3.0.1",
"ember-eslint-parser": "^0.5.9",
"ember-rfc176-data": "^0.3.18",
Expand Down
57 changes: 57 additions & 0 deletions tests/lib/rules/template-no-autofocus-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/template-no-autofocus-attribute');
const RuleTester = require('eslint').RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

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

ruleTester.run('template-no-autofocus-attribute', rule, {
valid: [
`<template>
<input type="text" />
</template>`,
`<template>
<button>Click me</button>
</template>`,
],

invalid: [
{
code: `<template>
<input type="text" autofocus />
</template>`,
output: `<template>
<input type="text"/>
</template>`,
errors: [
{
message:
'Avoid using autofocus attribute. Autofocusing elements can cause usability issues for sighted and non-sighted users.',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<textarea autofocus></textarea>
</template>`,
output: `<template>
<textarea></textarea>
</template>`,
errors: [
{
type: 'GlimmerAttrNode',
},
],
},
],
});
Loading