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>)


24
25
26
27
28
# 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) ⇒ Array(YARD::Docstring, Array<String>)

Parameters:

  • docstring (String, nil)

Returns:

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


10
11
12
13
14
15
16
17
18
19
20
21
# 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