Skip to content

Commit 1be40db

Browse files
committed
Port over no positive tabindex rule
Add tests and documentation
1 parent 8226a4b commit 1be40db

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ linters:
3131
enabled: true
3232
GitHub::Accessibility::NoAriaLabelMisuse:
3333
enabled: true
34+
GitHub::Accessibility::NoPositiveTabIndex:
35+
enabled: true
3436
GitHub::Accessibility::NoRedundantImageAlt:
3537
enabled: true
3638
```
@@ -41,6 +43,7 @@ linters:
4143
- [GitHub::Accessibility::IframeHasTitle](./docs/rules/accessibility/iframe-has-title.md)
4244
- [GitHub::Accessibility::ImageHasAlt](./docs/rules/accessibility/image-has-alt.md)
4345
- [GitHub::Accessibility::NoAriaLabelMisuse](./docs/rules/accessibility/no-aria-label-misuse.md)
46+
- [GitHub::Accessibility::NoPositiveTabIndex](./docs/rules/accessibility/no-positive-tab-index.md)
4447
- [GitHub::Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)
4548
4649
## Testing
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# No positive tab index
2+
3+
## Rule Details
4+
5+
Do not positive tabindex as it is error prone and can severely disrupt navigation experience for keyboard users.
6+
7+
Learn more at:
8+
9+
- [A11yProject: Use the tabindex attribute](https://www.a11yproject.com/posts/how-to-use-the-tabindex-attribute/)
10+
- [Deque: Avoid using Tabindex with positive numbers](https://dequeuniversity.com/tips/tabindex-positive-numbers)
11+
12+
### 👎 Examples of **incorrect** code for this rule:
13+
14+
```erb
15+
<button tabindex="3"></button>
16+
<button tabindex="1"></button>
17+
```
18+
19+
### 👍 Examples of **correct** code for this rule:
20+
21+
```erb
22+
<!-- good -->
23+
<button tabindex="0"></button>
24+
```
25+
26+
```erb
27+
<!-- also good -->
28+
<button tabindex="-1"></button>
29+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../custom_helpers"
4+
5+
module ERBLint
6+
module Linters
7+
module GitHub
8+
module Accessibility
9+
class NoPositiveTabIndex < Linter
10+
include ERBLint::Linters::CustomHelpers
11+
include LinterRegistry
12+
13+
MESSAGE = "Do not use positive tabindex as it is error prone and can severely disrupt navigation experience for keyboard users"
14+
15+
def run(processed_source)
16+
tags(processed_source).each do |tag|
17+
next if tag.closing?
18+
next unless tag.attributes["tabindex"]&.value.to_i.positive?
19+
20+
generate_offense(self.class, processed_source, tag)
21+
end
22+
23+
rule_disabled?(processed_source)
24+
end
25+
end
26+
end
27+
end
28+
end
29+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class NoPositiveTabIndexTest < LinterTestCase
6+
def linter_class
7+
ERBLint::Linters::GitHub::Accessibility::NoPositiveTabIndex
8+
end
9+
10+
def test_warns_if_positive_tabindex_is_used
11+
@file = "<button tabindex='1'></button>"
12+
@linter.run(processed_source)
13+
14+
refute_empty @linter.offenses
15+
end
16+
17+
def test_does_not_warn_if_negative_tabindex_is_used
18+
@file = "<button tabindex='-1'></button>"
19+
@linter.run(processed_source)
20+
21+
assert_empty @linter.offenses
22+
end
23+
24+
def test_does_not_warn_if_zero_tabindex_is_used
25+
@file = "<button tabindex='0'></button>"
26+
@linter.run(processed_source)
27+
28+
assert_empty @linter.offenses
29+
end
30+
end

0 commit comments

Comments
 (0)