Skip to content

Migrate accessibility rubocop rule LinkHref from dotcom to erblint-github #108

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add new a11y rubocop rule: LinkHref
  • Loading branch information
Adrián Bolonio committed Jul 28, 2022
commit fb79321412edd95913cf2a55be900c0978c0b98a
25 changes: 25 additions & 0 deletions lib/rubocop/cop/github/rails_link_href.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "rubocop"

module RuboCop
module Cop
module GitHub
module Accessibility
class LinkHref < Base
MSG = "Links should go somewhere, you probably want to use a `<button>` instead.".freeze

def on_send(node)
receiver, method_name, *args = *node

if receiver.nil? && method_name == :link_to
if args.first.type == :str && args.first.children.first == "#"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this logic is wrong.

When using <%= link_to 'Go to GitHub', 'https://github.com/' %> then args is [s(:str, "Go to GitHub"), s(:str, "https://github.com/")] meaning that args.first is the content of the link, and the the second argument args[1] is the content of the href attribute.

@khiga8 what do you think? Shall we change the logic to check the second argument?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the test fail?

Maybe we should also add a test like this?

  def test_link_href_hash_offense
    offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
      <%= link_to 'Go to GitHub', '#' %>
    ERB

    assert_equal 1, offenses.count
    assert_equal "Links should go somewhere, you probably want to use a `<button>` instead.", offenses[0].message
  end

Seems like our code should pass on all three of the tests including this one?

Copy link

@khiga8 khiga8 Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh yeah this logic does seem off. nice catch!

it is checking if the label is # rather than the href. I was looking through link_to docs and it looks like there's various formats supported like:

link_to "GitHub", "#"

link_to nil, "http://github.com" 

link_to @profile do 
   Mona's profile
end 

We can iterate on this linter in the future but maybe for now we just check if the second argument exists, check if it's a string, check if it's # and use the test case @owenniblock proposed?

So maybe something like:

if args.length > 1 && args[1].type == :str && args[1].children.first == "#"

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a future follow-up, we could add support for block syntax like:

link_to '#' do 
   Mona's profile
end 

add_offense(node.loc.selector)
end
end
end
end
end
end
end
end
28 changes: 28 additions & 0 deletions test/test_rails_link_href.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/rails_link_href"

class LinkHref < CopTest
def cop_class
RuboCop::Cop::GitHub::Accessibility::LinkHref
end

def test_link_href_offense
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
<%= link_to 'Go to GitHub' %>
ERB

assert_equal 1, offenses.count
assert_equal "Links should go somewhere, you probably want to use a `<button>` instead.", offenses[0].message
end

def test_link_href_no_offense
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
<%= link_to 'Go to GitHub', 'https://github.com/' %>
ERB

assert_equal 0, offenses.count
end
end
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy