Skip to content

Commit 90f9504

Browse files
author
Adrián Bolonio
authored
Merge pull request #106 from github/accessibility/issues/1430/migrate-a11y-rubocop-rules-no-redundant-image-alt
Migrate accessibility rubocop rule `NoRedundantImageAlt` from dotcom to erblint-github
2 parents 3399237 + 039822b commit 90f9504

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

config/accessibility.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ require:
33

44
GitHub/Accessibility/ImageHasAlt:
55
Enabled: true
6-
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/image-has-alt.md
6+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/image-has-alt.md
7+
GitHub/Accessibility/NoRedundantImageAlt:
8+
Enabled: true
9+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/no-redundant-image-alt.md

guides/no-redundant-image-alt.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# GitHub/Accessibility/NoRedundantImageAlt
2+
3+
## Rule Details
4+
5+
Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image
6+
7+
## Resources
8+
9+
- [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/)
10+
- [Primer: Alternative text for images](https://primer.style/design/accessibility/alternative-text-for-images)
11+
12+
## Examples
13+
### **Incorrect** code for this rule 👎
14+
15+
```erb
16+
<%= image_tag "cat.gif", size: "12x12", alt: "Picture of a cat" %>
17+
```
18+
19+
### **Correct** code for this rule 👍
20+
21+
```erb
22+
<!-- good -->
23+
<%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %>
24+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
5+
module RuboCop
6+
module Cop
7+
module GitHub
8+
module Accessibility
9+
class NoRedundantImageAlt < Base
10+
MSG = "Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image"
11+
REDUNDANT_ALT_WORDS = %w(image picture)
12+
13+
def_node_search :redundant_alt?, "(pair (sym :alt) (str #contains_redundant_alt_text?))"
14+
15+
def on_send(node)
16+
receiver, method_name, _= *node
17+
18+
if receiver.nil? && method_name == :image_tag
19+
if redundant_alt?(node)
20+
add_offense(node.loc.selector)
21+
end
22+
end
23+
end
24+
25+
private
26+
27+
def contains_redundant_alt_text?(string)
28+
return false if string.empty?
29+
30+
if (string.downcase.split & REDUNDANT_ALT_WORDS).any?
31+
return true
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end
File renamed without changes.

test/test_no_redundant_image_alt.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_redundant_image_alt"
6+
7+
class NoRedundantImageAlt < CopTest
8+
def cop_class
9+
RuboCop::Cop::GitHub::Accessibility::NoRedundantImageAlt
10+
end
11+
12+
def test_no_redundant_image_alt_offense
13+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
14+
<%= image_tag "cat.gif", size: "12x12", alt: "Picture of a cat" %>
15+
ERB
16+
17+
assert_equal 1, offenses.count
18+
assert_equal "Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image", offenses[0].message
19+
end
20+
21+
def test_no_redundant_image_alt_no_offense
22+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
23+
<%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %>
24+
ERB
25+
26+
assert_equal 0, offenses.count
27+
end
28+
end

0 commit comments

Comments
 (0)