Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Style/SelectByRegexp aware of ENV const #10457

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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