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

Conversation

bolonio
Copy link

@bolonio bolonio commented Jul 28, 2022

Context

The motivation of rubocop-github is to open-source our accessibility rubocop rules so non-GitHub people can benefit from them, have a space to provide comprehensive rule documentation, and also allow rules to be shared between Rails projects.

This PR migrates the accessibility rule LinkHref from dotcom to rubocop-github

Related Issue

@bolonio bolonio requested review from owenniblock and khiga8 July 28, 2022 14:55
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 

@bolonio bolonio requested a review from joelhawksley July 29, 2022 10:17
@bolonio bolonio requested a review from khiga8 August 1, 2022 09:18
@@ -0,0 +1,35 @@
# frozen_string_literal: true
Copy link

Choose a reason for hiding this comment

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

Should this be in test/accessibility/?

Copy link
Author

Choose a reason for hiding this comment

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

I didn’t put the tests inside a subfolder because bundle exec rake doesn’t find them. It only executes the test on the test folder, no subfolders

@bolonio bolonio requested a review from khiga8 August 1, 2022 14:18
@bolonio bolonio merged commit 83cb834 into master Aug 1, 2022
@bolonio bolonio deleted the accessibility/issues/1430/migrate-a11y-rubocop-rules-link-href branch August 1, 2022 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants