Class: RBS::Trace::File

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/trace/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



7
8
9
# File 'lib/rbs/trace/file.rb', line 7

def initialize(path)
  @path = path
end

Instance Method Details

#definitionsObject



12
13
14
# File 'lib/rbs/trace/file.rb', line 12

def definitions
  @definitions ||= {}
end

#rewriteObject



32
33
34
# File 'lib/rbs/trace/file.rb', line 32

def rewrite
  ::File.write(@path, with_rbs)
end

#with_rbsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rbs/trace/file.rb', line 17

def with_rbs
  lines = ::File.readlines(@path)
  reverse_definitions.each do |d|
    next if skip_insert?(lines, d)

    current = d.lineno - 1
    indent = lines[current]&.index("def")
    next unless indent

    lines.insert(current, d.rbs_comment(indent))
  end
  lines.join
end