Sha256: 4ead4da570718a19e6d8cdd12ca25fd04b7429b16c94e37060991b166438ab5d

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

# 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

Version data entries

14 entries across 14 versions & 4 rubygems

Version Path
rubocop-rails-2.26.2 lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/env_local.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/env_local.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/env_local.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/env_local.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/env_local.rb