Skip to content

Commit 8f967fa

Browse files
author
Adrián Bolonio
committed
Update linter rule LinkHasHref to LinkHasHrefCounter
1 parent be03f35 commit 8f967fa

File tree

5 files changed

+83
-32
lines changed

5 files changed

+83
-32
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ linters:
3131
enabled: true
3232
GitHub::Accessibility::ImageHasAlt:
3333
enabled: true
34-
GitHub::Accessibility::LinkHasHref:
34+
GitHub::Accessibility::LinkHasHrefCounter:
3535
enabled: true
3636
GitHub::Accessibility::NoAriaLabelMisuseCounter:
3737
enabled: true
@@ -49,7 +49,7 @@ linters:
4949
- [GitHub::Accessibility::AvoidGenericLinkTextCounter](./docs/rules/accessibility/avoid-generic-link-text-counter.md)
5050
- [GitHub::Accessibility::IframeHasTitle](./docs/rules/accessibility/iframe-has-title.md)
5151
- [GitHub::Accessibility::ImageHasAlt](./docs/rules/accessibility/image-has-alt.md)
52-
- [GitHub::Accessibility::LinkHasHref](./docs/rules/accessibility/link_has_href.md)
52+
- [GitHub::Accessibility::LinkHasHrefCounter](./docs/rules/accessibility/link_has_href-counter.md)
5353
- [GitHub::Accessibility::NoAriaLabelMisuseCounter](./docs/rules/accessibility/no-aria-label-misuse-counter.md)
5454
- [GitHub::Accessibility::NoPositiveTabIndex](./docs/rules/accessibility/no-positive-tab-index.md)
5555
- [GitHub::Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)

docs/rules/accessibility/link-has-href.md renamed to docs/rules/accessibility/link-has-href-counter.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# Link Has Href
1+
# Link Has Href counter
22

33
## Rule Details
44

5-
Links should go somewhere, you probably want to use a `<button>` instead.
5+
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.
6+
67

78
## Resources
89

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

1214
## Examples
1315
### **Incorrect** code for this rule 👎
@@ -22,4 +24,4 @@ Links should go somewhere, you probably want to use a `<button>` instead.
2224
```erb
2325
<!-- correct -->
2426
<a href='https://github.com/'>Go to GitHub</a>
25-
```
27+
```

lib/erblint-github/linters/github/accessibility/link_has_href.rb renamed to lib/erblint-github/linters/github/accessibility/link_has_href_counter.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ module ERBLint
66
module Linters
77
module GitHub
88
module Accessibility
9-
class LinkHasHref < Linter
9+
class LinkHasHrefCounter < Linter
1010
include ERBLint::Linters::CustomHelpers
1111
include LinterRegistry
1212

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

1515
def run(processed_source)
1616
tags(processed_source).each do |tag|
@@ -19,11 +19,24 @@ def run(processed_source)
1919

2020
href = possible_attribute_values(tag, "href")
2121
name = tag.attributes["name"]
22-
2322
generate_offense(self.class, processed_source, tag) if (!name && href.empty?) || href.include?("#")
2423
end
2524

26-
rule_disabled?(processed_source)
25+
counter_correct?(processed_source)
26+
end
27+
28+
def autocorrect(processed_source, offense)
29+
return unless offense.context
30+
31+
lambda do |corrector|
32+
if processed_source.file_content.include?("erblint:counter #{simple_class_name}")
33+
# update the counter if exists
34+
corrector.replace(offense.source_range, offense.context)
35+
else
36+
# add comment with counter if none
37+
corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n")
38+
end
39+
end
2740
end
2841
end
2942
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class LinkHasHrefCounter < LinterTestCase
6+
def linter_class
7+
ERBLint::Linters::GitHub::Accessibility::LinkHasHrefCounter
8+
end
9+
10+
def test_warns_if_link_has_no_href_attribute
11+
@file = "<a>Go to GitHub</a>"
12+
@linter.run(processed_source)
13+
14+
assert_equal(2, @linter.offenses.count)
15+
error_messages = @linter.offenses.map(&:message).sort
16+
assert_match(/If you must, add <%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %> to bypass this check./, error_messages.first)
17+
assert_match(/Links should go somewhere, you probably want to use a `<button>` instead./, error_messages.last)
18+
end
19+
20+
def test_does_not_warn_if_link_has_href_attribute
21+
@file = "<a href='https://github.com/'>Go to GitHub</a>"
22+
@linter.run(processed_source)
23+
24+
assert_empty @linter.offenses
25+
end
26+
27+
def test_does_not_warn_if_link_has_href_attribute_and_has_correct_counter_comment
28+
@file = <<~ERB
29+
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %>
30+
<a>Go to GitHub</a>
31+
ERB
32+
@linter.run(processed_source)
33+
34+
assert_equal 0, @linter.offenses.count
35+
end
36+
37+
def test_does_not_autocorrect_when_ignores_are_correct
38+
@file = <<~ERB
39+
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %>
40+
<a>Go to GitHub</a>
41+
ERB
42+
43+
assert_equal @file, corrected_content
44+
end
45+
46+
def test_does_autocorrect_when_ignores_are_not_correct
47+
@file = <<~ERB
48+
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 3 %>
49+
<a>Go to GitHub</a>
50+
ERB
51+
refute_equal @file, corrected_content
52+
53+
expected_content = <<~ERB
54+
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %>
55+
<a>Go to GitHub</a>
56+
ERB
57+
assert_equal expected_content, corrected_content
58+
end
59+
end

test/linters/accessibility/link_has_href_test.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)