Skip to content

Commit d1171aa

Browse files
committed
Update translation of RBS abstract methods to conditionally call super
1 parent ae2e1ae commit d1171aa

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

lib/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def visit_singleton_class_node(node)
5959
visit_scope(node) { super }
6060
end
6161

62+
ABSTRACT_METHOD_BODY = "defined?(super) ? super : raise(NotImplementedError, \"Abstract method called\")"
63+
6264
# @override
6365
#: (Prism::DefNode) -> void
6466
def visit_def_node(node)
@@ -87,9 +89,9 @@ def visit_def_node(node)
8789
node.location.end_offset - 1,
8890
if node.name.end_with?("=")
8991
indent = " " * node.location.start_column
90-
"\n#{indent} raise NotImplementedError, \"Abstract method called\"\n#{indent}end"
92+
"\n#{indent} #{ABSTRACT_METHOD_BODY}\n#{indent}end"
9193
else
92-
" = raise NotImplementedError, \"Abstract method called\""
94+
" = #{ABSTRACT_METHOD_BODY}"
9395
end,
9496
)
9597
end

test/spoom/cli/srb/sigs_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def foo; end
342342
class A
343343
# @abstract
344344
#: -> void
345-
def foo = raise NotImplementedError, "Abstract method called"
345+
def foo = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
346346
end
347347
RB
348348
end

test/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,28 @@ def bar=(x)
102102
class Foo
103103
# @abstract
104104
#: -> void
105-
def foo = raise NotImplementedError, "Abstract method called"
105+
def foo = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
106106
107107
class Bar
108108
# @abstract
109109
#: -> void
110-
def bar = raise NotImplementedError, "Abstract method called"
110+
def bar = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
111111
end
112112
113113
# @abstract
114114
#: (Integer x) -> void
115-
def baz(x) = raise NotImplementedError, "Abstract method called"
115+
def baz(x) = defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
116116
117117
# @abstract
118118
#: (Integer x) -> void
119119
def foo=(x)
120-
raise NotImplementedError, "Abstract method called"
120+
defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
121121
end
122122
123123
# @abstract
124124
#: (Integer x) -> void
125125
def bar=(x)
126-
raise NotImplementedError, "Abstract method called"
126+
defined?(super) ? super : raise(NotImplementedError, "Abstract method called")
127127
end
128128
end
129129
RBS

0 commit comments

Comments
 (0)