Class: RuboCop::Cop::Capybara::SpecificFinders

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

Overview

Checks if there is a more specific finder offered by Capybara.

Examples:

# bad
find('#some-id')
find('[id=some-id]')
find(:css, '#some-id')
find(:id, 'some-id')

# good
find_by_id('some-id')

Constant Summary collapse

MSG =
'Prefer `find_by_id` over `find`.'
RESTRICT_ON_SEND =
%i[find].freeze

Instance Method Summary collapse

Instance Method Details

#class_options(node) ⇒ Object



31
32
33
# File 'lib/rubocop/cop/capybara/specific_finders.rb', line 31

def_node_search :class_options, <<~PATTERN
  (pair (sym :class) $_ ...)
PATTERN

#find_argument(node) ⇒ Object



26
27
28
# File 'lib/rubocop/cop/capybara/specific_finders.rb', line 26

def_node_matcher :find_argument, <<~PATTERN
  (send _ :find $(sym {:css :id})? (str $_) ...)
PATTERN

#on_send(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/capybara/specific_finders.rb', line 35

def on_send(node)
  find_argument(node) do |sym, arg|
    next if CssSelector.pseudo_classes(arg).any?
    next if CssSelector.multiple_selectors?(arg)

    on_attr(node, sym, arg) if attribute?(arg)
    on_id(node, sym, arg) if CssSelector.id?(arg)
    on_sym_id(node, sym, arg) if sym.first&.value == :id
  end
end