class Rbs2ts::Converter::Members::MethodDefinition

Public Class Methods

new(member) click to toggle source
Calls superclass method Rbs2ts::Converter::Members::Base::new
# File lib/rbs2ts/converter/members.rb, line 47
def initialize(member)
  super
  @args_count = 0
end

Public Instance Methods

arg_name(arg) click to toggle source
# File lib/rbs2ts/converter/members.rb, line 66
def arg_name(arg)
  name = arg.name.nil? ? next_default_arg_name : arg.name.to_s
  CaseTransform.camel_lower(name)
end
args_to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 56
def args_to_ts
  [
    *required_positional_to_ts,
    *optional_positional_to_ts,
    *rest_positionals_to_ts,
    *trailing_positionals_to_ts,
    *keyword_args_to_ts
  ].join(", ")
end
has_rest_after_arguments() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 138
def has_rest_after_arguments
  method_type.trailing_positionals.present? || 
  method_type.required_keywords.present? ||
  method_type.optional_keywords.present?
end
keyword_args_to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 104
def keyword_args_to_ts
  required_keywords_ts = method_type.required_keywords.map {|key, value|
    "#{CaseTransform.camel_lower(key.to_s)}: #{Converter::Types::Resolver.to_ts(value.type)}"
  }
  optional_keywords_ts = method_type.optional_keywords.map {|key, value|
    "#{CaseTransform.camel_lower(key.to_s)}?: #{Converter::Types::Resolver.to_ts(value.type)}"
  }
  rest_keywords_ts = method_type.rest_keywords.nil? ? [] : ["[key: string]: unknown;"]

  ts_array = [
    *required_keywords_ts,
    *optional_keywords_ts,
    *rest_keywords_ts
  ]

  return [] if ts_array.empty?

  ts = ts_array.join(', ')

  ["#{next_default_arg_name}#{optional_ts_code}: { #{ts} }"]
end
method_type() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 126
def method_type
  member.types.first.type
end
next_default_arg_name() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 71
def next_default_arg_name
  @args_count = @args_count + 1
  "arg#{@args_count.to_s}"
end
optional_positional_to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 82
def optional_positional_to_ts
  method_type.optional_positionals.map {|arg|
    "#{arg_name(arg)}?: #{Converter::Types::Resolver.to_ts(arg.type)}"
  }
end
optional_ts_code() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 134
def optional_ts_code
  method_type.optional_positionals.present? ? '?' : ''
end
required_positional_to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 76
def required_positional_to_ts
  method_type.required_positionals.map {|arg|
    "#{arg_name(arg)}: #{Converter::Types::Resolver.to_ts(arg.type)}"
  }
end
rest_positionals_to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 88
def rest_positionals_to_ts
  arg = method_type.rest_positionals
  
  return [] if arg.nil?

  has_rest_after_arguments ?
    ["#{arg_name(arg)}#{optional_ts_code}: #{Converter::Types::Resolver.to_ts(arg.type)}[]"] :
    ["...#{arg_name(arg)}#{optional_ts_code}: #{Converter::Types::Resolver.to_ts(arg.type)}[]"]
end
return_type_to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 130
def return_type_to_ts
  Converter::Types::Resolver.to_ts(method_type.return_type)
end
to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 52
def to_ts
  "#{name}(#{args_to_ts}): #{return_type_to_ts};"
end
trailing_positionals_to_ts() click to toggle source
# File lib/rbs2ts/converter/members.rb, line 98
def trailing_positionals_to_ts
  method_type.trailing_positionals.map {|arg|
    "#{arg_name(arg)}#{optional_ts_code}: #{Converter::Types::Resolver.to_ts(arg.type)}"
  }
end