Skip to content

Commit bb358f2

Browse files
author
Adrián Bolonio
committed
Add new a11y rubocop rule: NoPositiveTabindex
1 parent b76dd8a commit bb358f2

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
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
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/rails_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)