From 4cd0e916190f360afddf7d7570192d2e85234106 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 16 Jun 2026 18:35:16 -0400 Subject: [PATCH] Add `Options` object Co-authored-by: Julia Boutin --- lib/rbi/rbs/method_type_translator.rb | 27 ++++++++++++++++++++++++--- lib/rbi/rbs/type_translator.rb | 8 ++++++++ rbi/rbi.rbi | 26 ++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/lib/rbi/rbs/method_type_translator.rb b/lib/rbi/rbs/method_type_translator.rb index 64d8a845..308c5f57 100644 --- a/lib/rbi/rbs/method_type_translator.rb +++ b/lib/rbi/rbs/method_type_translator.rb @@ -15,14 +15,35 @@ def translate(method, type) end end + # @abstract + class Options + #: (?erase_generic_types: bool) -> void + def initialize(erase_generic_types: false) + @erase_generic_types = erase_generic_types + end + + #: -> bool + def erase_generic_types? = @erase_generic_types + end + + class HumanReadableOptions < Options + @default = new.freeze #: HumanReadableOptions + class << self + #: HumanReadableOptions + attr_reader :default + end + end + #: Sig attr_reader :result - #: (Method) -> void - def initialize(method) + #: (Method, ?options: Options) -> void + def initialize(method, options: HumanReadableOptions.default) @method = method + @options = options + @result = Sig.new #: Sig - @type_translator = TypeTranslator.new #: TypeTranslator + @type_translator = TypeTranslator.new(options:) #: TypeTranslator end #: (::RBS::MethodType) -> void diff --git a/lib/rbi/rbs/type_translator.rb b/lib/rbi/rbs/type_translator.rb index 4afb88c3..7c6e90ad 100644 --- a/lib/rbi/rbs/type_translator.rb +++ b/lib/rbi/rbs/type_translator.rb @@ -35,6 +35,14 @@ def translate(type) end end + Options = MethodTypeTranslator::Options + HumanReadableOptions = MethodTypeTranslator::HumanReadableOptions + + #: (?options: Options) -> void + def initialize(options: HumanReadableOptions.default) + @options = options + end + #: (rbs_type) -> Type def translate(type) case type diff --git a/rbi/rbi.rbi b/rbi/rbi.rbi index 8a3a3c01..22e1ed05 100644 --- a/rbi/rbi.rbi +++ b/rbi/rbi.rbi @@ -1386,8 +1386,8 @@ RBI::Public::DEFAULT = T.let(T.unsafe(nil), RBI::Public) module RBI::RBS; end class RBI::RBS::MethodTypeTranslator - sig { params(method: ::RBI::Method).void } - def initialize(method); end + sig { params(method: ::RBI::Method, options: ::RBI::RBS::MethodTypeTranslator::Options).void } + def initialize(method, options: T.unsafe(nil)); end sig { returns(::RBI::Sig) } def result; end @@ -1417,7 +1417,27 @@ end class RBI::RBS::MethodTypeTranslator::Error < ::RBI::Error; end +class RBI::RBS::MethodTypeTranslator::HumanReadableOptions < ::RBI::RBS::MethodTypeTranslator::Options + class << self + sig { returns(::RBI::RBS::MethodTypeTranslator::HumanReadableOptions) } + def default; end + end +end + +class RBI::RBS::MethodTypeTranslator::Options + abstract! + + sig { params(erase_generic_types: T::Boolean).void } + def initialize(erase_generic_types: T.unsafe(nil)); end + + sig { returns(T::Boolean) } + def erase_generic_types?; end +end + class RBI::RBS::TypeTranslator + sig { params(options: ::RBI::RBS::MethodTypeTranslator::Options).void } + def initialize(options: T.unsafe(nil)); end + sig do params( type: T.any(::RBS::Types::Alias, ::RBS::Types::Bases::Any, ::RBS::Types::Bases::Bool, ::RBS::Types::Bases::Bottom, ::RBS::Types::Bases::Class, ::RBS::Types::Bases::Instance, ::RBS::Types::Bases::Nil, ::RBS::Types::Bases::Self, ::RBS::Types::Bases::Top, ::RBS::Types::Bases::Void, ::RBS::Types::ClassInstance, ::RBS::Types::ClassSingleton, ::RBS::Types::Function, ::RBS::Types::Interface, ::RBS::Types::Intersection, ::RBS::Types::Literal, ::RBS::Types::Optional, ::RBS::Types::Proc, ::RBS::Types::Record, ::RBS::Types::Tuple, ::RBS::Types::Union, ::RBS::Types::UntypedFunction, ::RBS::Types::Variable) @@ -1449,6 +1469,8 @@ class RBI::RBS::TypeTranslator end end +RBI::RBS::TypeTranslator::HumanReadableOptions = RBI::RBS::MethodTypeTranslator::HumanReadableOptions +RBI::RBS::TypeTranslator::Options = RBI::RBS::MethodTypeTranslator::Options RBI::RBS::TypeTranslator::RbsType = T.type_alias { T.any(::RBS::Types::Alias, ::RBS::Types::Bases::Any, ::RBS::Types::Bases::Bool, ::RBS::Types::Bases::Bottom, ::RBS::Types::Bases::Class, ::RBS::Types::Bases::Instance, ::RBS::Types::Bases::Nil, ::RBS::Types::Bases::Self, ::RBS::Types::Bases::Top, ::RBS::Types::Bases::Void, ::RBS::Types::ClassInstance, ::RBS::Types::ClassSingleton, ::RBS::Types::Function, ::RBS::Types::Interface, ::RBS::Types::Intersection, ::RBS::Types::Literal, ::RBS::Types::Optional, ::RBS::Types::Proc, ::RBS::Types::Record, ::RBS::Types::Tuple, ::RBS::Types::Union, ::RBS::Types::UntypedFunction, ::RBS::Types::Variable) } class RBI::RBSComment < ::RBI::Comment