Class: RuboCop::Cop::Rails::EnvLocal

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRailsVersion
Defined in:
lib/rubocop/cop/rails/env_local.rb

Overview

Checks for usage of ‘Rails.env.development? || Rails.env.test?` which can be replaced with `Rails.env.local?`, introduced in Rails 7.1.

Examples:


# bad
Rails.env.development? || Rails.env.test?

# good
Rails.env.local?

Constant Summary collapse

MSG =
'Use `Rails.env.local?` instead.'
LOCAL_ENVIRONMENTS =
%i[development? test?].to_set.freeze

Constants included from TargetRailsVersion

TargetRailsVersion::USES_REQUIRES_GEM_API

Instance Method Summary collapse

Methods included from TargetRailsVersion

minimum_target_rails_version, support_target_rails_version?

Instance Method Details

#on_or(node) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rails/env_local.rb', line 34

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

#rails_env_local_candidate?(node) ⇒ Object



27
28
29
30
31
32
# File 'lib/rubocop/cop/rails/env_local.rb', line 27

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