Skip to content
Closed
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ require "erblint-github/linters"
```yaml
---
linters:
GitHub::Accessibility::ImageHasAlt:
Accessibility::ImageHasAlt:
enabled: true
GitHub::Accessibility::NoRedundantImageAlt:
Accessibility::NoRedundantImageAlt:
enabled: true
```

### Rules

- [GitHub::Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)
- [GitHub::Accessibility::ImageHasAlt](./docs/rules/accessibility/image-has-alt.md)
- [Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)
- [Accessibility::ImageHasAlt](./docs/rules/accessibility/image-has-alt.md)

## Testing

Expand Down
2 changes: 1 addition & 1 deletion lib/erblint-github/linters.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# frozen_string_literal: true

Dir[File.join(__dir__, "linters", "github/**/*.rb")].sort.each { |file| require file }
Dir[File.join(__dir__, "linters", "**/*.rb")].sort.each { |file| require file }
29 changes: 29 additions & 0 deletions lib/erblint-github/linters/accessibility/image_has_alt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative "../custom_helpers"

module ERBLint
module Linters
module Accessibility
class ImageHasAlt < Linter
include ERBLint::Linters::CustomHelpers
include LinterRegistry

MESSAGE = "<img> should have an alt prop with meaningful text or an empty string for decorative images"

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.name != "img"
next if tag.closing?

alt = possible_attribute_values(tag, "alt")

generate_offense(self.class, processed_source, tag) if alt.empty?
end

rule_disabled?(processed_source)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require_relative "../custom_helpers"

module ERBLint
module Linters
module Accessibility
class NoRedundantImageAlt < Linter
include ERBLint::Linters::CustomHelpers
include LinterRegistry

MESSAGE = "<img> alt prop should not contain `image` or `picture` as screen readers already announce the element as an image"
REDUNDANT_ALT_WORDS = %w[image picture].freeze

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.name != "img"
next if tag.closing?

alt = possible_attribute_values(tag, "alt").join
next if alt.empty?

generate_offense(self.class, processed_source, tag) if (alt.downcase.split & REDUNDANT_ALT_WORDS).any?
end

rule_disabled?(processed_source)
end
end
end
end
end

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion test/linters/accessibility/image_has_alt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ImageHasAltTest < LinterTestCase
def linter_class
ERBLint::Linters::GitHub::Accessibility::ImageHasAlt
ERBLint::Linters::Accessibility::ImageHasAlt
end

def test_warns_if_image_has_no_alt_attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class NoRedundantImageAltTest < LinterTestCase
def linter_class
ERBLint::Linters::GitHub::Accessibility::NoRedundantImageAlt
ERBLint::Linters::Accessibility::NoRedundantImageAlt
end

def test_warns_if_alt_contains_image
Expand Down