Skip to content

Commit

Permalink
Merge pull request #12521 from koic/make_style_select_by_regexp_aware…
Browse files Browse the repository at this point in the history
…_of_safe_navigation_operator

[Fix #12458] Make `Style/SelectByRegexp` cops aware of safe navigation operator
  • Loading branch information
koic committed Dec 9, 2023
2 parents 037c860 + 73bf284 commit 4e9cfcc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12458](https://github.com/rubocop/rubocop/issues/12458): Make `Style/SelectByRegexp` cops aware of safe navigation operator. ([@koic][])
11 changes: 6 additions & 5 deletions lib/rubocop/cop/style/select_by_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ class SelectByRegexp < Base
# @!method regexp_match?(node)
def_node_matcher :regexp_match?, <<~PATTERN
{
(block send (args (arg $_)) ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
(numblock send $1 ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
(block call (args (arg $_)) ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
(numblock call $1 ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
}
PATTERN

# Returns true if a node appears to return a hash
# @!method creates_hash?(node)
def_node_matcher :creates_hash?, <<~PATTERN
{
(send (const _ :Hash) {:new :[]} ...)
(block (send (const _ :Hash) :new ...) ...)
(send _ { :to_h :to_hash } ...)
(call (const _ :Hash) {:new :[]} ...)
(block (call (const _ :Hash) :new ...) ...)
(call _ { :to_h :to_hash } ...)
}
PATTERN

Expand Down Expand Up @@ -100,6 +100,7 @@ def on_send(node)
register_offense(node, block_node, regexp, replacement)
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
alias on_csend on_send

private

Expand Down
43 changes: 43 additions & 0 deletions spec/rubocop/cop/style/select_by_regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,49 @@
end
end
end

context "with safe navigation #{method}", :ruby23 do
it 'registers an offense and corrects for `match?`' do
expect_offense(<<~RUBY, method: method)
array&.#{method} { |x| x.match? /regexp/ }
^^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY

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

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

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

it 'does not register an offense when the receiver is `Hash.new`' do
expect_no_offenses(<<~RUBY)
Hash&.new.#{method} { |x| x.match? /regexp/ }
Hash&.new { |hash, key| :default }.#{method} { |x| x.match? /regexp/ }
RUBY
end

it 'does not register an offense when the receiver is `to_h`' do
expect_no_offenses(<<~RUBY)
foo&.to_h.#{method} { |x| x.match? /regexp/ }
RUBY
end

it 'does not register an offense when the receiver is `to_hash`' do
expect_no_offenses(<<~RUBY)
foo&.to_hash.#{method} { |x| x.match? /regexp/ }
RUBY
end
end
end

shared_examples 'regexp mismatch' do |method, correction|
Expand Down

0 comments on commit 4e9cfcc

Please sign in to comment.