From 144af682b633f7d632257a22ba961b9a9a16c6bd Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Fri, 24 Mar 2023 02:44:25 +0900 Subject: [PATCH] Fix an offense message for `Capybara/SpecificFinders` This PR is fix an offense message for `Capybara/SpecificFinders`. Modify the message of the offense as follows: ```diff find('#some-id') - ^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. ``` --- CHANGELOG.md | 2 + lib/rubocop/cop/capybara/specific_finders.rb | 2 +- .../cop/capybara/specific_finders_spec.rb | 40 +++++++++---------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0222f19..2542a033 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Edge (Unreleased) +- Fix an offense message for `Capybara/SpecificFinders`. ([@ydah]) + ## 2.17.1 (2023-02-13) - Fix an incorrect autocorrect for `Capybara/CurrentPathExpectation`. ([@ydah]) diff --git a/lib/rubocop/cop/capybara/specific_finders.rb b/lib/rubocop/cop/capybara/specific_finders.rb index e3134a92..95d4ae77 100644 --- a/lib/rubocop/cop/capybara/specific_finders.rb +++ b/lib/rubocop/cop/capybara/specific_finders.rb @@ -19,7 +19,7 @@ class SpecificFinders < ::RuboCop::Cop::Base include RangeHelp - MSG = 'Prefer `find_by` over `find`.' + MSG = 'Prefer `find_by_id` over `find`.' RESTRICT_ON_SEND = %i[find].freeze # @!method find_argument(node) diff --git a/spec/rubocop/cop/capybara/specific_finders_spec.rb b/spec/rubocop/cop/capybara/specific_finders_spec.rb index d59b38bd..d51f297c 100644 --- a/spec/rubocop/cop/capybara/specific_finders_spec.rb +++ b/spec/rubocop/cop/capybara/specific_finders_spec.rb @@ -4,7 +4,7 @@ it 'registers an offense when using `find`' do expect_offense(<<~RUBY) find('#some-id') - ^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -15,7 +15,7 @@ it 'registers an offense when using `find` with no parentheses' do expect_offense(<<~RUBY) find "#some-id" - ^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -26,7 +26,7 @@ it 'registers an offense when using `find` with id and class' do expect_offense(<<~RUBY) find('#some-id.some-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -37,15 +37,15 @@ it 'registers an offense when using `find` with id include `\.`' do expect_offense(<<~RUBY) find('#some-id\\.some-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. find('#some-id\\>some-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. find('#some-id\\,some-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. find('#some-id\\+some-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. find('#some-id\\~some-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -60,7 +60,7 @@ it 'registers an offense when using `find` with multiple classes' do expect_offense(<<~RUBY) find('#some-id.some-cls.other-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -72,7 +72,7 @@ "and class: 'other-cls'" do expect_offense(<<~RUBY) find('#some-id.some-cls', class: 'other-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -84,7 +84,7 @@ "and class: ['other-cls1', 'other-cls2']" do expect_offense(<<~RUBY) find('#some-id.some-cls', class: ['other-cls1', 'other-cls2']) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -96,7 +96,7 @@ "and exact_text: 'foo', class: 'other-cls'" do expect_offense(<<~RUBY) find('#some-id.some-cls', exact_text: 'foo', class: 'other-cls') - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -107,7 +107,7 @@ it 'registers an offense when using `find` and other args' do expect_offense(<<~RUBY) find('#some-id', exact_text: 'foo') - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -119,7 +119,7 @@ 'with no parentheses' do expect_offense(<<~RUBY) find '#some-id', exact_text: 'foo' - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -130,9 +130,9 @@ it 'registers an offense when using `find` with method chain' do expect_offense(<<~RUBY) find('#some-id').find('#other-id').find('#another-id') - ^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. - ^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. - ^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. + ^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. + ^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -144,7 +144,7 @@ 'with argument is attribute specified id' do expect_offense(<<~RUBY) find('[id=some-id]') - ^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY) @@ -156,9 +156,9 @@ 'with argument is attribute specified id surrounded by quotation' do expect_offense(<<~RUBY) find('[id="foo"]') - ^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. find('[id="foo[bar]"]') - ^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`. + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by_id` over `find`. RUBY expect_correction(<<~RUBY)