From ce9113f1596865c52de1d8d148b09be079ad61b6 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 14 Sep 2022 14:04:23 +0900 Subject: [PATCH] Fix an error for `RSpec/Capybara/SpecificFinders` with no parentheses Fix: https://github.com/rubocop/rubocop-rspec/issues/1386 --- CHANGELOG.md | 2 ++ .../cop/rspec/capybara/specific_finders.rb | 11 +++++++-- .../rspec/capybara/specific_finders_spec.rb | 23 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae49849bc..a99fad8e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/lib/rubocop/cop/rspec/capybara/specific_finders.rb b/lib/rubocop/cop/rspec/capybara/specific_finders.rb index 398915f08..54acb006c 100644 --- a/lib/rubocop/cop/rspec/capybara/specific_finders.rb +++ b/lib/rubocop/cop/rspec/capybara/specific_finders.rb @@ -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 diff --git a/spec/rubocop/cop/rspec/capybara/specific_finders_spec.rb b/spec/rubocop/cop/rspec/capybara/specific_finders_spec.rb index 9d7998304..5996f06e9 100644 --- a/spec/rubocop/cop/rspec/capybara/specific_finders_spec.rb +++ b/spec/rubocop/cop/rspec/capybara/specific_finders_spec.rb @@ -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') @@ -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')