class Parlour::RbsGenerator::MethodSignature

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

Attributes

block[R]

The block this method accepts. @return [Block, nil]

parameters[R]

An array of {Parameter} instances representing this method's parameters. @return [Array<Parameter>]

return_type[R]

What this method returns. Passing nil denotes a void return. @return [Types::TypeLike, nil]

type_parameters[R]

This method's type parameters. @return [Array<Symbol>]

Public Class Methods

new(parameters, return_type = nil, block: nil, type_parameters: nil) click to toggle source

Creates a new method signature.

@param parameters [Array<Parameter>] An array of {Parameter} instances representing this

method's parameters.

@param return_type [Types::TypeLike, nil] What this method returns. Passing nil denotes a void return. @param block [Types::TypeLike, nil] The block this method accepts. Passing nil denotes none. @param type_parameters [Array<Symbol>, nil] This method's type parameters. @return [void]

# 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

Public Instance Methods

==(other) click to toggle source

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

@param other [Object] The other instance. If this is not a {MethodSignature} (or a

subclass of it), this will always return false.

@return [Boolean]

# 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) click to toggle source

Generates the RBS string for this signature.

@param options [Options] The formatting options to use. @return [Array<String>] The RBS string, formatted as specified.

# 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