lib/rubocop/cop/capybara/negation_matcher.rb in rubocop-capybara-2.20.0 vs lib/rubocop/cop/capybara/negation_matcher.rb in rubocop-capybara-2.21.0
- old
+ new
@@ -5,24 +5,24 @@
module Capybara
# Enforces use of `have_no_*` or `not_to` for negated expectations.
#
# @example EnforcedStyle: have_no (default)
# # bad
- # expect(page).not_to have_selector
+ # expect(page).not_to have_selector 'a'
# expect(page).not_to have_css('a')
#
# # good
- # expect(page).to have_no_selector
+ # expect(page).to have_no_selector 'a'
# expect(page).to have_no_css('a')
#
# @example EnforcedStyle: not_to
# # bad
- # expect(page).to have_no_selector
+ # expect(page).to have_no_selector 'a'
# expect(page).to have_no_css('a')
#
# # good
- # expect(page).not_to have_selector
+ # expect(page).not_to have_selector 'a'
# expect(page).not_to have_css('a')
#
class NegationMatcher < ::RuboCop::Cop::Base
extend AutoCorrector
include ConfigurableEnforcedStyle
@@ -40,22 +40,22 @@
.freeze
RESTRICT_ON_SEND = (POSITIVE_MATCHERS + NEGATIVE_MATCHERS).freeze
# @!method not_to?(node)
def_node_matcher :not_to?, <<~PATTERN
- (send ... :not_to
+ (send ... {:not_to :to_not}
(send nil? %POSITIVE_MATCHERS ...))
PATTERN
# @!method have_no?(node)
def_node_matcher :have_no?, <<~PATTERN
(send ... :to
(send nil? %NEGATIVE_MATCHERS ...))
PATTERN
def on_send(node)
- return unless offense?(node.parent)
+ return unless offense?(node)
matcher = node.method_name.to_s
add_offense(offense_range(node),
message: message(matcher)) do |corrector|
corrector.replace(node.parent.loc.selector, replaced_runner)
@@ -65,11 +65,12 @@
end
private
def offense?(node)
- (style == :have_no && not_to?(node)) ||
- (style == :not_to && have_no?(node))
+ node.arguments? &&
+ ((style == :have_no && not_to?(node.parent)) ||
+ (style == :not_to && have_no?(node.parent)))
end
def offense_range(node)
node.parent.loc.selector.with(end_pos: node.loc.selector.end_pos)
end