Module: YardToRbsInline::Converter

Defined in:
lib/yard_to_rbs_inline/converter.rb,
lib/yard_to_rbs_inline/converter/code_visitor.rb,
lib/yard_to_rbs_inline/converter/subscriptable.rb,
lib/yard_to_rbs_inline/converter/text_with_mod.rb

Defined Under Namespace

Modules: Subscriptable Classes: AppendLineContent, CodeVisitor, PrependLine, TextWithMod

Class Method Summary collapse

Class Method Details

.convert(file, dry_run: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yard_to_rbs_inline/converter.rb', line 8

def self.convert(file, dry_run: false)
  result = Prism.parse_file(file)

  visitor = CodeVisitor.new(source: result.source, comments: result.comments)
  unless result.source.source.match(/^# rbs_inline:/)
    visitor.text_with_mod.mods << PrependLine.new(line_num: 1, content: "# rbs_inline: enabled")
    visitor.text_with_mod.mods << PrependLine.new(line_num: 1, content: "")
  end

  result.value.accept(visitor)

  if dry_run
    puts visitor.text_with_mod.modified_text
  else
    File.write(file, visitor.text_with_mod.modified_text)
  end
end