Module: RuboCop::Cop::ViewComponent::Base
- Included in:
- ComponentSuffix, MissingPreview, NoGlobalState, PreferComposition, PreferPrivateMethods, PreferSlots
- Defined in:
- lib/rubocop/cop/view_component/base.rb
Overview
Shared helper methods for ViewComponent cops
Instance Method Summary collapse
- #component_namespaces ⇒ Object
-
#enclosing_class(node) ⇒ Object
Find the enclosing class node.
-
#inside_view_component?(node) ⇒ Boolean
Check if node is within a ViewComponent class.
-
#view_component_class?(node) ⇒ Boolean
Check if a class node inherits from ViewComponent::Base or ApplicationComponent.
-
#view_component_parent?(node) ⇒ Boolean
Check if node represents ViewComponent::Base, ApplicationComponent, or a configured additional parent class.
Instance Method Details
#component_namespaces ⇒ Object
33 34 35 |
# File 'lib/rubocop/cop/view_component/base.rb', line 33 def component_namespaces cop_config["ComponentNamespaces"] || [] end |
#enclosing_class(node) ⇒ Object
Find the enclosing class node
38 39 40 |
# File 'lib/rubocop/cop/view_component/base.rb', line 38 def enclosing_class(node) node.each_ancestor(:class).first end |
#inside_view_component?(node) ⇒ Boolean
Check if node is within a ViewComponent class
43 44 45 46 47 48 |
# File 'lib/rubocop/cop/view_component/base.rb', line 43 def inside_view_component?(node) klass = enclosing_class(node) return false unless klass view_component_class?(klass) end |
#view_component_class?(node) ⇒ Boolean
Check if a class node inherits from ViewComponent::Base or ApplicationComponent
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rubocop/cop/view_component/base.rb', line 9 def view_component_class?(node) return false unless node&.class_type? class_source = node.identifier.source return true if component_namespaces.any? { |ns| class_source.start_with?(ns) } parent_class = node.parent_class return false unless parent_class view_component_parent?(parent_class) end |
#view_component_parent?(node) ⇒ Boolean
Check if node represents ViewComponent::Base, ApplicationComponent, or a configured additional parent class
23 24 25 26 27 28 29 30 31 |
# File 'lib/rubocop/cop/view_component/base.rb', line 23 def view_component_parent?(node) return false unless node.const_type? source = node.source return true if source == "ViewComponent::Base" || source == "ApplicationComponent" additional = cop_config["ViewComponentParentClasses"] || [] additional.include?(source) end |