Class: RuboCop::Cop::Rails::UnknownEnv

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rails/unknown_env.rb

Overview

Checks that environments called with ‘Rails.env` predicates exist. By default the cop allows three environments which Rails ships with: `development`, `test`, and `production`. More can be added to the `Environments` config parameter.

Examples:

# bad
Rails.env.proudction?
Rails.env == 'proudction'
Rails.env != 'proudction'

# good
Rails.env.production?
Rails.env == 'production'
Rails.env != 'production'
# bad
case Rails.env
when 'proudction'
  do_something
end

# good
case Rails.env
when 'production'
  do_something
end

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#on_case(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubocop/cop/rails/unknown_env.rb', line 68

def on_case(node)
  return unless rails_env?(node.condition)

  node.when_branches.each do |branch|
    branch.conditions.each do |condition|
      next unless condition.str_type?
      next unless unknown_env_name?(condition.value)

      add_offense(condition, message: message(condition.value))
    end
  end
end

#on_send(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/cop/rails/unknown_env.rb', line 57

def on_send(node)
  unknown_environment_predicate?(node) do |name|
    add_offense(node.loc.selector, message: message(name))
  end

  unknown_environment_equal?(node) do |str_node|
    name = str_node.value
    add_offense(str_node, message: message(name))
  end
end