lib/rubocop/cop/rspec/capybara/current_path_expectation.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/capybara/current_path_expectation.rb in rubocop-rspec-1.42.0

- old
+ new

@@ -22,10 +22,12 @@ # # good # expect(page).to have_current_path("/callback") # expect(page).to have_current_path(/widgets/) # class CurrentPathExpectation < Cop + extend AutoCorrector + MSG = 'Do not set an RSpec expectation on `current_path` in ' \ 'Capybara feature specs - instead, use the ' \ '`have_current_path` matcher on `page`' def_node_matcher :expectation_set_on_current_path, <<-PATTERN @@ -45,34 +47,34 @@ $(send nil? :match (str $_))) PATTERN def on_send(node) expectation_set_on_current_path(node) do - add_offense(node, location: :selector) + add_offense(node.loc.selector) do |corrector| + next unless node.chained? + + autocorrect(corrector, node) + end end end - def autocorrect(node) - lambda do |corrector| - return unless node.chained? + private - as_is_matcher(node.parent) do |to_sym, matcher_node| - rewrite_expectation(corrector, node, to_sym, matcher_node) - end + def autocorrect(corrector, node) + as_is_matcher(node.parent) do |to_sym, matcher_node| + rewrite_expectation(corrector, node, to_sym, matcher_node) + end - regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp| - rewrite_expectation(corrector, node, to_sym, matcher_node) - convert_regexp_str_to_literal(corrector, matcher_node, regexp) - end + regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp| + rewrite_expectation(corrector, node, to_sym, matcher_node) + convert_regexp_str_to_literal(corrector, matcher_node, regexp) end end - private - def rewrite_expectation(corrector, node, to_symbol, matcher_node) current_path_node = node.first_argument - corrector.replace(current_path_node.loc.expression, 'page') + corrector.replace(current_path_node, 'page') corrector.replace(node.parent.loc.selector, 'to') matcher_method = if to_symbol == :to 'have_current_path' else 'have_no_current_path' @@ -82,11 +84,11 @@ end def convert_regexp_str_to_literal(corrector, matcher_node, regexp_str) str_node = matcher_node.first_argument regexp_expr = Regexp.new(regexp_str).inspect - corrector.replace(str_node.loc.expression, regexp_expr) + corrector.replace(str_node, regexp_expr) end # `have_current_path` with no options will include the querystring # while `page.current_path` does not. # This ensures the option `ignore_query: true` is added @@ -95,10 +97,10 @@ expectation_node = node.parent.last_argument expectation_last_child = expectation_node.children.last return if %i[regexp str].include?(expectation_last_child.type) corrector.insert_after( - expectation_last_child.loc.expression, + expectation_last_child, ', ignore_query: true' ) end end end