Class: GLRubocop::GLCops::ViewComponentDirectoryStructure

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

Overview

This cop checks that all ViewComponent is part of an allowlisted base module.

Good:

module Core
  module Users
    class Component < ApplicationViewComponent
    end
  end
end

Bad:

module Billing
  class Component < ApplicationViewComponent
  end
end

Constant Summary collapse

MSG =
'ViewComponent must belong to an allowed base module: %<allowed>s'.freeze
ALLOWED_MODULES =
%w[Core Admin NonprofitAdmin Packs Users].freeze

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/gl_rubocop/gl_cops/view_component_directory_structure.rb', line 22

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

  base_module = node.parent_module_name&.split('::')&.first
  return true if base_module.nil?
  return true if ALLOWED_MODULES.include?(base_module)

  add_offense(node, message: format(MSG, allowed: ALLOWED_MODULES.join(', ')))
end