Skip to content

Commit 3a45198

Browse files
committed
Add Rails/Env cop
1 parent 2e619f3 commit 3a45198

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1152,3 +1152,4 @@
11521152
[@fwolfst]: https://github.com/fwolfst
11531153
[@maxprokopiev]: https://github.com/maxprokopiev
11541154
[@ytjmt]: https://github.com/ytjmt
1155+
[@cdudas17]: https://github.com/cdudas17

Diff for: changelog/new_env_cop.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1375](https://github.com/rubocop/rubocop-rails/pull/1375): Add new `Rails/Env` cop. ([@cdudas17][])

Diff for: config/default.yml

+5
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ Rails/EnumUniqueness:
441441
Include:
442442
- app/models/**/*.rb
443443

444+
Rails/Env:
445+
Description: 'Use Feature Flags instead of `Rails.env`.'
446+
Enabled: pending
447+
VersionAdded: '<<next>>'
448+
444449
Rails/EnvLocal:
445450
Description: 'Use `Rails.env.local?` instead of `Rails.env.development? || Rails.env.test?`.'
446451
Enabled: pending

Diff for: lib/rubocop/cop/rails/env.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Rails
6+
# Checks for usage of `Rails.env` which can be replaced with Feature Flags
7+
#
8+
# @example
9+
#
10+
# # bad
11+
# Rails.env.development? || Rails.env.production? || Rails.env.local?
12+
#
13+
# # good
14+
# if Feature.enabled?(:new_feature)
15+
# # new feature code
16+
# end
17+
#
18+
class Env < Base
19+
MSG = 'Use Feature Flags instead of `Rails.env`.'
20+
# RESTRICT_ON_SEND = %i[env].freeze
21+
22+
def_node_matcher :rails_env_any?, <<~PATTERN
23+
(send (send (const {nil? cbase} :Rails) :env) ...)
24+
PATTERN
25+
26+
def on_send(node)
27+
rails_env_any?(node) do
28+
add_offense(node)
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end

Diff for: lib/rubocop/cop/rails_cops.rb

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
require_relative 'rails/enum_hash'
4949
require_relative 'rails/enum_syntax'
5050
require_relative 'rails/enum_uniqueness'
51+
require_relative 'rails/env'
5152
require_relative 'rails/env_local'
5253
require_relative 'rails/environment_comparison'
5354
require_relative 'rails/environment_variable_access'

Diff for: spec/rubocop/cop/rails/env_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::Rails::Env, :config do
4+
it 'registers an offense for `Rails.env.development? || Rails.env.test?`' do
5+
expect_offense(<<~RUBY)
6+
Rails.env.development? || Rails.env.test?
7+
^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
8+
^^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
9+
RUBY
10+
end
11+
12+
it 'registers an offense for `Rails.env.production?`' do
13+
expect_offense(<<~RUBY)
14+
Rails.env.production?
15+
^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
16+
RUBY
17+
end
18+
19+
it 'does not register an offense for unrelated config' do
20+
expect_no_offenses(<<~RUBY)
21+
Rails.environment
22+
RUBY
23+
end
24+
end

0 commit comments

Comments
 (0)