Module: Solargraph::RbsTranslator

Defined in:
lib/solargraph/rbs_translator.rb

Overview

Convert RBS types to complex types and pins.

Constant Summary collapse

RBS_TO_YARD_TYPE =
{
  'bool' => 'Boolean',
  'string' => 'String',
  'int' => 'Integer',
  'untyped' => '',
  'NilClass' => 'nil'
}.freeze

Class Method Summary collapse

Class Method Details

.to_complex_type(type, type_alias_decls: {}) ⇒ ComplexType

Relative type names (e.g. Bar in an inline #: () -> Bar) stay unrooted so they resolve against the pin's namespace like YARD tags do. Absolute names (::Bar, or any name from an RBS environment after resolve_type_names) and RBS builtins stay rooted.

Parameters:

  • type (RBS::Types::t)
  • type_alias_decls (Hash{String => RBS::AST::Declarations::TypeAlias}) (defaults to: {})

Returns:



23
24
25
# File 'lib/solargraph/rbs_translator.rb', line 23

def self.to_complex_type type, type_alias_decls: {}
  ComplexType.try_parse(type_to_tag(type, type_alias_decls))
end

.to_parameter_pin(param_type, name, decl, closure, type_alias_decls: {}) ⇒ Pin::Parameter

Parameters:

  • param_type (RBS::Types::Function::Param)
  • name (String)
  • decl (Symbol)
  • closure (Pin::Closure)
  • type_alias_decls (Hash{String => RBS::AST::Declarations::TypeAlias}) (defaults to: {})

Returns:



33
34
35
36
37
38
39
40
41
42
# File 'lib/solargraph/rbs_translator.rb', line 33

def self.to_parameter_pin param_type, name, decl, closure, type_alias_decls: {}
  return_type = if decl == :restarg
                  ComplexType.parse('Array')
                elsif decl == :kwrestarg
                  ComplexType.parse('Hash{Symbol => Object}')
                else
                  RbsTranslator.to_complex_type(param_type.type, type_alias_decls: type_alias_decls)
                end
  Solargraph::Pin::Parameter.new(decl: decl, name: name, closure: closure, return_type: return_type, source: :rbs, type_location: to_sg_location(param_type.location) || closure.type_location)
end

.to_parameter_pins(method_type, closure, parameter_names = [], type_alias_decls: {}) ⇒ Array<Pin::Parameter>

Parameters:

  • method_type (RBS::MethodType, RBS::Types::Block)
  • closure (Pin::Closure)
  • parameter_names (Array<String>) (defaults to: [])
  • type_alias_decls (Hash{String => RBS::AST::Declarations::TypeAlias}) (defaults to: {})

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/solargraph/rbs_translator.rb', line 49

def self.to_parameter_pins method_type, closure, parameter_names = [], type_alias_decls: {}
  if defined?(RBS::Types::UntypedFunction) && method_type.type.is_a?(RBS::Types::UntypedFunction)
    return [
      Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: closure, source: :rbs)
    ]
  end

  arg_num = 0
  params = []
  method_type.type.required_positionals.each do |param|
    # @sg-ignore Unresolved call to name on RBS::Types::Function::Param
    params.push RbsTranslator.to_parameter_pin(param, param.name&.to_s || parameter_names[arg_num] || "arg_#{arg_num}", :arg, closure, type_alias_decls: type_alias_decls)
    arg_num += 1
  end
  method_type.type.optional_positionals.each do |param|
    # @sg-ignore Unresolved call to name on RBS::Types::Function::Param
    params.push RbsTranslator.to_parameter_pin(param, param.name&.to_s || parameter_names[arg_num] || "arg_#{arg_num}", :optarg, closure, type_alias_decls: type_alias_decls)
    arg_num += 1
  end
  if method_type.type.rest_positionals
    rest_positionals = method_type.type.rest_positionals
    # @sg-ignore Unresolved call to name on RBS::Types::Function::Param
    rest_name = rest_positionals.name&.to_s || parameter_names[arg_num] || "arg_#{arg_num}"
    params.push RbsTranslator.to_parameter_pin(rest_positionals, rest_name, :restarg, closure, type_alias_decls: type_alias_decls)
    arg_num += 1
  end
  method_type.type.required_keywords.each do |param|
    # @sg-ignore Unresolved calls to last, first on generic<D>
    params.push RbsTranslator.to_parameter_pin(param.last, param.first.to_s, :kwarg, closure, type_alias_decls: type_alias_decls)
    arg_num += 1
  end
  method_type.type.optional_keywords.each do |param|
    # @sg-ignore Unresolved calls to last, first on generic<D>
    params.push RbsTranslator.to_parameter_pin(param.last, param.first.to_s, :kwoptarg, closure, type_alias_decls: type_alias_decls)
    arg_num += 1
  end
  if method_type.type.rest_keywords
    rest_keywords = method_type.type.rest_keywords
    # @sg-ignore Unresolved call to name on RBS::Types::Function::Param
    rest_keywords_name = rest_keywords.name&.to_s || parameter_names[arg_num] || "arg_#{arg_num}"
    params.push RbsTranslator.to_parameter_pin(rest_keywords, rest_keywords_name, :kwrestarg, closure, type_alias_decls: type_alias_decls)
  end
  params
end

.to_sg_location(location) ⇒ Solargraph::Location?

Parameters:

  • location (RBS::Location, nil)

Returns:



119
120
121
122
123
124
125
126
# File 'lib/solargraph/rbs_translator.rb', line 119

def self.to_sg_location location
  return nil if location&.name.nil?

  start_pos = Position.new(location.start_line - 1, location.start_column)
  end_pos = Position.new(location.end_line - 1, location.end_column)
  range = Range.new(start_pos, end_pos)
  Location.new(location.name.to_s, range)
end

.to_signature(method_type, closure, parameter_names = [], type_alias_decls: {}) ⇒ Pin::Signature

Parameters:

  • method_type (RBS::MethodType)
  • closure (Pin::Closure)
  • parameter_names (Array<String>) (defaults to: [])
  • type_alias_decls (Hash{String => RBS::AST::Declarations::TypeAlias}) (defaults to: {})

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/solargraph/rbs_translator.rb', line 99

def self.to_signature method_type, closure, parameter_names = [], type_alias_decls: {}
  # There may be edge cases here around different signatures
  # having different type params / orders - we may need to match
  # this data model and have generics live in signatures to
  # handle those correctly
  generics = method_type.type_params.map(&:name).map(&:to_s).uniq
  parameters = to_parameter_pins(method_type, closure, parameter_names, type_alias_decls: type_alias_decls)
  return_type = to_complex_type(method_type.type.return_type, type_alias_decls: type_alias_decls)
  block = if method_type.block
            # @sg-ignore https://github.com/castwide/solargraph/issues/1249
            block_parameters = to_parameter_pins(method_type.block, closure, type_alias_decls: type_alias_decls)
            block_return_type = to_complex_type(method_type.block.type.return_type, type_alias_decls: type_alias_decls)
            Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type, source: :rbs, type_location: closure.location, closure: closure)
          end
  Pin::Signature.new(generics: generics, parameters: parameters, return_type: return_type, block: block,
                     block_required: method_type.block&.required || false, source: :rbs, type_location: closure.location, closure: closure)
end