Module: Docscribe::Types::RBS::TypeFormatter

Defined in:
lib/docscribe/types/rbs/type_formatter.rb

Overview

Convert RBS type objects into YARD-ish type strings.

This is intentionally best-effort formatting: YARD type syntax is simpler than RBS, so some information is collapsed or approximated.

Class Method Summary collapse

Class Method Details

.fallback_string(type) ⇒ String

Note:

module_function: when included, also defines #fallback_string (instance visibility: private)

Fallback string conversion for unsupported or unexpected RBS type objects.

Performs a few normalizations for nicer YARD output:

  • strips leading ‘::`

  • converts ‘bool` to `Boolean`

  • converts ‘untyped` to `Object`

Parameters:

  • type (Object)

Returns:

  • (String)


123
124
125
126
127
128
# File 'lib/docscribe/types/rbs/type_formatter.rb', line 123

def fallback_string(type)
  type.to_s
      .gsub(/\A::/, '')
      .gsub(/\bbool\b/, 'Boolean')
      .gsub(/\buntyped\b/, 'Object')
end

.format_named(type, collapse_generics:) ⇒ String

Note:

module_function: when included, also defines #format_named (instance visibility: private)

Format a named RBS type, optionally preserving generic arguments.

Examples:

  • ‘::String` => `“String”`

  • ‘::Hash[::Symbol, untyped]` => `“Hash<Symbol, Object>”`

  • with ‘collapse_generics: true` => `“Hash”`

Parameters:

  • type (Object)

    named RBS type

  • collapse_generics (Boolean)

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/docscribe/types/rbs/type_formatter.rb', line 83

def format_named(type, collapse_generics:)
  name = type.name.to_s.delete_prefix('::')
  args = type.respond_to?(:args) ? type.args : []

  if args && !args.empty?
    return name if collapse_generics

    "#{name}<#{args.map { |a| to_yard(a, collapse_generics: collapse_generics) }.join(', ')}>"
  else
    name
  end
end

.format_union(type, collapse_generics:) ⇒ String

Note:

module_function: when included, also defines #format_union (instance visibility: private)

Format an RBS union type as a comma-separated YARD union.

Example:

  • ‘String | Integer | nil` => `“String, Integer, nil”`

Parameters:

  • type (::RBS::Types::Union)
  • collapse_generics (Boolean)

Returns:

  • (String)


66
67
68
69
70
# File 'lib/docscribe/types/rbs/type_formatter.rb', line 66

def format_union(type, collapse_generics:)
  type.types.map { |t| to_yard(t, collapse_generics: collapse_generics) }
      .uniq
      .join(', ')
end

.literal_to_yard(lit) ⇒ String

Note:

module_function: when included, also defines #literal_to_yard (instance visibility: private)

Map a literal Ruby value from an RBS literal type into a YARD-ish type name.

Parameters:

  • lit (Object)

    literal value

Returns:

  • (String)


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/docscribe/types/rbs/type_formatter.rb', line 101

def literal_to_yard(lit)
  case lit
  when Integer then 'Integer'
  when Float   then 'Float'
  when String  then 'String'
  when Symbol  then 'Symbol'
  when TrueClass, FalseClass then 'Boolean'
  when NilClass then 'nil'
  else 'Object'
  end
end

.to_yard(type, collapse_generics: false) ⇒ String

Note:

module_function: when included, also defines #to_yard (instance visibility: private)

Convert one RBS type object into a YARD-ish string.

Supported categories include:

  • base types (‘bool`, `nil`, `void`, `untyped`)

  • optional and union types

  • named types with optional generic arguments

  • literal types

  • Proc types

Parameters:

  • type (Object)

    RBS type object

  • collapse_generics (Boolean) (defaults to: false)

    whether generic arguments should be omitted

Returns:

  • (String)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/docscribe/types/rbs/type_formatter.rb', line 26

def to_yard(type, collapse_generics: false)
  return 'Object' unless type

  # RBS is loaded lazily by the provider; constants below exist only when rbs is available.
  case type
  when ::RBS::Types::Bases::Any
    'Object'
  when ::RBS::Types::Bases::Bool
    'Boolean'
  when ::RBS::Types::Bases::Void
    'void'
  when ::RBS::Types::Bases::Nil
    'nil'
  when ::RBS::Types::Optional
    "#{to_yard(type.type, collapse_generics: collapse_generics)}?"
  when ::RBS::Types::Union
    format_union(type, collapse_generics: collapse_generics)
  when ::RBS::Types::ClassInstance,
    ::RBS::Types::ClassSingleton,
    ::RBS::Types::Interface,
    ::RBS::Types::Alias
    format_named(type, collapse_generics: collapse_generics)
  when ::RBS::Types::Literal
    literal_to_yard(type.literal)
  when ::RBS::Types::Proc
    'Proc'
  else
    fallback_string(type)
  end
end