Class: SorbetView::Compiler::ComponentCompiler

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/sorbet_view/compiler/component_compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(adapter: Adapters::ErbAdapter.new, config: Configuration.load) ⇒ ComponentCompiler

Returns a new instance of ComponentCompiler.



10
11
12
13
14
# File 'lib/sorbet_view/compiler/component_compiler.rb', line 10

def initialize(adapter: Adapters::ErbAdapter.new, config: Configuration.load)
  @adapter = adapter
  @generator = T.let(RubyGenerator.new, RubyGenerator)
  @config = config
end

Instance Method Details

#compile(path, source) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sorbet_view/compiler/component_compiler.rb', line 17

def compile(path, source)
  extractions = HeredocExtractor.extract(source, path)
  extractions.map do |extraction|
    segments = @adapter.extract_segments(extraction.erb_source)

    # Add line_offset/column_offset so source map points to correct positions in the .rb file
    adjusted = segments.map do |seg|
      RubySegment.new(
        code: seg.code,
        line: seg.line + extraction.line_offset,
        column: seg.column + extraction.column_offset,
        type: seg.type
      )
    end

    context = TemplateContext.resolve_component(
      extraction.component_path,
      extraction.class_name,
      @config
    )
    @generator.generate(segments: adjusted, context: context, config: @config, component_mode: true)
  end
end

#compile_file(path) ⇒ Object



42
43
44
45
# File 'lib/sorbet_view/compiler/component_compiler.rb', line 42

def compile_file(path)
  source = File.read(path)
  compile(path, source)
end