From 7c21626f98dbb71aa7e21f7f1d56ba8cd5a94ebb Mon Sep 17 00:00:00 2001 From: "Joey@macstudio" <4296411@qq.com> Date: Wed, 15 Jul 2026 03:47:06 +0800 Subject: [PATCH] Fix HTML generation for RBS constants without values --- CHANGELOG.md | 1 + lib/yard/templates/helpers/method_helper.rb | 3 +++ spec/templates/helpers/method_helper_spec.rb | 6 ++++++ spec/templates/module_spec.rb | 13 +++++++++++++ 4 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e6448ca9..1c4464db3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Add compatibility with RDoc 8 - Fix TypesExplainer parsing of types following Hash collections (#1688) +- Fix HTML generation for RBS constants without source values (#1686) # [0.9.44] - May 25th, 2026 diff --git a/lib/yard/templates/helpers/method_helper.rb b/lib/yard/templates/helpers/method_helper.rb index 5e61bd180..16e5f30e5 100644 --- a/lib/yard/templates/helpers/method_helper.rb +++ b/lib/yard/templates/helpers/method_helper.rb @@ -64,8 +64,11 @@ def format_code(object, _show_lines = false) end.join("\n") end + # @param [String, nil] value the source code of a constant value # @return [String] formats source code of a constant value def format_constant(value) + return "" if value.nil? + # last can return nil, so default to empty string sp = value.split("\n").last || "" sp = sp[/^(\s+)/, 1] diff --git a/spec/templates/helpers/method_helper_spec.rb b/spec/templates/helpers/method_helper_spec.rb index 108a8609f..9060fc7d8 100644 --- a/spec/templates/helpers/method_helper_spec.rb +++ b/spec/templates/helpers/method_helper_spec.rb @@ -115,5 +115,11 @@ class TestFmtConst expect(format_constant("")).to eq "" end end + + context "when nil is passed as param" do + it "returns an empty string" do + expect(format_constant(nil)).to eq "" + end + end end end diff --git a/spec/templates/module_spec.rb b/spec/templates/module_spec.rb index 16dbd095d..1ee82af5b 100644 --- a/spec/templates/module_spec.rb +++ b/spec/templates/module_spec.rb @@ -201,6 +201,19 @@ module A html_equals(Registry.at('A').format(html_options), :module005) end + it "renders RBS constants without source values in html" do + Registry.clear + YARD::Parser::SourceParser.parse_string <<-'RBS', :rbs +class Foo + BAR: untyped +end + RBS + + html = Registry.at('Foo').format(html_options) + expect(html).to include('id="BAR-constant"') + expect(html).to include('
') + end + it "shows inherited methods from matching groups in the group section, not in flat inherited section" do Registry.clear YARD.parse_string <<-'eof'