Class: SorbetView::Compiler::HeredocExtractor

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

Constant Summary collapse

ERB_TEMPLATE_PATTERN =
/erb_template\s+<<~([A-Z_]+)/

Class Method Summary collapse

Class Method Details

.contains_erb_template?(source) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/sorbet_view/compiler/heredoc_extractor.rb', line 20

def self.contains_erb_template?(source)
  ERB_TEMPLATE_PATTERN.match?(source)
end

.extract(source, file_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sorbet_view/compiler/heredoc_extractor.rb', line 25

def self.extract(source, file_path)
  extractions = T.let([], T::Array[HeredocExtraction])
  lines = source.lines

  i = 0
  while i < lines.length
    line = T.must(lines[i])
    match = ERB_TEMPLATE_PATTERN.match(line)
    if match
      delimiter = T.must(match[1])
      heredoc_start = i + 1
      heredoc_lines = T.let([], T::Array[String])

      j = heredoc_start
      while j < lines.length
        heredoc_line = T.must(lines[j])
        break if heredoc_line.strip == delimiter

        heredoc_lines << heredoc_line
        j += 1
      end

      erb_source, column_offset = dedent_heredoc(heredoc_lines)
      class_name = class_name_from_source(source)

      extractions << HeredocExtraction.new(
        erb_source: erb_source,
        line_offset: heredoc_start,
        column_offset: column_offset,
        class_name: class_name,
        component_path: file_path
      )

      i = j + 1
    else
      i += 1
    end
  end

  extractions
end