Class: Spoom::Sorbet::Sigs

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/sorbet/sigs.rb

Defined Under Namespace

Classes: Scanner, SigTranslator, SigsLocator

Class Method Summary collapse

Class Method Details

.rbi_to_rbs(ruby_contents) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spoom/sorbet/sigs.rb', line 25

def rbi_to_rbs(ruby_contents)
  ruby_contents = ruby_contents.dup
  sigs = collect_sigs(ruby_contents)

  sigs.each do |sig, node|
    scanner = Scanner.new(ruby_contents)
    start_index = scanner.find_char_position(
      T.must(sig.loc&.begin_line&.pred),
      T.must(sig.loc).begin_column,
    )
    end_index = scanner.find_char_position(
      sig.loc&.end_line&.pred,
      T.must(sig.loc).end_column,
    )
    ruby_contents[start_index...end_index] = SigTranslator.translate(sig, node)
  end

  ruby_contents
end

.strip(ruby_contents) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/spoom/sorbet/sigs.rb', line 13

def strip(ruby_contents)
  sigs = collect_sigs(ruby_contents)
  lines_to_strip = sigs.flat_map { |sig, _| (sig.loc&.begin_line..sig.loc&.end_line).to_a }

  lines = []
  ruby_contents.lines.each_with_index do |line, index|
    lines << line unless lines_to_strip.include?(index + 1)
  end
  lines.join
end