-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforces the use of `Rails.env.local?` instead of `Rails.env.development? || Rails.env.test?`, as of Rails 7.1.
- Loading branch information
1 parent
c111a29
commit e0cb140
Showing
5 changed files
with
116 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 @@ | ||
* [#906](https://github.com/rubocop/rubocop-rails/pull/906): Add `Rails/EnvLocal` cop. ([@sambostock][]) |
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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Checks for usage of `Rails.env.development? || Rails.env.test?` which | ||
# can be replaced with `Rails.env.local?`, introduced in Rails 7.1. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# Rails.env.development? || Rails.env.test? | ||
# | ||
# # good | ||
# Rails.env.local? | ||
# | ||
class EnvLocal < Base | ||
extend AutoCorrector | ||
extend TargetRailsVersion | ||
|
||
MSG = 'Use `Rails.env.local?` instead.' | ||
LOCAL_ENVIRONMENTS = %i[development? test?].to_set.freeze | ||
|
||
minimum_target_rails_version 7.1 | ||
|
||
# @!method rails_env_local_candidate?(node) | ||
def_node_matcher :rails_env_local_candidate?, <<~PATTERN | ||
(or | ||
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS) | ||
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS) | ||
) | ||
PATTERN | ||
|
||
def on_or(node) | ||
rails_env_local_candidate?(node) do |*environments| | ||
next unless environments.to_set == LOCAL_ENVIRONMENTS | ||
|
||
add_offense(node) do |corrector| | ||
corrector.replace(node, 'Rails.env.local?') | ||
end | ||
end | ||
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::EnvLocal, :config do | ||
shared_examples 'non-local candidates' do | ||
it 'registers no offenses for non-local `Rails.env._? || Rails.env._?`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.development? || Rails.env.production? | ||
Rails.env.test? || Rails.env.production? | ||
Rails.env.production? || Rails.env.other? | ||
RUBY | ||
end | ||
|
||
it 'registers no offenses for single `Rails.env._?`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.development? | ||
Rails.env.test? | ||
Rails.env.production? | ||
Rails.env.other? | ||
RUBY | ||
end | ||
end | ||
|
||
context 'In Rails >= 7.1', :rails71 do | ||
it 'registers an offense for `Rails.env.development? || Rails.env.test?`' do | ||
expect_offense(<<~RUBY) | ||
Rails.env.development? || Rails.env.test? | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.env.local?` instead. | ||
Rails.env.test? || Rails.env.development? | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.env.local?` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.env.local? | ||
Rails.env.local? | ||
RUBY | ||
end | ||
|
||
it 'registers no offenses for `Rails.env.local?`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.local? | ||
RUBY | ||
end | ||
|
||
include_examples 'non-local candidates' | ||
end | ||
|
||
context 'In Rails < 7.1', :rails70 do | ||
it 'registers no offenses for `Rails.env.development? || Rails.env.test?`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.development? || Rails.env.test? | ||
Rails.env.test? || Rails.env.development? | ||
RUBY | ||
end | ||
|
||
it 'registers no offenses for `Rails.env.local?`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.local? | ||
RUBY | ||
end | ||
|
||
include_examples 'non-local candidates' | ||
end | ||
end |