-
-
Notifications
You must be signed in to change notification settings - Fork 253
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
d1414a7
commit e259391
Showing
5 changed files
with
180 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,78 @@ | ||
# 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 EnforcedStyle: local (default) | ||
# | ||
# # bad | ||
# Rails.env.development? || Rails.env.test? | ||
# | ||
# # good | ||
# Rails.env.local? | ||
# | ||
# @example EnforcedStyle: no_local | ||
# | ||
# # bad | ||
# Rails.env.local? | ||
# | ||
# # good | ||
# Rails.env.development? || Rails.env.test? | ||
# | ||
class EnvLocal < Base | ||
extend AutoCorrector | ||
extend TargetRailsVersion | ||
|
||
LOCAL_MSG = 'Use `Rails.env.local?` instead.' | ||
NO_LOCAL_MSG = 'Use `Rails.env.development? || Rails.env.test?` instead.' | ||
LOCAL_ENVIRONMENTS = %i[development? test?].to_set.freeze | ||
RESTRICT_ON_SEND = %i[local?].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_node_matcher :rails_env_local?, <<~PATTERN | ||
(send (send (const {cbase nil? } :Rails) :env) :local?) | ||
PATTERN | ||
|
||
def on_or(node) | ||
return unless prefer_local? | ||
|
||
rails_env_local_candidate?(node) do |*environments| | ||
next unless LOCAL_ENVIRONMENTS == environments.to_set | ||
|
||
add_offense(node, message: LOCAL_MSG) do |corrector| | ||
corrector.replace(node, 'Rails.env.local?') | ||
end | ||
end | ||
end | ||
|
||
def on_send(node) | ||
return if prefer_local? | ||
|
||
rails_env_local?(node) do | ||
add_offense(node, message: NO_LOCAL_MSG) do |corrector| | ||
corrector.replace(node, 'Rails.env.development? || Rails.env.test?') | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def prefer_local? | ||
cop_config.fetch("EnforcedStyle") == "local" | ||
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,91 @@ | ||
# 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 | ||
context "with EnforcedStyle: local" do | ||
let(:cop_config) { { "EnforcedStyle" => "local" } } | ||
|
||
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 "with EnforcedStyle: no_local" do | ||
let(:cop_config) { { "EnforcedStyle" => "no_local" } } | ||
|
||
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 an offense for `Rails.env.local?`' do | ||
expect_offense(<<~RUBY) | ||
Rails.env.local? | ||
^^^^^^^^^^^^^^^^ Use `Rails.env.development? || Rails.env.test?` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.env.development? || Rails.env.test? | ||
RUBY | ||
end | ||
|
||
include_examples 'non-local candidates' | ||
end | ||
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 |