-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1376](https://github.com/rubocop/rubocop-rails/issues/1376): Add new `Rails/Env` cop. ([@cdudas17][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 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.production? || Rails.env.local? | ||
# | ||
# # good | ||
# if FeatureFlag.enabled?(:new_feature) | ||
# # new feature code | ||
# end | ||
# | ||
class Env < Base | ||
MSG = 'Use Feature Flags or config instead of `Rails.env`.' | ||
RESTRICT_ON_SEND = %i[env].freeze | ||
|
||
def on_send(node) | ||
return unless node.receiver&.const_name == 'Rails' | ||
return unless node.parent&.method_name && node.parent&.predicate_method? | ||
|
||
add_offense(node.parent) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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 or config instead of `Rails.env`. | ||
^^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags or config instead of `Rails.env`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for `Rails.env.production?`' do | ||
expect_offense(<<~RUBY) | ||
Rails.env.production? | ||
^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags or config instead of `Rails.env`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `Rails.env`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `Rails.env.capitalize`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.capitalize | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for unrelated config' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.environment | ||
RUBY | ||
end | ||
end |