From 43384df0d6478c0277fba2d5b60c07bfe77e62b3 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Sun, 24 Dec 2023 11:21:34 -0600 Subject: [PATCH] Remove extra whitespace in method signature params RDoc includes extra whitespace in a method's parameter list when the the method's `def` statement is spread across multiple lines. For example, a `def` like: ```ruby def define_attribute( name, cast_type, default: NO_DEFAULT_PROVIDED, user_provided_default: true ) # ... end ``` Would produce a parameter list like: ``` ( name, cast_type, default: NO_DEFAULT_PROVIDED, user_provided_default: true ) ``` This commit monkey-patches `RDoc::AnyMethod#params` to remove the extra whitespace within the parentheses: ``` (name, cast_type, default: NO_DEFAULT_PROVIDED, user_provided_default: true) ``` This is implemented as a monkey patch so that it fixes `params` for rendered method docs as well as search index entries. --- lib/sdoc/rdoc_monkey_patches.rb | 7 +++++++ spec/rdoc_monkey_patches_spec.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/sdoc/rdoc_monkey_patches.rb b/lib/sdoc/rdoc_monkey_patches.rb index b57486ec..e2269615 100644 --- a/lib/sdoc/rdoc_monkey_patches.rb +++ b/lib/sdoc/rdoc_monkey_patches.rb @@ -9,6 +9,13 @@ def path end) +RDoc::AnyMethod.prepend(Module.new do + def params + super&.sub(/\A\(\s+/, "(")&.sub(/\s+\)\z/, ")") + end +end) + + RDoc::Markup::ToHtmlCrossref.prepend(Module.new do def cross_reference(name, text = nil, code = true) if text diff --git a/spec/rdoc_monkey_patches_spec.rb b/spec/rdoc_monkey_patches_spec.rb index e112e2e9..6eed23db 100644 --- a/spec/rdoc_monkey_patches_spec.rb +++ b/spec/rdoc_monkey_patches_spec.rb @@ -12,6 +12,23 @@ end end + describe RDoc::AnyMethod do + it "omits extra whitespace in #params" do + rdoc_method = rdoc_top_level_for(<<~RUBY).find_module_named("Foo").find_method("bar", false) + module Foo + def bar( + x, + y, + z + ) + end + end + RUBY + + _(rdoc_method.params).must_equal "(x, y, z)" + end + end + describe RDoc::Markup::ToHtmlCrossref do it "prevents unintentional ref links" do description = rdoc_top_level_for(<<~RUBY).find_module_named("CoolApp").description