From 27684927ea9fc1acad1ac90b43271ce5f09e5c5d Mon Sep 17 00:00:00 2001 From: Giacomo GK Date: Sat, 27 Dec 2025 20:45:16 +0900 Subject: [PATCH] fix: remove return types for methods without parentheses --- lib/t_ruby/compiler.rb | 7 ++++++- spec/t_ruby/type_erasure_spec.rb | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/t_ruby/compiler.rb b/lib/t_ruby/compiler.rb index c514ce2..0c285ad 100644 --- a/lib/t_ruby/compiler.rb +++ b/lib/t_ruby/compiler.rb @@ -547,11 +547,16 @@ def clean_param(param) def erase_return_types(source) result = source.dup - # Remove return type: ): Type or ): Type etc. + # Remove return type after parentheses: ): Type or ): Type 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 diff --git a/spec/t_ruby/type_erasure_spec.rb b/spec/t_ruby/type_erasure_spec.rb index a825b9f..3448f69 100644 --- a/spec/t_ruby/type_erasure_spec.rb +++ b/spec/t_ruby/type_erasure_spec.rb @@ -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 " \