Sha256: 4e08ae009f35d8c6584f65abd4647da69c009d473d1b5015553454914ecfa8b0

Contents?: true

Size: 1000 Bytes

Versions: 4

Compression:

Stored size: 1000 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 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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yard-sorbet-0.6.1 lib/yard-sorbet/directives.rb
yard-sorbet-0.6.0 lib/yard-sorbet/directives.rb
yard-sorbet-0.5.3 lib/yard-sorbet/directives.rb
yard-sorbet-0.5.2 lib/yard-sorbet/directives.rb