-
Notifications
You must be signed in to change notification settings - Fork 860
feat(eslint-plugin): add new @elastic/eui/accessible-interactive-element rule
#9093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Added new `accessible-interactive-element` rule. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/eslint-plugin/src/rules/a11y/accessible_interactive_element.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { ESLintUtils, type TSESTree } from '@typescript-eslint/utils'; | ||
| import { INTERACTIVE_EUI_COMPONENTS } from '../../utils/constants'; | ||
| import { extractAttrValue } from "../../utils/get_attr_value"; | ||
|
|
||
| export const AccessibleInteractiveElements = ESLintUtils.RuleCreator.withoutDocs({ | ||
| create(context) { | ||
| return { | ||
| JSXOpeningElement(node) { | ||
| if (node.name.type !== 'JSXIdentifier') return; | ||
| const componentName = node.name.name; | ||
| if (!INTERACTIVE_EUI_COMPONENTS.includes(componentName)) return; | ||
|
|
||
| const tabIndexAttribute = node.attributes.find( | ||
| (attr): attr is TSESTree.JSXAttribute => | ||
| attr.type === 'JSXAttribute' && | ||
| attr.name.type === 'JSXIdentifier' && | ||
| attr.name.name === 'tabIndex' | ||
| ); | ||
|
|
||
|
|
||
| if (tabIndexAttribute && (Number(extractAttrValue(context, tabIndexAttribute)) || 0) === -1) { | ||
| context.report({ | ||
| node: node, | ||
| messageId: 'disallowTabIndex', | ||
| data: { component: componentName }, | ||
| fix: fixer => fixer.remove(tabIndexAttribute), | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Ensure interactive EUI components remain accessible by prohibiting tabIndex={-1}, which removes them from keyboard navigation.', | ||
| }, | ||
| fixable: 'code', | ||
| schema: [], | ||
| messages: { | ||
| disallowTabIndex: '{{component}} is an interactive EUI component and must not use tabIndex={-1}, as this removes it from keyboard navigation and impairs accessibility.', | ||
| }, | ||
| }, | ||
| defaultOptions: [], | ||
| }); | ||
144 changes: 144 additions & 0 deletions
144
packages/eslint-plugin/src/rules/a11y/accessible_interactive_elements.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import dedent from 'dedent'; | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import { AccessibleInteractiveElements } from './accessible_interactive_element'; | ||
|
|
||
| const languageOptions = { | ||
| parserOptions: { | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run('accessible-interactive-element', AccessibleInteractiveElements, { | ||
| valid: [ | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiButton>Click me</EuiButton> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiButton tabIndex={0}>Focusable</EuiButton> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiLink href="#">Link</EuiLink> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <span tabIndex={-1}>Not interactive EUI</span> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiButton tabIndex={1}>Custom tab order</EuiButton> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiButton tabIndex={-1}>Should not be focusable</EuiButton> | ||
| ) | ||
| `, | ||
| output: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiButton >Should not be focusable</EuiButton> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowTabIndex', | ||
| data: { component: 'EuiButton' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiLink tabIndex={-1} href="#">Link</EuiLink> | ||
| ) | ||
| `, | ||
| output: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiLink href="#">Link</EuiLink> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowTabIndex', | ||
| data: { component: 'EuiLink' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiButton tabIndex={-1} color="primary">Primary</EuiButton> | ||
| ) | ||
| `, | ||
| output: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiButton color="primary">Primary</EuiButton> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowTabIndex', | ||
| data: { component: 'EuiButton' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiBadge tabIndex={-1} /> | ||
| ) | ||
| `, | ||
| output: dedent` | ||
| const MyComponent = () => ( | ||
| <EuiBadge /> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowTabIndex', | ||
| data: { component: 'EuiBadge' }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doubt:
So we cannot reuse
findAttrValuehere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, maybe name not so clear but here for
fixmethod we need ref totabIndexAttribute