Module: RuboCop::Cop::ViewComponent::TemplateAnalyzer
- Included in:
- PreferPrivateMethods
- Defined in:
- lib/rubocop/cop/view_component/template_analyzer.rb
Overview
Helper module for analyzing ViewComponent ERB templates
Instance Method Summary collapse
-
#extract_method_calls(template_path) ⇒ Set<Symbol>
Extract method calls from an ERB template.
- #sibling_templates(base_path) ⇒ Object
- #sidecar_templates(sidecar_dir, component_name) ⇒ Object
-
#template_paths_for(component_path) ⇒ Array<String>
Find template file paths for a component.
Instance Method Details
#extract_method_calls(template_path) ⇒ Set<Symbol>
Extract method calls from an ERB template
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubocop/cop/view_component/template_analyzer.rb', line 44 def extract_method_calls(template_path) return Set.new unless File.exist?(template_path) source = File.read(template_path) ruby_code = Herb.extract_ruby(source) # Parse the extracted Ruby code parse_ruby_for_method_calls(ruby_code) rescue StandardError => e # Graceful degradation on parse errors warn "Warning: Failed to parse template #{template_path}: #{e.}" if ENV["RUBOCOP_DEBUG"] Set.new end |
#sibling_templates(base_path) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rubocop/cop/view_component/template_analyzer.rb', line 25 def sibling_templates(base_path) paths = [] sibling = "#{base_path}.html.erb" paths << sibling if File.exist?(sibling) paths.concat(Dir.glob("#{base_path}.*.html.erb")) paths end |
#sidecar_templates(sidecar_dir, component_name) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/rubocop/cop/view_component/template_analyzer.rb', line 33 def sidecar_templates(sidecar_dir, component_name) paths = [] sidecar = File.join(sidecar_dir, "#{component_name}.html.erb") paths << sidecar if File.exist?(sidecar) paths.concat(Dir.glob(File.join(sidecar_dir, "#{component_name}.*.html.erb"))) paths end |
#template_paths_for(component_path) ⇒ Array<String>
Find template file paths for a component
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rubocop/cop/view_component/template_analyzer.rb', line 13 def template_paths_for(component_path) return [] unless component_path base_path = component_path.sub(/\.rb$/, "") component_dir = File.dirname(component_path) component_name = File.basename(component_path, ".rb") sidecar_dir = File.join(component_dir, component_name) paths = sibling_templates(base_path) + sidecar_templates(sidecar_dir, component_name) paths.uniq end |