Skip to content

Commit

Permalink
Change Rails/EnvLocal to handle negated conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Oct 5, 2024
1 parent 986484c commit 8ef1aa1
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 @@
* [#1373](https://github.com/rubocop/rubocop-rails/pull/1373): 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 8ef1aa1

Please sign in to comment.