Class: RuboCop::Cop::ViewComponent::PreferSlots

Inherits:
Base
  • Object
show all
Includes:
Base
Defined in:
lib/rubocop/cop/view_component/prefer_slots.rb

Overview

Detects parameters that accept HTML content and suggests using slots.

Examples:

# bad
class ModalComponent < ViewComponent::Base
  def initialize(title:, body_html:)
    @title = title
    @body_html = body_html
  end
end

# good
class ModalComponent < ViewComponent::Base
  renders_one :body

  def initialize(title:)
    @title = title
  end
end

Constant Summary collapse

MSG =
"Consider using `%<slot_method>s` instead of passing HTML " \
"as a parameter. This maintains Rails' automatic HTML escaping."
HTML_PARAM_PATTERN =
/_html$/

Instance Method Summary collapse

Methods included from Base

#enclosing_class, #inside_view_component?, #view_component_class?, #view_component_parent?

Instance Method Details

#on_class(node) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/view_component/prefer_slots.rb', line 36

def on_class(node)
  return unless view_component_class?(node)

  initialize_method = find_initialize(node)
  return unless initialize_method

  check_initialize_params(initialize_method)
end