Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate accessibility rubocop rule ImageHasAlt from dotcom to erblint-github #105

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ inherit_gem:
rubocop-github:
- config/default.yml
- config/rails.yml
- config/accessibility.yml
```

### Legacy usage
Expand All @@ -33,4 +34,4 @@ If you are using a rubocop version < 1.0.0, you can use rubocop-github version

## The Cops

All cops are located under [`lib/rubocop/cop/github`](lib/rubocop/cop/github), and contain examples/documentation.
All cops are located under [`lib/rubocop/cop/github`](lib/rubocop/cop/github) and [`lib/rubocop/cop/github/accessibility`](lib/rubocop/cop/github/accessibility), and contain examples/documentation.
6 changes: 6 additions & 0 deletions config/accessibility.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require:
- rubocop/cop/github/accessibility

GitHub/Accessibility/ImageHasAlt:
Enabled: true
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/image-has-alt.md
29 changes: 29 additions & 0 deletions guides/image-has-alt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# GitHub/Accessibility/ImageHasAlt

## Rule Details

Images should have an alt prop with meaningful text or an empty string for decorative images.

## Resources

- [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/)
- [Primer: Alternative text for images](https://primer.style/design/accessibility/alternative-text-for-images)

## Examples
### **Incorrect** code for this rule 👎

```erb
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %>
```

### **Correct** code for this rule 👍

```erb
<!-- good -->
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %>
```

```erb
<!-- also good -->
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "" %>
```
26 changes: 26 additions & 0 deletions lib/rubocop/cop/github/accessibility/image_has_alt.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions test/accessibility/test_image_has_alt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative "./cop_test"
require "minitest/autorun"
require "rubocop/cop/github/accessibility/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