Class: RuboCop::Cop::Ezcater::FeatureFlagNameValid

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/ezcater/feature_flag_name_valid.rb

Overview

Match the naming validations in the feature flag service.

Examples:


# good
EzFF.at_100?("Flag1")
EzFF.active?("Foo::Bar", tracking_id: 1234)
EzFF.at_100?("Foo::Bar::Baz")
MY_FLAG="Foo::Bar"; EzFF.at_100?(MY_FLAG)

# bad
EzFF.at_100?("Foo:Bar")
EzFF.active?("Foo:::Bar", tracking_id: 1234)
EzFF.at_100?("Foo:Bar:Baz")
EzFF.at_100?("Foo::Bar  ")
EzFF.at_100?("Foo::Bar && rm -rf * ")
EzFF.active?("foo::bar", identifiers: ["user:1"])
MY_FLAG="Foo:bar"

Constant Summary collapse

WHITESPACE =
/\s/
ISOLATED_COLON =
/(?<!:):(?!:)/
TRIPLE_COLON =
/:::/
INVALID_CHARACTERS =
/[^a-zA-Z0-9:]/
TITLECASE_SEGMENT =
/^([A-Z][a-z0-9]*)+$/
WHITESPACE_MSG =
"Feature flag names must not contain whitespace."
DOUBLE_COLON_MSG =
"Feature flag names must use double colons (::) as namespace separators."
INVALID_CHARACTERS_MSG =
"Feature flag names must only contain alphanumeric characters and colons."
TITLECASE_SEGMENT_MSG =
"Feature flag names must use titlecase for each segment."

Instance Method Summary collapse

Instance Method Details

#on_casgn(node) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/ezcater/feature_flag_name_valid.rb', line 45

def on_casgn(node)
  feature_flag_constant_assignment(node) do |const_name, flag_name|
    if const_name.end_with?("_FF", "_FLAG", "_FLAG_NAME", "_FEATURE_FLAG")
      errors = find_name_violations(flag_name)
      add_offense(node, location: :expression, message: errors.join(", ")) if errors.any?
    end
  end
end

#on_send(node) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/rubocop/cop/ezcater/feature_flag_name_valid.rb', line 54

def on_send(node)
  return unless feature_flag_method_call(node)

  feature_flag_method_call(node) do |flag_name|
    errors = find_name_violations(flag_name)
    add_offense(node, location: :expression, message: errors.join(", ")) if errors.any?
  end
end