-
-
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
936845d
commit 1004ce3
Showing
5 changed files
with
106 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,45 @@ | ||
# 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 | ||
|
||
minimum_target_rails_version 7.1 | ||
|
||
MSG = 'Use `Rails.env.local?` instead.' | ||
|
||
LOCAL_ENVIRONMENTS = %i(development? test?).to_set.freeze | ||
|
||
# @!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 LOCAL_ENVIRONMENTS == environments.to_set | ||
|
||
add_offense(node) { |corrector| corrector.replace(node, "Rails.env.local?") } | ||
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,54 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::EnvLocal, :config do | ||
shared_examples "non-local candidates" do | ||
it "registers no offenses for combined non-local env checks" 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 environment checks" do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.development? | ||
Rails.env.test? | ||
Rails.env.production? | ||
Rails.env.other? | ||
Rails.env.local? | ||
RUBY | ||
end | ||
end | ||
|
||
context 'In Rails >= 7.1', :rails71 do | ||
let(:message) { RuboCop::Cop::Rails::EnvLocal::MSG } | ||
|
||
it "registers an offense for combined local env checks" do | ||
expect_offense(<<~RUBY) | ||
Rails.env.development? || Rails.env.test? | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message} | ||
Rails.env.test? || Rails.env.development? | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message} | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.env.local? | ||
Rails.env.local? | ||
RUBY | ||
end | ||
|
||
include_examples "non-local candidates" | ||
end | ||
|
||
context 'In Rails < 7.1', :rails70 do | ||
it "registers no offenses for combined local env checks" do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.env.development? || Rails.env.test? | ||
Rails.env.test? || Rails.env.development? | ||
RUBY | ||
end | ||
|
||
include_examples "non-local candidates" | ||
end | ||
end |