Class: Rubocop::Cop::RSpec::HaveLinkParameters

Inherits:
Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Defined in:
lib/rubocop/cop/rspec/have_link_parameters.rb

Overview

Checks for unused parameters to the ‘have_link` matcher.

Examples:


# bad
expect(page).to have_link('Link', 'https://example.com')

# good
expect(page).to have_link('Link', href: 'https://example.com')
expect(page).to have_link('Example')

Constant Summary collapse

MESSAGE =
"The second argument to `have_link` should be a Hash."

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rspec/have_link_parameters.rb', line 31

def on_send(node)
  return unless unused_parameters?(node)

  location = node.arguments[1..]
               .map(&:source_range)
               .reduce(:join)

  add_offense(location, message: MESSAGE) do |corrector|
    corrector.insert_after(location.end, "\n")
  end
end

#unused_parameters?(node) ⇒ Object



25
26
27
28
29
# File 'lib/rubocop/cop/rspec/have_link_parameters.rb', line 25

def_node_matcher :unused_parameters?, <<~PATTERN
  (send nil? :have_link
    _ !{hash nil}
  )
PATTERN