Skip to content

Commit 083e24c

Browse files
authored
Merge pull request #1374 from fatkodima/env_local-handle-negations
Change `Rails/EnvLocal` to handle negated conditions
2 parents 986484c + 855e0b0 commit 083e24c

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1374](https://github.com/rubocop/rubocop-rails/pull/1374): Change `Rails/EnvLocal` to handle negated conditions. ([@fatkodima][])

lib/rubocop/cop/rails/env_local.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,50 @@ class EnvLocal < Base
1919
extend TargetRailsVersion
2020

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

2425
minimum_target_rails_version 7.1
2526

26-
# @!method rails_env_local_candidate?(node)
27-
def_node_matcher :rails_env_local_candidate?, <<~PATTERN
27+
# @!method rails_env_local_or?(node)
28+
def_node_matcher :rails_env_local_or?, <<~PATTERN
2829
(or
2930
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
3031
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
3132
)
3233
PATTERN
3334

35+
# @!method rails_env_local_and?(node)
36+
def_node_matcher :rails_env_local_and?, <<~PATTERN
37+
(and
38+
(send
39+
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
40+
:!)
41+
(send
42+
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
43+
:!)
44+
)
45+
PATTERN
46+
3447
def on_or(node)
35-
rails_env_local_candidate?(node) do |*environments|
48+
rails_env_local_or?(node) do |*environments|
3649
next unless environments.to_set == LOCAL_ENVIRONMENTS
3750

3851
add_offense(node) do |corrector|
3952
corrector.replace(node, 'Rails.env.local?')
4053
end
4154
end
4255
end
56+
57+
def on_and(node)
58+
rails_env_local_and?(node) do |*environments|
59+
next unless environments.to_set == LOCAL_ENVIRONMENTS
60+
61+
add_offense(node, message: MSG_NEGATED) do |corrector|
62+
corrector.replace(node, '!Rails.env.local?')
63+
end
64+
end
65+
end
4366
end
4467
end
4568
end

spec/rubocop/cop/rails/env_local_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
RUBY
1111
end
1212

13+
it 'registers no offenses for non-local `!Rails.env._? && !Rails.env._?`' do
14+
expect_no_offenses(<<~RUBY)
15+
!Rails.env.development? && Rails.env.production?
16+
!Rails.env.test? && Rails.env.production?
17+
!Rails.env.production? && Rails.env.other?
18+
RUBY
19+
end
20+
1321
it 'registers no offenses for single `Rails.env._?`' do
1422
expect_no_offenses(<<~RUBY)
1523
Rails.env.development?
@@ -35,6 +43,20 @@
3543
RUBY
3644
end
3745

46+
it 'registers an offense for `!Rails.env.development? && !Rails.env.test?`' do
47+
expect_offense(<<~RUBY)
48+
!Rails.env.development? && !Rails.env.test?
49+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!Rails.env.local?` instead.
50+
!Rails.env.test? && !Rails.env.development?
51+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!Rails.env.local?` instead.
52+
RUBY
53+
54+
expect_correction(<<~RUBY)
55+
!Rails.env.local?
56+
!Rails.env.local?
57+
RUBY
58+
end
59+
3860
it 'registers no offenses for `Rails.env.local?`' do
3961
expect_no_offenses(<<~RUBY)
4062
Rails.env.local?
@@ -52,6 +74,13 @@
5274
RUBY
5375
end
5476

77+
it 'registers no offenses for `!Rails.env.development? && !Rails.env.test?`' do
78+
expect_no_offenses(<<~RUBY)
79+
!Rails.env.development? && !Rails.env.test?
80+
!Rails.env.test? && !Rails.env.development?
81+
RUBY
82+
end
83+
5584
it 'registers no offenses for `Rails.env.local?`' do
5685
expect_no_offenses(<<~RUBY)
5786
Rails.env.local?

0 commit comments

Comments
 (0)