Skip to content
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Edge (Unreleased)

- Add new `Capybara/TextMatcherStyle` cop. ([@nzlaura])
- Fix a false positive for `Capybara/FindAllFirst` when using logical operators with `all('...')[0]`. ([@ydah])

## 2.22.1 (2025-03-12)
Expand Down Expand Up @@ -91,6 +92,7 @@
[@darhazer]: https://github.com/Darhazer
[@earlopain]: https://github.com/earlopain
[@koic]: https://github.com/koic
[@nzlaura]: https://github.com/nzlaura
[@onumis]: https://github.com/onumis
[@oskarsezerins]: https://github.com/OskarsEzerins
[@pirj]: https://github.com/pirj
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ Capybara/SpecificMatcher:
VersionAdded: '2.12'
Reference: https://www.rubydoc.info/gems/rubocop-capybara/RuboCop/Cop/Capybara/SpecificMatcher

Capybara/TextMatcherStyle:
Description: Checks for usage of `have_content` and `have_no_content`.
Enabled: pending
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-capybara/RuboCop/Cop/Capybara/TextMatcherStyle

Capybara/VisibilityMatcher:
Description: Checks for boolean visibility in Capybara finders.
Enabled: true
Expand Down
48 changes: 48 additions & 0 deletions lib/rubocop/cop/capybara/text_matcher_style.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Capybara
# Checks for usage of `have_content` and `have_no_content`.
#
# Capybara provides `have_text` and `have_no_text` matchers that are
# more concise and preferred over their aliases `have_content` and
# `have_no_content`.
#
# @example
# # bad
# expect(page).to have_content('capy')
# expect(page).to have_no_content('bara')
#
# # good
# expect(page).to have_text('capy')
# expect(page).to have_no_text('bara')
#
class TextMatcherStyle < ::RuboCop::Cop::Base
extend AutoCorrector

MSG = 'Prefer `%<good>s` over `%<bad>s`.'
RESTRICT_ON_SEND = %i[have_content have_no_content].freeze
PREFERRED_METHOD = {
'have_content' => 'have_text',
'have_no_content' => 'have_no_text'
}.freeze

def on_send(node)
method_node = node.loc.selector
add_offense(method_node, message: message(method_node)) do |corrector|
corrector.replace(method_node,
PREFERRED_METHOD[method_node.source])
end
end
alias on_csend on_send

private

def message(node)
format(MSG, good: PREFERRED_METHOD[node.source], bad: node.source)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/capybara_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
require_relative 'capybara/specific_actions'
require_relative 'capybara/specific_finders'
require_relative 'capybara/specific_matcher'
require_relative 'capybara/text_matcher_style'
require_relative 'capybara/visibility_matcher'
48 changes: 48 additions & 0 deletions spec/rubocop/cop/capybara/text_matcher_style_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Capybara::TextMatcherStyle do
it 'registers an offense when using `have_content`' do
expect_offense(<<~RUBY)
expect(page).to have_content('text')
^^^^^^^^^^^^ Prefer `have_text` over `have_content`.
RUBY

expect_correction(<<~RUBY)
expect(page).to have_text('text')
RUBY
end

it 'registers an offense when using `have_no_content`' do
expect_offense(<<~RUBY)
expect(page).to have_no_content('capybara')
^^^^^^^^^^^^^^^ Prefer `have_no_text` over `have_no_content`.
RUBY

expect_correction(<<~RUBY)
expect(page).to have_no_text('capybara')
RUBY
end

it 'does not register an offense when using `have_text`' do
expect_no_offenses(<<~RUBY)
expect(page).to have_text('ruby')
RUBY
end

it 'does not register an offense when using `have_no_text`' do
expect_no_offenses(<<~RUBY)
expect(page).to have_no_text('capybara')
RUBY
end

it 'preserves arguments during autocorrection' do
expect_offense(<<~RUBY)
expect(page).to have_content('text', exact: true)
^^^^^^^^^^^^ Prefer `have_text` over `have_content`.
RUBY

expect_correction(<<~RUBY)
expect(page).to have_text('text', exact: true)
RUBY
end
end