Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/spoom/rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ def initialize(string, location)
end
end

class Annotation < Comment; end
class Annotation < Comment
#: () -> bool
def abstract?
@string == "@abstract"
end
end

class Signature < Comment
# Locations of the `#|` continuation comment lines that make up a multiline signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def initialize(
end #: Integer?

@overloads_strategy = options.overloads_strategy #: Symbol
@translate_abstract_methods = options.translate_abstract_methods #: bool
@type_translator = RBI::RBS::TypeTranslator.new #: RBI::RBS::TypeTranslator
end

Expand Down Expand Up @@ -141,6 +142,7 @@ def visit_attr(node)
def rewrite_def(def_node, comments)
return if comments.empty?
return if comments.signatures.empty?
return if !@translate_abstract_methods && comments.method_annotations.any?(&:abstract?)

signatures = apply_overloads_strategy(
comments.signatures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ class Options
#: BaseRBIFormat
attr_reader :output_format

#: bool
attr_reader :translate_abstract_methods

#: (
#| ?overloads_strategy: Symbol,
#| ?output_format: BaseRBIFormat,
#| ?translate_abstract_methods: bool,
#| ) -> void
def initialize(
overloads_strategy: :translate_all,
output_format: HumanReadableRBIFormat.default
output_format: HumanReadableRBIFormat.default,
translate_abstract_methods: true
)
unless ALLOWED_OVERLOAD_STRATEGIES.include?(overloads_strategy)
raise ArgumentError, "Unknown overloads_strategy: #{overloads_strategy.inspect}. " \
Expand All @@ -62,6 +67,7 @@ def initialize(

@overloads_strategy = overloads_strategy
@output_format = output_format
@translate_abstract_methods = translate_abstract_methods

freeze
end
Expand Down
14 changes: 11 additions & 3 deletions rbi/spoom.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -2578,7 +2578,11 @@ end
module Spoom::PrismTypes; end
Spoom::PrismTypes::AnyScopeNode = T.type_alias { T.any(::Prism::ClassNode, ::Prism::ModuleNode, ::Prism::SingletonClassNode) }
module Spoom::RBS; end
class Spoom::RBS::Annotation < ::Spoom::RBS::Comment; end

class Spoom::RBS::Annotation < ::Spoom::RBS::Comment
sig { returns(T::Boolean) }
def abstract?; end
end

class Spoom::RBS::Comment
sig { params(string: ::String, location: ::Prism::Location).void }
Expand Down Expand Up @@ -3209,17 +3213,21 @@ class Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::Options
sig do
params(
overloads_strategy: ::Symbol,
output_format: ::Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::BaseRBIFormat
output_format: ::Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::BaseRBIFormat,
translate_abstract_methods: T::Boolean
).void
end
def initialize(overloads_strategy: T.unsafe(nil), output_format: T.unsafe(nil)); end
def initialize(overloads_strategy: T.unsafe(nil), output_format: T.unsafe(nil), translate_abstract_methods: T.unsafe(nil)); end

sig { returns(::Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::BaseRBIFormat) }
def output_format; end

sig { returns(::Symbol) }
def overloads_strategy; end

sig { returns(T::Boolean) }
def translate_abstract_methods; end

class << self
sig { returns(::Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::Options) }
def default; end
Expand Down
82 changes: 78 additions & 4 deletions test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,66 @@ def bar; end
)
end

def test_translate_to_rbi_skipping_abstract_methods
assert_rewrites_rbs(
from: <<~RUBY,
# @abstract
class Foo
# @abstract
#: -> String
def bar; end
end

# @abstract
module Baz
# @abstract
#: -> String
def qux; end
end
RUBY

to_pretty_format_for_humans: <<~RUBY,
class Foo
extend T::Helpers

abstract!

# @abstract
#: -> String
def bar; end
end

module Baz
extend T::Helpers

abstract!

# @abstract
#: -> String
def qux; end
end
RUBY

to_line_matched_format_for_machines: <<~RUBY,
# RBS_REWRITTEN_ANNOTATION: @abstract

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not rewritten.

The other such comment is RBS_IGNORED_UNKNOWN_ANNOTATION, which isn't right here (this is known, but intentionally ignore).

Perhaps

Suggested change
# RBS_REWRITTEN_ANNOTATION: @abstract
# RBS_IGNORED_ANNOTATION: @abstract

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the @abstract on the class/module though. The one on methods don't get any prefix at all.

Do you think we should still add one?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I meant to comment this on the method-level annotation.

I didn't implement the RBS_REWRITTEN_ANNOTATION comment to method-level annotations, that's just an oversight on my part. Could be nice to add it (not in this PR, and not urgent).

Those comments only exist for debuggability, to help with investigating any potential issues with the output.

class Foo; extend T::Helpers; abstract!
# @abstract
#: -> String
def bar; end
end

# RBS_REWRITTEN_ANNOTATION: @abstract
module Baz; extend T::Helpers; abstract!
# @abstract
#: -> String
def qux; end
end
RUBY

translate_abstract_methods: false,
)
end

def test_translate_to_rbi_method_sigs_without_runtime
assert_rewrites_rbs(
from: <<~RUBY,
Expand Down Expand Up @@ -1280,13 +1340,23 @@ def foo; end

private

#: (String, ?max_line_length: Integer?, ?overloads_strategy: Symbol) -> String
def rbs_comments_to_sorbet_sigs(ruby_contents, max_line_length: nil, overloads_strategy: :translate_all)
#: (String,
#| ?max_line_length: Integer?,
#| ?overloads_strategy: Symbol,
#| ?translate_abstract_methods: bool
#| ) -> String
def rbs_comments_to_sorbet_sigs(
ruby_contents,
max_line_length: nil,
overloads_strategy: :translate_all,
translate_abstract_methods: true
)
RBSCommentsToSorbetSigs::HumanReadableTranslator.new(
ruby_contents,
file: "test.rb",
options: RBSCommentsToSorbetSigs::Options.new(
overloads_strategy:,
translate_abstract_methods:,
output_format: RBSCommentsToSorbetSigs::HumanReadableRBIFormat.new(
max_line_length:,
),
Expand All @@ -1299,14 +1369,16 @@ def rbs_comments_to_sorbet_sigs(ruby_contents, max_line_length: nil, overloads_s
#| to_pretty_format_for_humans: String,
#| to_line_matched_format_for_machines: String | Symbol,
#| ?max_line_length: Integer?,
#| ?overloads_strategy: Symbol
#| ?overloads_strategy: Symbol,
#| ?translate_abstract_methods: bool
#| ) -> void
def assert_rewrites_rbs(
from:,
to_pretty_format_for_humans:,
to_line_matched_format_for_machines:,
max_line_length: nil,
overloads_strategy: :translate_all
overloads_strategy: :translate_all,
translate_abstract_methods: true
)
source_with_rbs = from
expected_pretty_format = to_pretty_format_for_humans
Expand All @@ -1317,6 +1389,7 @@ def assert_rewrites_rbs(
source_with_rbs,
max_line_length:,
overloads_strategy:,
translate_abstract_methods:,
)

assert_equal(expected_pretty_format, rewritten_output)
Expand Down Expand Up @@ -1347,6 +1420,7 @@ def assert_rewrites_rbs(
file: "test.rb",
options: RBSCommentsToSorbetSigs::Options.new(
overloads_strategy:,
translate_abstract_methods:,
output_format: RBSCommentsToSorbetSigs::LineMatchedRBIFormat.default,
),
).rewrite
Expand Down
Loading