Skip to content

Commit

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

Make `Style/SelectByRegexp` aware of `ENV` const
  • Loading branch information
koic committed Mar 21, 2022
2 parents b901a9f + 170d3ea commit 3112ce7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
@@ -0,0 +1 @@
* [#10457](https://github.com/rubocop/rubocop/pull/10457): Make `Style/SelectByRegexp` aware of `ENV` const. ([@koic][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/style/select_by_regexp.rb
Expand Up @@ -69,6 +69,11 @@ class SelectByRegexp < Base
}
PATTERN

# @!method env_const?(node)
def_node_matcher :env_const?, <<~PATTERN
(const {nil? cbase} :ENV)
PATTERN

# @!method calls_lvar?(node, name)
def_node_matcher :calls_lvar?, <<~PATTERN
{
Expand All @@ -94,7 +99,7 @@ def on_send(node)
def receiver_allowed?(node)
return false unless node

node.hash_type? || creates_hash?(node)
node.hash_type? || creates_hash?(node) || env_const?(node)
end

def register_offense(node, block_node, regexp)
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/style/select_by_regexp_spec.rb
Expand Up @@ -249,6 +249,13 @@
RUBY
end

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

context 'with `numblock`s', :ruby27 do
it 'registers an offense and corrects for `match?`' do
expect_offense(<<~RUBY, method: method)
Expand Down

0 comments on commit 3112ce7

Please sign in to comment.