Skip to content

Commit 43384df

Browse files
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.
1 parent b1153b4 commit 43384df

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/sdoc/rdoc_monkey_patches.rb

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ def path
99
end)
1010

1111

12+
RDoc::AnyMethod.prepend(Module.new do
13+
def params
14+
super&.sub(/\A\(\s+/, "(")&.sub(/\s+\)\z/, ")")
15+
end
16+
end)
17+
18+
1219
RDoc::Markup::ToHtmlCrossref.prepend(Module.new do
1320
def cross_reference(name, text = nil, code = true)
1421
if text

spec/rdoc_monkey_patches_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
end
1313
end
1414

15+
describe RDoc::AnyMethod do
16+
it "omits extra whitespace in #params" do
17+
rdoc_method = rdoc_top_level_for(<<~RUBY).find_module_named("Foo").find_method("bar", false)
18+
module Foo
19+
def bar(
20+
x,
21+
y,
22+
z
23+
)
24+
end
25+
end
26+
RUBY
27+
28+
_(rdoc_method.params).must_equal "(x, y, z)"
29+
end
30+
end
31+
1532
describe RDoc::Markup::ToHtmlCrossref do
1633
it "prevents unintentional ref links" do
1734
description = rdoc_top_level_for(<<~RUBY).find_module_named("CoolApp").description

0 commit comments

Comments
 (0)