Class: RuboCop::Cop::Rails::Env

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

Overview

Checks for usage of ‘Rails.env` which can be replaced with Feature Flags

Examples:


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

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

Constant Summary collapse

MSG =
'Use Feature Flags or config instead of `Rails.env`.'
RESTRICT_ON_SEND =
%i[env].freeze
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?, local?

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?
  ]
).freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/rails/env.rb', line 44

def on_send(node)
  return unless node.receiver&.const_name == 'Rails'

  parent = node.parent
  return unless parent.respond_to?(:predicate_method?) && parent.predicate_method?

  return if ALLOWED_LIST.include?(parent.method_name)

  add_offense(parent)
end