-
Notifications
You must be signed in to change notification settings - Fork 161
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
Changes from 1 commit
fb79321
dc28c6e
b6645c2
295968e
3fc0b67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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 == "#" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic is wrong. When using @khiga8 what do you think? Shall we change the logic to check the second argument? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Seems like our code should pass on all three of the tests including this one? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 So maybe something like: if args.length > 1 && args[1].type == :str && args[1].children.first == "#" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
add_offense(node.loc.selector) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
Uh oh!
There was an error while loading. Please reload this page.