Skip to content

Commit a17f67e

Browse files
authored
Merge pull request #1415 from xing/jamuc/handle_empty_string_method_helper
Handle empty string param in method helper
2 parents 1046981 + 17f0b49 commit a17f67e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/yard/templates/helpers/method_helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def format_code(object, _show_lines = false)
6666

6767
# @return [String] formats source code of a constant value
6868
def format_constant(value)
69-
sp = value.split("\n").last[/^(\s+)/, 1]
69+
# last can return nil, so default to empty string
70+
sp = value.split("\n").last || ""
71+
sp = sp[/^(\s+)/, 1]
7072
num = sp ? sp.size : 0
7173
html_syntax_highlight value.gsub(/^\s{#{num}}/, '')
7274
end

spec/templates/helpers/method_helper_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,17 @@ class TestFmtConst
103103
expect(format_constant(bar)).to eq ':BAR'
104104
expect(format_constant(baz)).to eq ":'B+z'"
105105
end
106+
107+
context "when an empty string is passed as param" do
108+
it "returns an empty string" do
109+
# html_syntax_highlight will be called by format_constant
110+
# and in turn will enquire for options.highlight
111+
expect(self).to receive(:options).once.and_return(
112+
Options.new.update(:highlight => false)
113+
)
114+
115+
expect(format_constant("")).to eq ""
116+
end
117+
end
106118
end
107119
end

0 commit comments

Comments
 (0)