Skip to content

Commit 83cb834

Browse files
author
Adrián Bolonio
authored
Migrate accessibility rubocop rule LinkHref from dotcom to erblint-github
Migrate accessibility rubocop rule `LinkHref` from dotcom to erblint-github
2 parents 3e11dc9 + 3fc0b67 commit 83cb834

4 files changed

Lines changed: 92 additions & 0 deletions

File tree

config/accessibility.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ require:
44
GitHub/Accessibility/ImageHasAlt:
55
Enabled: true
66
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/image-has-alt.md
7+
GitHub/Accessibility/LinkHasHref:
8+
Enabled: true
9+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/link-has-href.md
710
GitHub/Accessibility/NoPositiveTabindex:
811
Enabled: true
912
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/no-positive-tabindex.md

guides/link-has-href.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# GitHub/Accessibility/LinkHasHref
2+
3+
## Rule Details
4+
5+
Links should go somewhere, you probably want to use a `<button>` instead.
6+
7+
## Resources
8+
9+
- [`<a>`: The Anchor element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)
10+
- [Primer: Links](https://primer.style/design/accessibility/links)
11+
- [HTML Spec: The a element](https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-a-element)
12+
13+
## Examples
14+
### **Incorrect** code for this rule 👎
15+
16+
```erb
17+
<%= link_to 'Go to GitHub' %>
18+
```
19+
20+
```erb
21+
<%= link_to 'Go to GitHub', '#' %>
22+
```
23+
24+
### **Correct** code for this rule 👍
25+
26+
```erb
27+
<!-- good -->
28+
<%= link_to 'Go to GitHub', 'https://github.com/' %>
29+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
5+
module RuboCop
6+
module Cop
7+
module GitHub
8+
module Accessibility
9+
class LinkHasHref < Base
10+
MSG = "Links should go somewhere, you probably want to use a `<button>` instead.".freeze
11+
12+
def on_send(node)
13+
receiver, method_name, *args = *node
14+
15+
if receiver.nil? && method_name == :link_to
16+
if args.length == 1 || (args.length > 1 && args[1].type == :str && args[1].children.first == "#")
17+
add_offense(node.loc.selector)
18+
end
19+
end
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end

test/test_link_has_href.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "./cop_test"
4+
require "minitest/autorun"
5+
require "rubocop/cop/github/accessibility/link_has_href"
6+
7+
class LinkHasHref < CopTest
8+
def cop_class
9+
RuboCop::Cop::GitHub::Accessibility::LinkHasHref
10+
end
11+
12+
def test_link_has_href_offense
13+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
14+
<%= link_to 'Go to GitHub' %>
15+
ERB
16+
17+
assert_equal 1, offenses.count
18+
assert_equal "Links should go somewhere, you probably want to use a `<button>` instead.", offenses[0].message
19+
20+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
21+
<%= link_to 'Go to GitHub', '#' %>
22+
ERB
23+
24+
assert_equal 1, offenses.count
25+
assert_equal "Links should go somewhere, you probably want to use a `<button>` instead.", offenses[0].message
26+
end
27+
28+
def test_link_has_href_no_offense
29+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
30+
<%= link_to 'Go to GitHub', 'https://github.com/' %>
31+
ERB
32+
33+
assert_equal 0, offenses.count
34+
end
35+
end

0 commit comments

Comments
 (0)