Skip to content

Commit ecaa305

Browse files
author
Giacomo Guiulfo
authored
fix: remove return types for methods without parentheses (#24)
1 parent 228c692 commit ecaa305

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/t_ruby/compiler.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,16 @@ def clean_param(param)
547547
def erase_return_types(source)
548548
result = source.dup
549549

550-
# Remove return type: ): Type or ): Type<Foo> etc.
550+
# Remove return type after parentheses: ): Type or ): Type<Foo> etc.
551551
result.gsub!(/\)\s*:\s*[^\n]+?(?=\s*$)/m) do |_match|
552552
")"
553553
end
554554

555+
# Remove return type for methods without parentheses: def method_name: Type
556+
result.gsub!(/^(\s*#{TRuby::VISIBILITY_PATTERN}def\s+#{TRuby::METHOD_NAME_PATTERN})\s*:\s*[^\n]+?(?=\s*$)/m) do |_match|
557+
::Regexp.last_match(1)
558+
end
559+
555560
result
556561
end
557562
end

spec/t_ruby/type_erasure_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ def greet(name: String): String
157157
expect(result).to include("def hello()")
158158
end
159159

160+
it "handles functions without parentheses and with return type" do
161+
source = "def hello: String\n 'hello'\nend"
162+
eraser = TRuby::TypeErasure.new(source)
163+
164+
result = eraser.erase
165+
expect(result).to include("def hello")
166+
expect(result).not_to include(": String")
167+
end
168+
160169
it "handles nested structures" do
161170
source = "class Greeter" + "\n " \
162171
"def greet(name: String): String" + "\n " \

0 commit comments

Comments
 (0)