Skip to content

Commit 1473601

Browse files
committed
Make Rails/EnvironmentComparison cop detect comparisons in case statements
Closes rubocop#1395
1 parent 88b9dc5 commit 1473601

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

lib/rubocop/cop/rails/environment_comparison.rb

+17-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ class EnvironmentComparison < Base
2222

2323
SYM_MSG = 'Do not compare `Rails.env` with a symbol, it will always evaluate to `false`.'
2424

25+
CASE_MSG = 'Favor environment check predicate methods over case comparison.'
26+
2527
RESTRICT_ON_SEND = %i[== !=].freeze
2628

2729
def_node_matcher :comparing_str_env_with_rails_env_on_lhs?, <<~PATTERN
2830
(send
29-
(send (const {nil? cbase} :Rails) :env)
31+
#rails_env_const?
3032
{:== :!=}
3133
$str
3234
)
@@ -36,13 +38,13 @@ class EnvironmentComparison < Base
3638
(send
3739
$str
3840
{:== :!=}
39-
(send (const {nil? cbase} :Rails) :env)
41+
#rails_env_const?
4042
)
4143
PATTERN
4244

4345
def_node_matcher :comparing_sym_env_with_rails_env_on_lhs?, <<~PATTERN
4446
(send
45-
(send (const {nil? cbase} :Rails) :env)
47+
#rails_env_const?
4648
{:== :!=}
4749
$sym
4850
)
@@ -52,14 +54,18 @@ class EnvironmentComparison < Base
5254
(send
5355
$sym
5456
{:== :!=}
55-
(send (const {nil? cbase} :Rails) :env)
57+
#rails_env_const?
5658
)
5759
PATTERN
5860

5961
def_node_matcher :content, <<~PATTERN
6062
({str sym} $_)
6163
PATTERN
6264

65+
def_node_matcher :rails_env_const?, <<~PATTERN
66+
(send (const {nil? cbase} :Rails) :env)
67+
PATTERN
68+
6369
def on_send(node)
6470
if (env_node = comparing_str_env_with_rails_env_on_lhs?(node) ||
6571
comparing_str_env_with_rails_env_on_rhs?(node))
@@ -79,6 +85,13 @@ def on_send(node)
7985
end
8086
end
8187

88+
def on_case(case_node)
89+
condition = case_node.condition
90+
return unless rails_env_const?(condition)
91+
92+
add_offense(condition, message: CASE_MSG)
93+
end
94+
8295
private
8396

8497
def autocorrect(corrector, node)

spec/rubocop/cop/rails/environment_comparison_spec.rb

+41
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,51 @@
101101
end
102102
end
103103

104+
context 'when comparing Rails.env using a case statement' do
105+
it 'registers an offense' do
106+
expect_offense(<<~RUBY)
107+
case Rails.env
108+
^^^^^^^^^ Favor environment check predicate methods over case comparison.
109+
when "production"
110+
do_production_thing
111+
when "staging"
112+
do_staging_thing
113+
else
114+
do_other_thing
115+
end
116+
RUBY
117+
end
118+
119+
context 'with pattern matching' do
120+
it 'register an offense' do
121+
expect_offense(<<~RUBY)
122+
case Rails.env
123+
^^^^^^^^^ Favor environment check predicate methods over case comparison.
124+
when "test" | "development"
125+
do_test_thing
126+
else
127+
do_other_thing
128+
end
129+
RUBY
130+
end
131+
end
132+
end
133+
104134
it 'does not register an offense when using `#good_method`' do
105135
expect_no_offenses(<<~RUBY)
106136
Rails.env.production?
107137
Rails.env.test?
108138
RUBY
109139
end
140+
141+
it 'does not register an offense for other case statements' do
142+
expect_no_offenses(<<~RUBY)
143+
case some_method
144+
when "test" | "development"
145+
do_test_thing
146+
else
147+
do_other_thing
148+
end
149+
RUBY
150+
end
110151
end

0 commit comments

Comments
 (0)