Skip to content

Commit

Permalink
Merge pull request #1374 from fatkodima/env_local-handle-negations
Browse files Browse the repository at this point in the history
Change `Rails/EnvLocal` to handle negated conditions
  • Loading branch information
koic authored Oct 7, 2024
2 parents 986484c + 855e0b0 commit 083e24c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/change_env_local_to_handle_negated_conditions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1374](https://github.com/rubocop/rubocop-rails/pull/1374): Change `Rails/EnvLocal` to handle negated conditions. ([@fatkodima][])
29 changes: 26 additions & 3 deletions lib/rubocop/cop/rails/env_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,50 @@ class EnvLocal < Base
extend TargetRailsVersion

MSG = 'Use `Rails.env.local?` instead.'
MSG_NEGATED = 'Use `!Rails.env.local?` instead.'
LOCAL_ENVIRONMENTS = %i[development? test?].to_set.freeze

minimum_target_rails_version 7.1

# @!method rails_env_local_candidate?(node)
def_node_matcher :rails_env_local_candidate?, <<~PATTERN
# @!method rails_env_local_or?(node)
def_node_matcher :rails_env_local_or?, <<~PATTERN
(or
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
)
PATTERN

# @!method rails_env_local_and?(node)
def_node_matcher :rails_env_local_and?, <<~PATTERN
(and
(send
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
:!)
(send
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
:!)
)
PATTERN

def on_or(node)
rails_env_local_candidate?(node) do |*environments|
rails_env_local_or?(node) do |*environments|
next unless environments.to_set == LOCAL_ENVIRONMENTS

add_offense(node) do |corrector|
corrector.replace(node, 'Rails.env.local?')
end
end
end

def on_and(node)
rails_env_local_and?(node) do |*environments|
next unless environments.to_set == LOCAL_ENVIRONMENTS

add_offense(node, message: MSG_NEGATED) do |corrector|
corrector.replace(node, '!Rails.env.local?')
end
end
end
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/rubocop/cop/rails/env_local_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
RUBY
end

it 'registers no offenses for non-local `!Rails.env._? && !Rails.env._?`' do
expect_no_offenses(<<~RUBY)
!Rails.env.development? && Rails.env.production?
!Rails.env.test? && Rails.env.production?
!Rails.env.production? && Rails.env.other?
RUBY
end

it 'registers no offenses for single `Rails.env._?`' do
expect_no_offenses(<<~RUBY)
Rails.env.development?
Expand All @@ -35,6 +43,20 @@
RUBY
end

it 'registers an offense for `!Rails.env.development? && !Rails.env.test?`' do
expect_offense(<<~RUBY)
!Rails.env.development? && !Rails.env.test?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!Rails.env.local?` instead.
!Rails.env.test? && !Rails.env.development?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!Rails.env.local?` instead.
RUBY

expect_correction(<<~RUBY)
!Rails.env.local?
!Rails.env.local?
RUBY
end

it 'registers no offenses for `Rails.env.local?`' do
expect_no_offenses(<<~RUBY)
Rails.env.local?
Expand All @@ -52,6 +74,13 @@
RUBY
end

it 'registers no offenses for `!Rails.env.development? && !Rails.env.test?`' do
expect_no_offenses(<<~RUBY)
!Rails.env.development? && !Rails.env.test?
!Rails.env.test? && !Rails.env.development?
RUBY
end

it 'registers no offenses for `Rails.env.local?`' do
expect_no_offenses(<<~RUBY)
Rails.env.local?
Expand Down

0 comments on commit 083e24c

Please sign in to comment.