Skip to content

Commit 3e11dc9

Browse files
author
Adrián Bolonio
authored
Merge pull request #107 from github/accessibility/issues/1430/migrate-a11y-rubocop-rules-no-positive-tabindex
Migrate accessibility rubocop rule `NoPositiveTabindex` from dotcom to erblint-github
2 parents 90f9504 + bb56929 commit 3e11dc9

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

config/accessibility.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ require:
44
GitHub/Accessibility/ImageHasAlt:
55
Enabled: true
66
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/image-has-alt.md
7+
GitHub/Accessibility/NoPositiveTabindex:
8+
Enabled: true
9+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/no-positive-tabindex.md
710
GitHub/Accessibility/NoRedundantImageAlt:
811
Enabled: true
9-
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/no-redundant-image-alt.md
12+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/no-redundant-image-alt.md

guides/no-positive-tabindex.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# GitHub/Accessibility/NoPositiveTabindex
2+
3+
## Rule Details
4+
5+
Positive tabindex is error-prone and often inaccessible.
6+
7+
## Resources
8+
9+
- [F44: Failure of Success Criterion 2.4.3 due to using tabindex to create a tab order that does not preserve meaning and operability](https://www.w3.org/TR/WCAG20-TECHS/F44.html)
10+
- [Deque University: Avoid Using Tabindex with Positive Numbers](https://dequeuniversity.com/tips/tabindex-positive-numbers)
11+
12+
## Examples
13+
### **Incorrect** code for this rule 👎
14+
15+
```erb
16+
<%= button_tag "Continue", :tabindex => 3 %>
17+
```
18+
19+
### **Correct** code for this rule 👍
20+
21+
```erb
22+
<!-- good -->
23+
<%= button_tag "Continue", :tabindex => -1 %>
24+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
5+
module RuboCop
6+
module Cop
7+
module GitHub
8+
module Accessibility
9+
class NoPositiveTabindex < Base
10+
MSG = "Positive tabindex is error-prone and often inaccessible."
11+
12+
def on_send(node)
13+
receiver, method_name, *args = *node
14+
if receiver.nil?
15+
args.select do |arg|
16+
arg.type == :hash
17+
end.each do |hash|
18+
hash.each_pair do |key, value|
19+
next if key.type == :dsym
20+
next unless key.respond_to?(:value)
21+
if key.value == :tabindex && value.source.to_i > 0
22+
add_offense(hash)
23+
end
24+
end
25+
end
26+
end
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end

test/test_no_positive_tabindex.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "./cop_test"
4+
require "minitest/autorun"
5+
require "rubocop/cop/github/accessibility/no_positive_tabindex"
6+
7+
class NoPositiveTabindex < CopTest
8+
def cop_class
9+
RuboCop::Cop::GitHub::Accessibility::NoPositiveTabindex
10+
end
11+
12+
def test_no_positive_tabindex_alt_offense
13+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
14+
<%= button_tag "Continue", :tabindex => 3 %>
15+
ERB
16+
17+
assert_equal 1, offenses.count
18+
assert_equal "Positive tabindex is error-prone and often inaccessible.", offenses[0].message
19+
end
20+
21+
def test_no_positive_tabindex_alt_no_offense
22+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
23+
<%= button_tag "Continue", :tabindex => -1 %>
24+
ERB
25+
26+
assert_equal 0, offenses.count
27+
end
28+
end

0 commit comments

Comments
 (0)