Skip to content

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

Merged
bolonio merged 5 commits into
masterfrom
accessibility/issues/1430/migrate-a11y-rubocop-rules-link-href
Aug 1, 2022
Merged

Migrate accessibility rubocop rule LinkHref from dotcom to erblint-github#108
bolonio merged 5 commits into
masterfrom
accessibility/issues/1430/migrate-a11y-rubocop-rules-link-href

Conversation

@bolonio

Copy link
Copy Markdown

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

receiver, method_name, *args = *node

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

Copy link
Copy Markdown
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
Copy Markdown
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?

@khiga8khiga8Jul 28, 2022

Copy link
Copy Markdown

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:

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

Copy link
Copy Markdown

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 

Comment threadlib/rubocop/cop/github/rails_link_href.rb Outdated
@bolonio
bolonio requested a review from khiga8August 1, 2022 09:18
@@ -0,0 +1,35 @@
# frozen_string_literal: true

Copy link
Copy Markdown

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
Copy Markdown
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 khiga8August 1, 2022 14:18
@bolonio
bolonio merged commit 83cb834 into masterAug 1, 2022
@bolonio
bolonio deleted the accessibility/issues/1430/migrate-a11y-rubocop-rules-link-href branch August 1, 2022 14:25
Sign up for freeto 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

@bolonio@owenniblock@khiga8