Class: RuboCop::Cop::Capybara::RedundantWithinFind

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/capybara/redundant_within_find.rb

Overview

Checks for redundant ‘within find(…)` calls.

Examples:

# bad
within find('foo.bar') do
  # ...
end

# good
within 'foo.bar' do
  # ...
end

# bad
within find_by_id('foo') do
  # ...
end

# good
within '#foo' do
  # ...
end

Constant Summary collapse

MSG =
'Redundant `within %<method>s(...)` call detected.'
RESTRICT_ON_SEND =
%i[within].freeze
FIND_METHODS =
Set.new(%i[find find_by_id]).freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rubocop/cop/capybara/redundant_within_find.rb', line 41

def on_send(node)
  within_find(node) do |find_node|
    add_offense(find_node, message: msg(find_node)) do |corrector|
      corrector.replace(find_node, replaced(find_node))
    end
  end
end

#within_find(node) ⇒ Object



36
37
38
39
# File 'lib/rubocop/cop/capybara/redundant_within_find.rb', line 36

def_node_matcher :within_find, <<~PATTERN
  (send nil? :within
    $(send nil? %FIND_METHODS ...))
PATTERN