Skip to content

Commit b1ff70b

Browse files
authored
Merge pull request #906 from sambostock/rails-env-local
Add `Rails/EnvLocal` cop to enforce upcoming `Rails.env.local?`
2 parents c111a29 + e0cb140 commit b1ff70b

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#906](https://github.com/rubocop/rubocop-rails/pull/906): Add `Rails/EnvLocal` cop. ([@sambostock][])

config/default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ Rails/EnumUniqueness:
430430
Include:
431431
- app/models/**/*.rb
432432

433+
Rails/EnvLocal:
434+
Description: 'Use `Rails.env.local?` instead of `Rails.env.development? || Rails.env.test?`.'
435+
Enabled: pending
436+
VersionAdded: '<<next>>'
437+
433438
Rails/EnvironmentComparison:
434439
Description: "Favor `Rails.env.production?` over `Rails.env == 'production'`."
435440
Enabled: true

lib/rubocop/cop/rails/env_local.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Rails
6+
# Checks for usage of `Rails.env.development? || Rails.env.test?` which
7+
# can be replaced with `Rails.env.local?`, introduced in Rails 7.1.
8+
#
9+
# @example
10+
#
11+
# # bad
12+
# Rails.env.development? || Rails.env.test?
13+
#
14+
# # good
15+
# Rails.env.local?
16+
#
17+
class EnvLocal < Base
18+
extend AutoCorrector
19+
extend TargetRailsVersion
20+
21+
MSG = 'Use `Rails.env.local?` instead.'
22+
LOCAL_ENVIRONMENTS = %i[development? test?].to_set.freeze
23+
24+
minimum_target_rails_version 7.1
25+
26+
# @!method rails_env_local_candidate?(node)
27+
def_node_matcher :rails_env_local_candidate?, <<~PATTERN
28+
(or
29+
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
30+
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
31+
)
32+
PATTERN
33+
34+
def on_or(node)
35+
rails_env_local_candidate?(node) do |*environments|
36+
next unless environments.to_set == LOCAL_ENVIRONMENTS
37+
38+
add_offense(node) do |corrector|
39+
corrector.replace(node, 'Rails.env.local?')
40+
end
41+
end
42+
end
43+
end
44+
end
45+
end
46+
end

lib/rubocop/cop/rails_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
require_relative 'rails/eager_evaluation_log_message'
4747
require_relative 'rails/enum_hash'
4848
require_relative 'rails/enum_uniqueness'
49+
require_relative 'rails/env_local'
4950
require_relative 'rails/environment_comparison'
5051
require_relative 'rails/environment_variable_access'
5152
require_relative 'rails/exit'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::Rails::EnvLocal, :config do
4+
shared_examples 'non-local candidates' do
5+
it 'registers no offenses for non-local `Rails.env._? || Rails.env._?`' do
6+
expect_no_offenses(<<~RUBY)
7+
Rails.env.development? || Rails.env.production?
8+
Rails.env.test? || Rails.env.production?
9+
Rails.env.production? || Rails.env.other?
10+
RUBY
11+
end
12+
13+
it 'registers no offenses for single `Rails.env._?`' do
14+
expect_no_offenses(<<~RUBY)
15+
Rails.env.development?
16+
Rails.env.test?
17+
Rails.env.production?
18+
Rails.env.other?
19+
RUBY
20+
end
21+
end
22+
23+
context 'In Rails >= 7.1', :rails71 do
24+
it 'registers an offense for `Rails.env.development? || Rails.env.test?`' do
25+
expect_offense(<<~RUBY)
26+
Rails.env.development? || Rails.env.test?
27+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.env.local?` instead.
28+
Rails.env.test? || Rails.env.development?
29+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.env.local?` instead.
30+
RUBY
31+
32+
expect_correction(<<~RUBY)
33+
Rails.env.local?
34+
Rails.env.local?
35+
RUBY
36+
end
37+
38+
it 'registers no offenses for `Rails.env.local?`' do
39+
expect_no_offenses(<<~RUBY)
40+
Rails.env.local?
41+
RUBY
42+
end
43+
44+
include_examples 'non-local candidates'
45+
end
46+
47+
context 'In Rails < 7.1', :rails70 do
48+
it 'registers no offenses for `Rails.env.development? || Rails.env.test?`' do
49+
expect_no_offenses(<<~RUBY)
50+
Rails.env.development? || Rails.env.test?
51+
Rails.env.test? || Rails.env.development?
52+
RUBY
53+
end
54+
55+
it 'registers no offenses for `Rails.env.local?`' do
56+
expect_no_offenses(<<~RUBY)
57+
Rails.env.local?
58+
RUBY
59+
end
60+
61+
include_examples 'non-local candidates'
62+
end
63+
end

0 commit comments

Comments
 (0)