Module: Metz::TemplateRubyExtractor
- Included in:
- HamlRubyExtractor, SlimRubyExtractor
- Defined in:
- lib/metz/template_ruby_extractor.rb
Overview
Shared iteration backbone for line-based Ruby extractors that target indentation-driven template languages (HAML, Slim). Concrete extractors define three patterns -- an expression line, a statement line, and a filter-block start -- and this module walks the raw source line by line, hands every matching body to RuboCop as its own ProcessedSource, and tracks the byte offset of each body inside the original template so offense locations refer back to the source file rather than the synthetic snippet.
Defined Under Namespace
Instance Method Summary collapse
- #build_processed_source(body, processed_source) ⇒ Object
- #build_snippet(body, body_offset, processed_source) ⇒ Object
- #call(processed_source) ⇒ Object
- #empty_snippet(processed_source) ⇒ Object
- #install! ⇒ Object
Instance Method Details
#build_processed_source(body, processed_source) ⇒ Object
44 45 46 47 |
# File 'lib/metz/template_ruby_extractor.rb', line 44 def build_processed_source(body, processed_source) ruby_version = processed_source.ruby_version || RUBY_VERSION.to_f RuboCop::ProcessedSource.new(body, ruby_version, processed_source.path) end |
#build_snippet(body, body_offset, processed_source) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/metz/template_ruby_extractor.rb', line 35 def build_snippet(body, body_offset, processed_source) return nil if body.strip.empty? ps = build_processed_source(body, processed_source) return nil unless ps.valid_syntax? { offset: body_offset, processed_source: ps } end |
#call(processed_source) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/metz/template_ruby_extractor.rb', line 15 def call(processed_source) path = processed_source.path return nil unless path && handles?(path) snippets = Walker.new(self, processed_source).snippets snippets << empty_snippet(processed_source) if snippets.empty? snippets end |
#empty_snippet(processed_source) ⇒ Object
30 31 32 33 |
# File 'lib/metz/template_ruby_extractor.rb', line 30 def empty_snippet(processed_source) ps = build_processed_source("", processed_source) { offset: 0, processed_source: ps } end |
#install! ⇒ Object
24 25 26 27 28 |
# File 'lib/metz/template_ruby_extractor.rb', line 24 def install! extractors = RuboCop::Runner.ruby_extractors entrypoint = method(:call) extractors.unshift(entrypoint) unless extractors.include?(entrypoint) end |