Skip to content

Commit

Permalink
Merge pull request #1387 from ydah/fix/1386
Browse files Browse the repository at this point in the history
Fix an error for `RSpec/Capybara/SpecificFinders` with no parentheses
  • Loading branch information
Darhazer authored Sep 14, 2022
2 parents 5627784 + ce9113f commit c14de33
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

* Fix an error for `RSpec/Capybara/SpecificFinders` with no parentheses. ([@ydah][])

## 2.13.1 (2022-09-12)

* Include config/obsoletion.yml in the gemspec. ([@hosamaly][])
Expand Down
11 changes: 9 additions & 2 deletions lib/rubocop/cop/rspec/capybara/specific_finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,15 @@ def to_options(attrs)
end

def offense_range(node)
range_between(node.loc.selector.begin_pos,
node.loc.end.end_pos)
range_between(node.loc.selector.begin_pos, end_pos(node))
end

def end_pos(node)
if node.loc.end
node.loc.end.end_pos
else
node.loc.expression.end_pos
end
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cop/rspec/capybara/specific_finders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
RUBY
end

it 'registers an offense when using `find` with no parentheses' do
expect_offense(<<~RUBY)
find "#some-id"
^^^^^^^^^^^^^^^ Prefer `find_by` over `find`.
RUBY

expect_correction(<<~RUBY)
find_by_id 'some-id'
RUBY
end

it 'registers an offense when using `find` and other args' do
expect_offense(<<~RUBY)
find('#some-id', exact_text: 'foo')
Expand All @@ -23,6 +34,18 @@
RUBY
end

it 'registers an offense when using `find` and other args ' \
'with no parentheses' do
expect_offense(<<~RUBY)
find '#some-id', exact_text: 'foo'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`.
RUBY

expect_correction(<<~RUBY)
find_by_id 'some-id', exact_text: 'foo'
RUBY
end

it 'registers an offense when using `find` with method chain' do
expect_offense(<<~RUBY)
find('#some-id').find('#other-id').find('#another-id')
Expand Down

0 comments on commit c14de33

Please sign in to comment.