Class: RuboCop::Cop::Naming::ViewComponent

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/naming/view_component.rb

Overview

This cop requires ViewComponent classes to end with Component in their classname.

Examples:

# bad
class Foo < ::ViewComponent::Base
  # ...
end

# good
class FooComponent < ::ViewComponent::Base
  # ...
end

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/naming/view_component.rb', line 20

def on_class(node)
  inheritance_klass = node.node_parts[1]&.source
  return unless view_component_class?(inheritance_klass)

  klass = node.node_parts[0]&.source
  return if klass.end_with?("Component")

  add_offense(node.node_parts[0], message: "End ViewComponent classnames with 'Component'")
end