Skip to content
Open
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
6 changes: 4 additions & 2 deletions lib/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def visit_singleton_class_node(node)
visit_scope(node) { super }
end

ABSTRACT_METHOD_BODY = "defined?(super) ? super : raise(NotImplementedError, \"Abstract method called\")"
Copy link
Contributor

Choose a reason for hiding this comment

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

As a follow up, it would be nice to provide the replacement through the translator options:

# does not rewrite abstract methods
SorbetSigsToRBSComments.new(
  translate_abstract_methods: nil
)

# old behavior, uses only raise
SorbetSigsToRBSComments.new(
  translate_abstract_methods: "raise NotImplementedError, \"Abstract method called\""
)

# new behavior, uses defined super
SorbetSigsToRBSComments.new(
  translate_abstract_methods: "defined?(super) ? super : raise(NotImplementedError, \"Abstract method called\")"
)


# @override
#: (Prism::DefNode) -> void
def visit_def_node(node)
Expand Down Expand Up @@ -87,9 +89,9 @@ def visit_def_node(node)
node.location.end_offset - 1,
if node.name.end_with?("=")
indent = " " * node.location.start_column
"\n#{indent} raise NotImplementedError, \"Abstract method called\"\n#{indent}end"
"\n#{indent} #{ABSTRACT_METHOD_BODY}\n#{indent}end"
else
" = raise NotImplementedError, \"Abstract method called\""
" = #{ABSTRACT_METHOD_BODY}"
end,
)
end
Expand Down
2 changes: 2 additions & 0 deletions rbi/spoom.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,8 @@ class Spoom::Sorbet::Translate::SorbetSigsToRBSComments < ::Spoom::Sorbet::Trans
def visit_sig(node); end
end

Spoom::Sorbet::Translate::SorbetSigsToRBSComments::ABSTRACT_METHOD_BODY = T.let(T.unsafe(nil), String)

class Spoom::Sorbet::Translate::StripSorbetSigs < ::Spoom::Sorbet::Translate::Translator
sig { override.params(node: ::Prism::CallNode).void }
def visit_call_node(node); end
Expand Down
2 changes: 1 addition & 1 deletion test/spoom/cli/srb/sigs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def foo; end
class A
# @abstract
#: -> void
def foo = raise NotImplementedError, "Abstract method called"
def foo = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
end
RB
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,28 @@ def bar=(x)
class Foo
# @abstract
#: -> void
def foo = raise NotImplementedError, "Abstract method called"
def foo = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")

class Bar
# @abstract
#: -> void
def bar = raise NotImplementedError, "Abstract method called"
def bar = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
end

# @abstract
#: (Integer x) -> void
def baz(x) = raise NotImplementedError, "Abstract method called"
def baz(x) = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")

# @abstract
#: (Integer x) -> void
def foo=(x)
raise NotImplementedError, "Abstract method called"
defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
end

# @abstract
#: (Integer x) -> void
def bar=(x)
raise NotImplementedError, "Abstract method called"
defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
end
end
RBS
Expand Down