Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Bump RuboCop requirement to +1.81. ([@ydah])
- Fix a false positive for `Capybara/FindAllFirst` when using logical operators with `all('...')[0]`. ([@ydah])
- Add support for using `find` with `:link` and `:field` in `Capybara/SpecificFinders` cop. ([@ydah])

## 2.22.1 (2025-03-12)

Expand Down
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/cops_capybara.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,13 @@ find('#some-id')
find('[id=some-id]')
find(:css, '#some-id')
find(:id, 'some-id')
find(:link, 'Home')
find(:field, 'Name')

# good
find_by_id('some-id')
find_link('Home')
find_field('Name')
----

[#references-capybaraspecificfinders]
Expand Down
56 changes: 48 additions & 8 deletions lib/rubocop/cop/capybara/specific_finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,77 @@ module Capybara
# find('[id=some-id]')
# find(:css, '#some-id')
# find(:id, 'some-id')
# find(:link, 'Home')
# find(:field, 'Name')
#
# # good
# find_by_id('some-id')
# find_link('Home')
# find_field('Name')
#
class SpecificFinders < ::RuboCop::Cop::Base
class SpecificFinders < ::RuboCop::Cop::Base # rubocop:disable Metrics/ClassLength
extend AutoCorrector
include RangeHelp

MSG = 'Prefer `find_by_id` over `find`.'
MSG = 'Prefer `%<replacement>s` over `find`.'
RESTRICT_ON_SEND = %i[find].freeze

# @!method find_argument(node)
def_node_matcher :find_argument, <<~PATTERN
(send _ :find $(sym {:css :id})? (str $_) ...)
(send _ :find $(sym {:css :id :link :field})? (str $_) ...)
PATTERN

# @!method find_argument_without_locator(node)
def_node_matcher :find_argument_without_locator, <<~PATTERN
(send _ :find $(sym {:link :field}))
PATTERN

# @!method class_options(node)
def_node_search :class_options, <<~PATTERN
(pair (sym :class) $_ ...)
PATTERN

def on_send(node)
def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
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
on_sym_selector(node, sym) if sym.first && selector?(sym)
end
end

private

def selector?(sym)
%i[link field].include?(sym.first.value)
end

def on_sym_selector(node, sym)
replacement = "find_#{sym.first.value}"
register_selector_offense(node, replacement)
end

def register_selector_offense(node, replacement)
message = format(MSG, replacement: replacement)
add_offense(offense_range(node), message: message) do |corrector|
corrector.replace(node.loc.selector, replacement)
remove_selector_argument(corrector, node)
end
end

def remove_selector_argument(corrector, node)
if node.arguments.size == 1
corrector.remove(node.first_argument)
else
range = range_between(node.first_argument.source_range.begin_pos,
node.arguments[1].source_range.begin_pos)
corrector.remove(range)
end
end

def on_attr(node, sym, arg)
attrs = CssSelector.attributes(arg)
return unless (id = attrs['id'])
Expand All @@ -71,16 +108,19 @@ def attribute?(arg)
end

def register_offense(node, sym, id, classes = [])
add_offense(offense_range(node)) do |corrector|
message = format(MSG, replacement: 'find_by_id')
add_offense(offense_range(node), message: message) do |corrector|
corrector.replace(node.loc.selector, 'find_by_id')
corrector.replace(node.first_argument, id.delete('\\'))
unless classes.compact.empty?
autocorrect_classes(corrector, node, classes)
end
autocorrect_id_classes(corrector, node, classes)
corrector.remove(deletion_range(node)) unless sym.empty?
end
end

def autocorrect_id_classes(corrector, node, classes)
autocorrect_classes(corrector, node, classes) if classes.compact.any?
end

def deletion_range(node)
range_between(node.first_argument.source_range.end_pos,
node.arguments[1].source_range.end_pos)
Expand Down
70 changes: 70 additions & 0 deletions spec/rubocop/cop/capybara/specific_finders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@
RUBY
end

it 'registers an offense when using `find` ' \
'with argument is attribute specified id and style attribute' do
expect_offense(<<~RUBY)
find('[id=some-id][style=display:none]')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`.
RUBY

expect_correction(<<~RUBY)
find_by_id('some-id', style: 'display:none')
RUBY
end

it 'does not register an offense when using `find ' \
'with argument is attribute not specified id' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -308,4 +320,62 @@
find('#foo:is(:enabled,:checked)')
RUBY
end

it 'registers an offense when using `find` with `:link`' do
expect_offense(<<~RUBY)
find(:link, 'Home')
^^^^^^^^^^^^^^^^^^^ Prefer `find_link` over `find`.
RUBY

expect_correction(<<~RUBY)
find_link('Home')
RUBY
end

it 'registers an offense when using `find` with `:field`' do
expect_offense(<<~RUBY)
find(:field, 'Name')
^^^^^^^^^^^^^^^^^^^^ Prefer `find_field` over `find`.
RUBY

expect_correction(<<~RUBY)
find_field('Name')
RUBY
end

it 'registers an offense when using `find` with `:link` ' \
'and option' do
expect_offense(<<~RUBY)
find(:link, 'Home', class: 'navbar-link')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_link` over `find`.
RUBY

expect_correction(<<~RUBY)
find_link('Home', class: 'navbar-link')
RUBY
end

it 'registers an offense when using `find` with `:field` ' \
'and option' do
expect_offense(<<~RUBY)
find(:field, 'Email', visible: false)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_field` over `find`.
RUBY

expect_correction(<<~RUBY)
find_field('Email', visible: false)
RUBY
end

it 'does not register an offense when using `find_link`' do
expect_no_offenses(<<~RUBY)
find_link('Home')
RUBY
end

it 'does not register an offense when using `find_field`' do
expect_no_offenses(<<~RUBY)
find_field('Name')
RUBY
end
end