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/t_ruby/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,16 @@ def clean_param(param)
def erase_return_types(source)
result = source.dup

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

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

result
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/t_ruby/type_erasure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ def greet(name: String): String
expect(result).to include("def hello()")
end

it "handles functions without parentheses and with return type" do
source = "def hello: String\n 'hello'\nend"
eraser = TRuby::TypeErasure.new(source)

result = eraser.erase
expect(result).to include("def hello")
expect(result).not_to include(": String")
end

it "handles nested structures" do
source = "class Greeter" + "\n " \
"def greet(name: String): String" + "\n " \
Expand Down