From 5daf4bc263937015547c2a5dfc1edd8082594ff3 Mon Sep 17 00:00:00 2001 From: Orhan Toy Date: Tue, 28 Jun 2022 12:50:45 +0200 Subject: [PATCH 01/47] Fix .rubocop.yml example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 328ed2ba..5d664e17 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ gem "rubocop-rails", require: false ``` yaml inherit_gem: rubocop-github: - - config/default_edge.yml - - config/rails_edge.yml + - config/default.yml + - config/rails.yml ``` ### Legacy usage From 36c807b3c497263393895b0494c455237f633c43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Bolonio?= Date: Thu, 28 Jul 2022 13:28:19 +0000 Subject: [PATCH 02/47] Add new a11y rubocop rule: ImageHasAlt --- lib/rubocop/cop/github/rails_image_has_alt.rb | 26 +++++++++++++++++ test/test_rails_image_has_alt.rb | 28 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 lib/rubocop/cop/github/rails_image_has_alt.rb create mode 100644 test/test_rails_image_has_alt.rb diff --git a/lib/rubocop/cop/github/rails_image_has_alt.rb b/lib/rubocop/cop/github/rails_image_has_alt.rb new file mode 100644 index 00000000..698d9224 --- /dev/null +++ b/lib/rubocop/cop/github/rails_image_has_alt.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "rubocop" + +module RuboCop + module Cop + module GitHub + module Accessibility + class ImageHasAlt < Base + MSG = "Images should have an alt prop with meaningful text or an empty string for decorative images" + + def_node_search :has_alt_attribute?, "(sym :alt)" + + def on_send(node) + receiver, method_name, _= *node + + if receiver.nil? && method_name == :image_tag + alt = has_alt_attribute?(node) + add_offense(node.loc.selector) if alt.nil? + end + end + end + end + end + end +end diff --git a/test/test_rails_image_has_alt.rb b/test/test_rails_image_has_alt.rb new file mode 100644 index 00000000..e241c9eb --- /dev/null +++ b/test/test_rails_image_has_alt.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require_relative "./cop_test" +require "minitest/autorun" +require "rubocop/cop/github/rails_image_has_alt" + +class TestImageHasAlt < CopTest + def cop_class + RuboCop::Cop::GitHub::Accessibility::ImageHasAlt + end + + def test_image_has_alt_offense + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + <%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %> + ERB + + assert_equal 1, offenses.count + assert_equal "Images should have an alt prop with meaningful text or an empty string for decorative images", offenses[0].message + end + + def test_image_has_alt_no_offense + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + <%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %> + ERB + + assert_equal 0, offenses.count + end +end From 19eb191a0ab31f82d41c9dd418d2caad3745bdcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Bolonio?= Date: Thu, 28 Jul 2022 13:45:50 +0000 Subject: [PATCH 03/47] Add new a11y rubocop rule: NoRedundantImageAlt --- .../github/rails_no_redundant_image_alt.rb | 38 +++++++++++++++++++ test/test_rails_no_redundant_image_alt.rb | 28 ++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 lib/rubocop/cop/github/rails_no_redundant_image_alt.rb create mode 100644 test/test_rails_no_redundant_image_alt.rb diff --git a/lib/rubocop/cop/github/rails_no_redundant_image_alt.rb b/lib/rubocop/cop/github/rails_no_redundant_image_alt.rb new file mode 100644 index 00000000..0f84a872 --- /dev/null +++ b/lib/rubocop/cop/github/rails_no_redundant_image_alt.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require "rubocop" + +module RuboCop + module Cop + module GitHub + module Accessibility + class NoRedundantImageAlt < Base + MSG = "Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image" + REDUNDANT_ALT_WORDS = %w(image picture) + + def_node_search :redundant_alt?, "(pair (sym :alt) (str #contains_redundant_alt_text?))" + + def on_send(node) + receiver, method_name, _= *node + + if receiver.nil? && method_name == :image_tag + if redundant_alt?(node) + add_offense(node.loc.selector) + end + end + end + + private + + def contains_redundant_alt_text?(string) + return false if string.empty? + + if (string.downcase.split & REDUNDANT_ALT_WORDS).any? + return true + end + end + end + end + end + end +end diff --git a/test/test_rails_no_redundant_image_alt.rb b/test/test_rails_no_redundant_image_alt.rb new file mode 100644 index 00000000..43101bb6 --- /dev/null +++ b/test/test_rails_no_redundant_image_alt.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require_relative "./cop_test" +require "minitest/autorun" +require "rubocop/cop/github/rails_no_redundant_image_alt" + +class NoRedundantImageAlt < CopTest + def cop_class + RuboCop::Cop::GitHub::Accessibility::NoRedundantImageAlt + end + + def test_no_redundant_image_alt_offense + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + <%= image_tag "cat.gif", size: "12x12", alt: "Picture of a cat" %> + ERB + + assert_equal 1, offenses.count + assert_equal "Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image", offenses[0].message + end + + def test_no_redundant_image_alt_no_offense + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + <%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %> + ERB + + assert_equal 0, offenses.count + end +end From ca3930a258b4225a745495d51852db8508eeb2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Bolonio?= Date: Thu, 28 Jul 2022 14:20:58 +0000 Subject: [PATCH 04/47] Add new a11y rubocop rule: NoPositiveTabindex --- .../cop/github/rails_no_positive_tabindex.rb | 32 +++++++++++++++++++ test/test_rails_no_positive_tabindex.rb | 28 ++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 lib/rubocop/cop/github/rails_no_positive_tabindex.rb create mode 100644 test/test_rails_no_positive_tabindex.rb diff --git a/lib/rubocop/cop/github/rails_no_positive_tabindex.rb b/lib/rubocop/cop/github/rails_no_positive_tabindex.rb new file mode 100644 index 00000000..2a63fbe1 --- /dev/null +++ b/lib/rubocop/cop/github/rails_no_positive_tabindex.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "rubocop" + +module RuboCop + module Cop + module GitHub + module Accessibility + class NoPositiveTabindex < Base + MSG = "Positive tabindex is error-prone and often inaccessible." + + def on_send(node) + receiver, method_name, *args = *node + if receiver.nil? + args.select do |arg| + arg.type == :hash + end.each do |hash| + hash.each_pair do |key, value| + next if key.type == :dsym + next unless key.respond_to?(:value) + if key.value == :tabindex && value.source.to_i > 0 + add_offense(hash) + end + end + end + end + end + end + end + end + end +end diff --git a/test/test_rails_no_positive_tabindex.rb b/test/test_rails_no_positive_tabindex.rb new file mode 100644 index 00000000..db174acf --- /dev/null +++ b/test/test_rails_no_positive_tabindex.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require_relative "./cop_test" +require "minitest/autorun" +require "rubocop/cop/github/rails_no_positive_tabindex" + +class NoPositiveTabindex < CopTest + def cop_class + RuboCop::Cop::GitHub::Accessibility::NoPositiveTabindex + end + + def test_no_positive_tabindex_alt_offense + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + <%= button_tag "Continue", :tabindex => 3 %> + ERB + + assert_equal 1, offenses.count + assert_equal "Positive tabindex is error-prone and often inaccessible.", offenses[0].message + end + + def test_no_positive_tabindex_alt_no_offense + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + <%= button_tag "Continue", :tabindex => -1 %> + ERB + + assert_equal 0, offenses.count + end +end From fb79321412edd95913cf2a55be900c0978c0b98a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Bolonio?= Date: Thu, 28 Jul 2022 14:54:39 +0000 Subject: [PATCH 05/47] Add new a11y rubocop rule: LinkHref --- lib/rubocop/cop/github/rails_link_href.rb | 25 ++++++++++++++++++++ test/test_rails_link_href.rb | 28 +++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/rubocop/cop/github/rails_link_href.rb create mode 100644 test/test_rails_link_href.rb diff --git a/lib/rubocop/cop/github/rails_link_href.rb b/lib/rubocop/cop/github/rails_link_href.rb new file mode 100644 index 00000000..f2165f7b --- /dev/null +++ b/lib/rubocop/cop/github/rails_link_href.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "rubocop" + +module RuboCop + module Cop + module GitHub + module Accessibility + class LinkHref < Base + MSG = "Links should go somewhere, you probably want to use a `