module YARDSorbet::Directives

Extract & re-add directives to a docstring

Public Class Methods

add_directives(docstring, directives) click to toggle source
# File lib/yard-sorbet/directives.rb, line 24
def self.add_directives(docstring, directives)
  directives.each do |directive|
    docstring.concat("\n#{directive}")
  end
end
extract_directives(docstring) click to toggle source
# File lib/yard-sorbet/directives.rb, line 10
def self.extract_directives(docstring)
  parser = YARD::DocstringParser.new.parse(docstring)
  # Directives are already parsed at this point, and there doesn't
  # seem to be an API to tweeze them from one node to another without
  # managing YARD internal state. Instead, we just extract them from
  # the raw text and re-attach them.
  directives = parser.raw_text&.split("\n")&.select do |line|
    line.start_with?('@!')
  end || []

  [parser.to_docstring, directives]
end