Class: RuboCop::Cop::Gusto::RailsEnv

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/gusto/rails_env.rb

Overview

NOTE: Being pushed upstream here: github.com/rubocop/rubocop-rails/pull/1375 Checks for usage of ‘Rails.env` which can be replaced with Feature Flags

Although ‘local?` is a form of an environment-specific check, it is allowed because it cannot be used to control overall environment rollout, but it can be helpful to distinguish or protect code that is explicitly written to only ever execute in a dev or test environment. `local?` is also a form of a feature flag.

Examples:


# bad
Rails.env.production? || Rails.env.demo?

# good
if FeatureFlag.enabled?(:new_feature)
  # new feature code
end

# good
raise unless Rails.env.local?

# good
abort ("The Rails environment is running in production mode!") unless Rails.env.local?

Constant Summary collapse

ALLOWED_LIST =

This allow list is derived from: (Rails.env.methods - Object.instance_methods).select { |m| m.to_s.end_with?(‘?’) } and then removing the environment specific methods like development?, test?, production?

Set.new(
  %i(
    unicode_normalized?
    exclude?
    empty?
    acts_like_string?
    include?
    is_utf8?
    casecmp?
    match?
    starts_with?
    ends_with?
    start_with?
    end_with?
    valid_encoding?
    ascii_only?
    between?
    local?
  )
).freeze
MSG =
"Use Feature Flags or config instead of `Rails.env`."
RESTRICT_ON_SEND =
%i(env).freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



65
66
67
# File 'lib/rubocop/cop/gusto/rails_env.rb', line 65

def on_send(node)
  add_offense(node.parent) if prohibited_rails_env?(node.parent)
end

#prohibited_rails_env?(node) ⇒ Object



58
59
60
61
62
63
# File 'lib/rubocop/cop/gusto/rails_env.rb', line 58

def_node_matcher :prohibited_rails_env?, <<~PATTERN
  (send
    (send (const _ :Rails) :env)
    #prohibited_predicate?
  )
PATTERN