Class: GLRubocop::GLCops::ViewComponentClassNaming

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

Overview

This cop checks naming for classes inheriting from ApplicationViewComponent or ApplicationViewComponentPreview.

Good:

class Component < ApplicationViewComponent
end

class ComponentPreview < ApplicationViewComponentPreview
end

Bad:

class UserCardComponent < ApplicationViewComponent
end

class UserCardComponentPreview < ApplicationViewComponentPreview
end

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gl_rubocop/gl_cops/view_component_class_naming.rb', line 20

def on_class(node)
  parent_class = node.parent_class&.const_name
  if parent_class == 'ApplicationViewComponent'
    return true if node.identifier.const_name == 'Component'

    add_offense(node, message: 'ViewComponent class names must be "Component".')
  end
  if parent_class == 'ApplicationViewComponentPreview'
    return true if node.identifier.const_name == 'ComponentPreview'

    add_offense(node, message: 'ViewComponentPreview class names must be "ComponentPreview".')
  end
  true
end