Skip to content
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: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ linters:
enabled: true
GitHub::Accessibility::ImageHasAlt:
enabled: true
GitHub::Accessibility::LinkHasHrefCounter:
enabled: true
GitHub::Accessibility::NoAriaLabelMisuseCounter:
enabled: true
GitHub::Accessibility::NoPositiveTabIndex:
Expand All @@ -47,6 +49,7 @@ linters:
- [GitHub::Accessibility::AvoidGenericLinkTextCounter](./docs/rules/accessibility/avoid-generic-link-text-counter.md)
- [GitHub::Accessibility::IframeHasTitle](./docs/rules/accessibility/iframe-has-title.md)
- [GitHub::Accessibility::ImageHasAlt](./docs/rules/accessibility/image-has-alt.md)
- [GitHub::Accessibility::LinkHasHrefCounter](./docs/rules/accessibility/link_has_href-counter.md)
- [GitHub::Accessibility::NoAriaLabelMisuseCounter](./docs/rules/accessibility/no-aria-label-misuse-counter.md)
- [GitHub::Accessibility::NoPositiveTabIndex](./docs/rules/accessibility/no-positive-tab-index.md)
- [GitHub::Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)
Expand Down
27 changes: 27 additions & 0 deletions docs/rules/accessibility/link-has-href-counter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Link Has Href counter

## Rule Details

An `<a>` element that has an href attribute represents a hyperlink (a hypertext anchor) labeled by its contents. Links, `<a>` elements, should go somewhere, you probably want to use a `<button>` instead.


## Resources

- [`<a>`: The Anchor element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)
- [Primer: Links](https://primer.style/design/accessibility/links)
- [HTML Spec: The a element](https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-a-element)

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

```erb
<!-- incorrect -->
<a>Go to GitHub</a>
```

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

```erb
<!-- correct -->
<a href='https://github.com/'>Go to GitHub</a>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require_relative "../../custom_helpers"

module ERBLint
module Linters
module GitHub
module Accessibility
class LinkHasHrefCounter < Linter
include ERBLint::Linters::CustomHelpers
include LinterRegistry

MESSAGE = "Links should go somewhere, you probably want to use a `<button>` instead."

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

href = possible_attribute_values(tag, "href")
name = tag.attributes["name"]
generate_offense(self.class, processed_source, tag) if (!name && href.empty?) || href.include?("#")
end

counter_correct?(processed_source)
end

def autocorrect(processed_source, offense)
return unless offense.context

lambda do |corrector|
if processed_source.file_content.include?("erblint:counter #{simple_class_name}")
# update the counter if exists
corrector.replace(offense.source_range, offense.context)
else
# add comment with counter if none
corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n")
end
end
end
end
end
end
end
end
59 changes: 59 additions & 0 deletions test/linters/accessibility/link_has_href_counter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require "test_helper"

class LinkHasHrefCounter < LinterTestCase
def linter_class
ERBLint::Linters::GitHub::Accessibility::LinkHasHrefCounter
end

def test_warns_if_link_has_no_href_attribute
@file = "<a>Go to GitHub</a>"
@linter.run(processed_source)

assert_equal(2, @linter.offenses.count)
error_messages = @linter.offenses.map(&:message).sort
assert_match(/If you must, add <%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %> to bypass this check./, error_messages.first)
assert_match(/Links should go somewhere, you probably want to use a `<button>` instead./, error_messages.last)
end

def test_does_not_warn_if_link_has_href_attribute
@file = "<a href='https://github.com/'>Go to GitHub</a>"
@linter.run(processed_source)

assert_empty @linter.offenses
end

def test_does_not_warn_if_link_has_href_attribute_and_has_correct_counter_comment
@file = <<~ERB
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %>
<a>Go to GitHub</a>
ERB
@linter.run(processed_source)

assert_equal 0, @linter.offenses.count
end

def test_does_not_autocorrect_when_ignores_are_correct
@file = <<~ERB
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %>
<a>Go to GitHub</a>
ERB

assert_equal @file, corrected_content
end

def test_does_autocorrect_when_ignores_are_not_correct
@file = <<~ERB
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 3 %>
<a>Go to GitHub</a>
ERB
refute_equal @file, corrected_content

expected_content = <<~ERB
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %>
<a>Go to GitHub</a>
ERB
assert_equal expected_content, corrected_content
end
end