@@ -24,6 +24,8 @@ module Capybara
2424 # expect(page).to have_current_path(/widgets/)
2525 #
2626 class CurrentPathExpectation < Cop
27+ extend AutoCorrector
28+
2729 MSG = 'Do not set an RSpec expectation on `current_path` in ' \
2830 'Capybara feature specs - instead, use the ' \
2931 '`have_current_path` matcher on `page`'
@@ -47,30 +49,30 @@ class CurrentPathExpectation < Cop
4749
4850 def on_send ( node )
4951 expectation_set_on_current_path ( node ) do
50- add_offense ( node , location : :selector )
52+ add_offense ( node . loc . selector ) do |corrector |
53+ next unless node . chained?
54+
55+ autocorrect ( corrector , node )
56+ end
5157 end
5258 end
5359
54- def autocorrect ( node )
55- lambda do |corrector |
56- return unless node . chained?
60+ private
5761
58- as_is_matcher ( node . parent ) do |to_sym , matcher_node |
59- rewrite_expectation ( corrector , node , to_sym , matcher_node )
60- end
62+ def autocorrect ( corrector , node )
63+ as_is_matcher ( node . parent ) do |to_sym , matcher_node |
64+ rewrite_expectation ( corrector , node , to_sym , matcher_node )
65+ end
6166
62- regexp_str_matcher ( node . parent ) do |to_sym , matcher_node , regexp |
63- rewrite_expectation ( corrector , node , to_sym , matcher_node )
64- convert_regexp_str_to_literal ( corrector , matcher_node , regexp )
65- end
67+ regexp_str_matcher ( node . parent ) do |to_sym , matcher_node , regexp |
68+ rewrite_expectation ( corrector , node , to_sym , matcher_node )
69+ convert_regexp_str_to_literal ( corrector , matcher_node , regexp )
6670 end
6771 end
6872
69- private
70-
7173 def rewrite_expectation ( corrector , node , to_symbol , matcher_node )
7274 current_path_node = node . first_argument
73- corrector . replace ( current_path_node . loc . expression , 'page' )
75+ corrector . replace ( current_path_node , 'page' )
7476 corrector . replace ( node . parent . loc . selector , 'to' )
7577 matcher_method = if to_symbol == :to
7678 'have_current_path'
@@ -84,7 +86,7 @@ def rewrite_expectation(corrector, node, to_symbol, matcher_node)
8486 def convert_regexp_str_to_literal ( corrector , matcher_node , regexp_str )
8587 str_node = matcher_node . first_argument
8688 regexp_expr = Regexp . new ( regexp_str ) . inspect
87- corrector . replace ( str_node . loc . expression , regexp_expr )
89+ corrector . replace ( str_node , regexp_expr )
8890 end
8991
9092 # `have_current_path` with no options will include the querystring
@@ -97,7 +99,7 @@ def add_ignore_query_options(corrector, node)
9799 return if %i[ regexp str ] . include? ( expectation_last_child . type )
98100
99101 corrector . insert_after (
100- expectation_last_child . loc . expression ,
102+ expectation_last_child ,
101103 ', ignore_query: true'
102104 )
103105 end
0 commit comments