Class: RuboCop::Cop::Gusto::FeatureFlagConstants

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

Overview

Enforces that FeatureFlag.active? is called with a constant rather than a string literal. Defining flag keys as constants keeps them in one place, makes typos a load-time error instead of a silent always-off flag, and lets tools find every reference to a flag.

FeatureFlag is a constant, so it is never nil and safe navigation (FeatureFlag&.active?) is never used; the cop only handles on_send.

Examples:

# bad
FeatureFlag.active?("some_feature_flag")

# good
FeatureFlag.active?(SomeModule::SOME_FEATURE_FLAG)

Constant Summary collapse

MSG =
"FeatureFlag keys should be constants, not strings"
RESTRICT_ON_SEND =
%i(active?).freeze

Instance Method Summary collapse

Instance Method Details

#feature_flag_with_string?(node) ⇒ Object



25
26
27
# File 'lib/rubocop/cop/gusto/feature_flag_constants.rb', line 25

def_node_matcher :feature_flag_with_string?, <<~PATTERN
  (send (const nil? :FeatureFlag) :active? (str _) ...)
PATTERN

#on_send(node) ⇒ Object



29
30
31
# File 'lib/rubocop/cop/gusto/feature_flag_constants.rb', line 29

def on_send(node)
  add_offense(node) if feature_flag_with_string?(node)
end