Class: GLRubocop::GLCops::LimitFlashOptions

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/gl_rubocop/gl_cops/limit_flash_options.rb

Constant Summary collapse

ALLOWED_FLASH_KEYS =

Bad:

flash[:error] = "Not allowed"
flash.now[:anything_else] = "Not allowed"
Alert::Component.new(type: :notice, message: "Error")
Notifications::Dismissible::Component.new(variant: :blue_box, message: "Error")
i[success info warning danger].freeze

Instance Method Summary collapse

Instance Method Details

#check_key(node, key) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/gl_rubocop/gl_cops/limit_flash_options.rb', line 69

def check_key(node, key)
  return false unless key
  return false if ALLOWED_FLASH_KEYS.include?(key)

  add_offense(
    node,
    message: "'#{key}' is not one of the permitted flash keys: #{ALLOWED_FLASH_KEYS}"
  )
end

#on_send(node) ⇒ Object

Checks for usage of flash or flash.now with keys not in the allowlist



63
64
65
66
67
# File 'lib/gl_rubocop/gl_cops/limit_flash_options.rb', line 63

def on_send(node)
  check_key(node, rails_flash?(node))
  check_key(node, alert_component_new?(node))
  check_key(node, notifications_dismissible_component_new?(node))
end