Sha256: a0742e5361bff3c260c26016928642f0d170519b7d86c2860f2386892a820d07

Contents?: true

Size: 946 Bytes

Versions: 6

Compression:

Stored size: 946 Bytes

Contents

# typed: strict
# frozen_string_literal: true

# Extract & re-add directives to a docstring
module YARDSorbet::Directives
  extend T::Sig

  sig { params(docstring: T.nilable(String)).returns([YARD::Docstring, T::Array[String]]) }
  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

  sig { params(docstring: String, directives: T::Array[String]).void }
  def self.add_directives(docstring, directives)
    directives.each do |directive|
      docstring.concat("\n#{directive}")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
yard-sorbet-0.5.1 lib/yard-sorbet/directives.rb
yard-sorbet-0.5.0 lib/yard-sorbet/directives.rb
yard-sorbet-0.4.1 lib/yard-sorbet/directives.rb
yard-sorbet-0.4.0 lib/yard-sorbet/directives.rb
yard-sorbet-0.3.0 lib/yard-sorbet/directives.rb
yard-sorbet-0.2.0 lib/yard-sorbet/directives.rb