Sha256: 9f4622bc9ecb65491f6081f6c22e9c68992739539e153acd3186705a1b7a7ef6

Contents?: true

Size: 1.5 KB

Versions: 11

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks that environments called with `Rails.env` predicates
      # exist.
      #
      # @example
      #   # bad
      #   Rails.env.proudction?
      #
      #   # good
      #   Rails.env.production?
      class UnknownEnv < Cop
        include NameSimilarity

        MSG = 'Unknown environment `%<name>s`.'
        MSG_SIMILAR = 'Unknown environment `%<name>s`. ' \
                      'Did you mean `%<similar>s`?'

        def_node_matcher :unknown_environment?, <<-PATTERN
          (send
            (send
              {(const nil? :Rails) (const (cbase) :Rails)}
              :env)
            $#unknown_env_name?)
        PATTERN

        def on_send(node)
          unknown_environment?(node) do |name|
            add_offense(node, location: :selector, message: message(name))
          end
        end

        private

        def collect_variable_like_names(_scope)
          environments.map { |env| env + '?' }
        end

        def message(name)
          similar = find_similar_name(name.to_s, [])
          if similar
            format(MSG_SIMILAR, name: name, similar: similar)
          else
            format(MSG, name: name)
          end
        end

        def unknown_env_name?(name)
          name = name.to_s
          name.end_with?('?') &&
            !environments.include?(name[0..-2])
        end

        def environments
          cop_config['Environments']
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
rubocop-rails-2.3.2 lib/rubocop/cop/rails/unknown_env.rb
rubocop-rails-2.3.1 lib/rubocop/cop/rails/unknown_env.rb
rubocop-rails-2.3.0 lib/rubocop/cop/rails/unknown_env.rb
rubocop-rails-2.2.1 lib/rubocop/cop/rails/unknown_env.rb
rubocop-rails-2.2.0 lib/rubocop/cop/rails/unknown_env.rb
rubocop-rails-2.1.0 lib/rubocop/cop/rails/unknown_env.rb
rubocop-rails-2.0.1 lib/rubocop/cop/rails/unknown_env.rb
rubocop-0.71.0 lib/rubocop/cop/rails/unknown_env.rb
rubocop-rails-2.0.0 lib/rubocop/cop/rails/unknown_env.rb
rubocop-0.70.0 lib/rubocop/cop/rails/unknown_env.rb
rubocop-0.69.0 lib/rubocop/cop/rails/unknown_env.rb