Skip to content

Commit

Permalink
Add Rails/Env cop
Browse files Browse the repository at this point in the history
  • Loading branch information
cdudas17 committed Oct 10, 2024
1 parent 2e619f3 commit 3a45198
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1152,3 +1152,4 @@
[@fwolfst]: https://github.com/fwolfst
[@maxprokopiev]: https://github.com/maxprokopiev
[@ytjmt]: https://github.com/ytjmt
[@cdudas17]: https://github.com/cdudas17
1 change: 1 addition & 0 deletions changelog/new_env_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1375](https://github.com/rubocop/rubocop-rails/pull/1375): Add new `Rails/Env` cop. ([@cdudas17][])
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ Rails/EnumUniqueness:
Include:
- app/models/**/*.rb

Rails/Env:
Description: 'Use Feature Flags instead of `Rails.env`.'
Enabled: pending
VersionAdded: '<<next>>'

Rails/EnvLocal:
Description: 'Use `Rails.env.local?` instead of `Rails.env.development? || Rails.env.test?`.'
Enabled: pending
Expand Down
34 changes: 34 additions & 0 deletions lib/rubocop/cop/rails/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Checks for usage of `Rails.env` which can be replaced with Feature Flags
#
# @example
#
# # bad
# Rails.env.development? || Rails.env.production? || Rails.env.local?
#
# # good
# if Feature.enabled?(:new_feature)
# # new feature code
# end
#
class Env < Base
MSG = 'Use Feature Flags instead of `Rails.env`.'
# RESTRICT_ON_SEND = %i[env].freeze

def_node_matcher :rails_env_any?, <<~PATTERN
(send (send (const {nil? cbase} :Rails) :env) ...)
PATTERN

def on_send(node)
rails_env_any?(node) do
add_offense(node)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
require_relative 'rails/enum_hash'
require_relative 'rails/enum_syntax'
require_relative 'rails/enum_uniqueness'
require_relative 'rails/env'
require_relative 'rails/env_local'
require_relative 'rails/environment_comparison'
require_relative 'rails/environment_variable_access'
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/rails/env_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::Env, :config do
it 'registers an offense for `Rails.env.development? || Rails.env.test?`' do
expect_offense(<<~RUBY)
Rails.env.development? || Rails.env.test?
^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
^^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
RUBY
end

it 'registers an offense for `Rails.env.production?`' do
expect_offense(<<~RUBY)
Rails.env.production?
^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
RUBY
end

it 'does not register an offense for unrelated config' do
expect_no_offenses(<<~RUBY)
Rails.environment
RUBY
end
end

0 comments on commit 3a45198

Please sign in to comment.