Class: RuboCop::Cop::OpenProject::AddPreviewForViewComponent

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/open_project/add_preview_for_view_component.rb

Overview

A lookbook preview must exist for each ViewComponent.

Components are located in ‘app/components` and previews are searched in `lookbook/previews`.

Constant Summary collapse

COMPONENT_PATH =
"/app/components/"
PREVIEW_PATH =
"/lookbook/previews/"

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/cop/open_project/add_preview_for_view_component.rb', line 14

def on_class(node)
  path = node.loc.expression.source_buffer.name
  return unless path.include?(COMPONENT_PATH) && path.end_with?(".rb")

  preview_path = path.sub(COMPONENT_PATH, PREVIEW_PATH).sub(".rb", "_preview.rb")

  return if File.exist?(preview_path)

  message = "Missing Lookbook preview for #{path}. Expected preview to exist at #{preview_path}."
  add_offense(node.loc.name, message:)
end