Class: Parlour::RbsGenerator::MethodSignature

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbs_generator/method_signature.rb

Overview

Represents one signature in a method definition. (This is not an RbsObject because it doesn’t generate a full line.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, return_type = nil, block: nil, type_parameters: nil) ⇒ void

Creates a new method signature.

Parameters:

  • parameters (Array<Parameter>)

    An array of Parameter instances representing this method’s parameters.

  • return_type (Types::TypeLike, nil) (defaults to: nil)

    What this method returns. Passing nil denotes a void return.

  • block (Types::TypeLike, nil) (defaults to: nil)

    The block this method accepts. Passing nil denotes none.

  • type_parameters (Array<Symbol>, nil) (defaults to: nil)

    This method’s type parameters.



25
26
27
28
29
30
# File 'lib/parlour/rbs_generator/method_signature.rb', line 25

def initialize(parameters, return_type = nil, block: nil, type_parameters: nil)
  @parameters = parameters
  @return_type = return_type
  @block = block
  @type_parameters = type_parameters || []
end

Instance Attribute Details

#blockBlock? (readonly)

The block this method accepts.

Returns:



59
60
61
# File 'lib/parlour/rbs_generator/method_signature.rb', line 59

def block
  @block
end

#parametersArray<Parameter> (readonly)

An array of Parameter instances representing this method’s parameters.

Returns:



49
50
51
# File 'lib/parlour/rbs_generator/method_signature.rb', line 49

def parameters
  @parameters
end

#return_typeTypes::TypeLike? (readonly)

What this method returns. Passing nil denotes a void return.

Returns:



54
55
56
# File 'lib/parlour/rbs_generator/method_signature.rb', line 54

def return_type
  @return_type
end

#type_parametersArray<Symbol> (readonly)

This method’s type parameters.

Returns:

  • (Array<Symbol>)


64
65
66
# File 'lib/parlour/rbs_generator/method_signature.rb', line 64

def type_parameters
  @type_parameters
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another method signature.

Parameters:

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/parlour/rbs_generator/method_signature.rb', line 38

def ==(other)
  MethodSignature === other &&
    parameters      == other.parameters &&
    return_type     == other.return_type &&
    block           == other.block &&
    type_parameters == other.type_parameters
end

#generate_rbs(options) ⇒ Array<String>

Generates the RBS string for this signature.

Parameters:

  • options (Options)

    The formatting options to use.

Returns:

  • (Array<String>)

    The RBS string, formatted as specified.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/parlour/rbs_generator/method_signature.rb', line 71

def generate_rbs(options)
  block_type = @block&.generate_rbs(options)

  rbs_params = parameters.reject { |x| x.kind == :block }.map(&:to_rbs_param)
  rbs_return_type = String === @return_type ? @return_type : @return_type&.generate_rbs

  generated_params = parameters.length >= options.break_params \
    ? ["("] +
      (
        parameters.empty? ? [] : rbs_params.map.with_index do |x, i|
          options.indented(
            1,
            # Don't include the comma on the last parameter.
            parameters.length == i + 1 ? "#{x}" : "#{x},"
          )
        end
      ) +
      [")"]

    : ["(#{rbs_params.join(', ')})"]

  generated_params[0] = "#{
    type_parameters.any? ? "[#{type_parameters.join(', ')}] " : '' 
  }" + T.must(generated_params[0])

  generated_params[-1] = T.must(generated_params[-1]) + "#{
    (block_type && block_type.first != 'untyped') ? " #{block_type.first}" : '' # TODO: doesn't support multi-line block types
  } -> #{rbs_return_type || 'void'}"

  generated_params
end