Sha256: b992bca4aa3c2add2b27c2da08c2006e360edbb046bd555cac72359d424d3c7e

Contents?: true

Size: 937 Bytes

Versions: 4

Compression:

Stored size: 937 Bytes

Contents

# typed: strict
# frozen_string_literal: true

module YARDSorbet
  # Extract & re-add directives to a docstring
  module 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 { _1.start_with?('@!') } || []
      [parser.to_docstring, directives]
    end

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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
yard-sorbet-0.8.1 lib/yard-sorbet/directives.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/yard-sorbet-0.8.0/lib/yard-sorbet/directives.rb
yard-sorbet-0.8.0 lib/yard-sorbet/directives.rb
yard-sorbet-0.7.0 lib/yard-sorbet/directives.rb