Skip to content

Commit

Permalink
Update Style/SelectByRegexp to handle numblocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis authored and bbatsov committed Sep 28, 2021
1 parent c2f3b22 commit a3f9b58
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/rubocop/cop/style/select_by_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class SelectByRegexp < Base

# @!method regexp_match?(node)
def_node_matcher :regexp_match?, <<~PATTERN
(block send (args (arg $_)) ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
{
(block send (args (arg $_)) ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
(numblock send $1 ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
}
PATTERN

# @!method calls_lvar?(node, name)
Expand Down Expand Up @@ -81,6 +84,8 @@ def register_offense(node, block_node, regexp)

def extract_send_node(block_node)
return unless (block_arg_name, regexp_method_send_node = regexp_match?(block_node))

block_arg_name = :"_#{block_arg_name}" if block_node.numblock_type?
return unless calls_lvar?(regexp_method_send_node, block_arg_name)

regexp_method_send_node
Expand Down
2 changes: 1 addition & 1 deletion rubocop.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0')
s.add_runtime_dependency('rexml')
s.add_runtime_dependency('rubocop-ast', '>= 1.9.1', '< 2.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.12.0', '< 2.0')
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 3.0')

Expand Down
58 changes: 58 additions & 0 deletions spec/rubocop/cop/style/select_by_regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,64 @@
array.#{method} { |x| /regexp/.match?(foo(x)) }
RUBY
end

context 'with `numblock`s', :ruby27 do
it 'registers an offense and corrects for `match?`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { _1.match? /regexp/ }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY

expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end

it 'registers an offense and corrects for `Regexp#match?`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { /regexp/.match?(_1) }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY

expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end

it 'registers an offense and corrects for `blockvar =~ regexp`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { _1 =~ /regexp/ }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^ #{message}
RUBY

expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end

it 'registers an offense and corrects for `regexp =~ blockvar`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { /regexp/ =~ _1 }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^ #{message}
RUBY

expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end

it 'does not register an offense if there is more than one numbered param' do
expect_no_offenses(<<~RUBY)
array.#{method} { _1 =~ _2 }
RUBY
end

it 'does not register an offense when the param is a method argument' do
expect_no_offenses(<<~RUBY)
array.#{method} { /regexp/.match?(foo(_1)) }
RUBY
end
end
end
end
end

0 comments on commit a3f9b58

Please sign in to comment.