Module: YARDSorbet::TagUtils

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

Overview

Helper methods for working with YARD tags

Constant Summary collapse

VOID_RETURN_TYPE =

The void return type, as a constant to reduce array allocations

T.let(['void'].freeze, [String])

Class Method Summary collapse

Class Method Details

.find_tag(docstring, tag_name, name) ⇒ YARD::Tags::Tag?

Returns the tag with the matching tag_name and name, or nil.

Parameters:

  • docstring (YARD::Docstring)
  • tag_name (String)
  • name (String, nil)

Returns:

  • (YARD::Tags::Tag, nil)

    the tag with the matching tag_name and name, or nil



16
# File 'lib/yard-sorbet/tag_utils.rb', line 16

def self.find_tag(docstring, tag_name, name) = docstring.tags.find { _1.tag_name == tag_name && _1.name == name }

.upsert_tag(docstring, tag_name, types = nil, name = nil, text = '') ⇒ void

This method returns an undefined value.

Create or update a YARD tag with type information

Parameters:

  • docstring (YARD::Docstring)
  • tag_name (String)
  • types (Array<String>, nil) (defaults to: nil)
  • name (String, nil) (defaults to: nil)
  • text (String) (defaults to: '')


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yard-sorbet/tag_utils.rb', line 28

def self.upsert_tag(docstring, tag_name, types = nil, name = nil, text = '')
  tag = find_tag(docstring, tag_name, name)
  if tag
    return unless types

    # Updating a tag in place doesn't seem to work, so we'll delete it, add the types, and re-add it
    docstring.delete_tag_if { _1 == tag }
    # overwrite any existing type annotation (sigs should win)
    tag.types = types
    tag.text = text unless text.empty?
  else
    tag = YARD::Tags::Tag.new(tag_name, text, types, name)
  end
  docstring.add_tag(tag)
end