Module: YARDSorbet::Directives

Extended by:
T::Sig
Defined in:
lib/yard-sorbet/directives.rb

Overview

Extract & re-add directives to a docstring

Class Method Summary collapse

Class Method Details

.add_directives(docstring, directives) ⇒ void

This method returns an undefined value.

Parameters:

  • docstring (String)
  • directives (Array<String>)


21
22
23
# File 'lib/yard-sorbet/directives.rb', line 21

def self.add_directives(docstring, directives)
  directives.each { docstring.concat("\n#{_1}") }
end

.extract_directives(docstring) ⇒ Array(YARD::Docstring, Array<String>)

Parameters:

  • docstring (String, nil)

Returns:

  • (Array(YARD::Docstring, Array<String>))


10
11
12
13
14
15
16
17
18
# 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 { _1.start_with?('@!') } || []
  [parser.to_docstring, directives]
end