Skip to content

Commit 42a643e

Browse files
committed
(PDOC-283) Fix namespaced symbols
Previously in Yard 0.9.19 dyna_symbols (symbols represented as strings e.g. :'something') were converted to their unqualified symbol name e.g. :something. However YARD treated this as a bug and in Yard 0.19.20 dynasymbols are now their original source. This commit modifies the base ruby handler to always convert dyna_symbols to their plain name, as Puppet uses this for actual documentation and Puppet Code. For example the fully qualified function 'foo::bar' will appear in the ruby function definition as `.. :'foo::bar'` however in the Puppet documentation the name should appears as `foo::bar`. This commit also adds tests for this scenario as there were no existing tests for this.
1 parent 8b9e267 commit 42a643e

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

lib/puppet-strings/yard/handlers/ruby/base.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ def node_as_string(node)
1818
when :label
1919
node.source[0..-2]
2020
when :dyna_symbol
21-
node.source
21+
# YARD 0.9.20 changed how dyna_symbols are represented
22+
# https://github.com/lsegal/yard/commit/225ded9ef38c6d2be5a3b0fc7effbc7d6644768d
23+
if yard_version >= Gem::Version.new('0.9.20')
24+
node.source[2..-2]
25+
else
26+
node.source
27+
end
2228
when :string_literal
2329
content = node.jump(:tstring_content)
2430
return content.source if content != node
@@ -46,4 +52,9 @@ def get_name(statementobject, statementtype)
4652
name
4753
end
4854

55+
private
56+
57+
def yard_version
58+
@yard_version ||= Gem::Version.new(YARD::VERSION)
59+
end
4960
end

spec/unit/puppet-strings/yard/handlers/ruby/function_handler_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,23 @@ def other(b)
567567
end
568568
end
569569

570+
describe 'parsing a function with a namespaced name' do
571+
let(:source) { <<-SOURCE
572+
# An example 4.x function.
573+
Puppet::Functions.create_function(:'foo::bar::baz') do
574+
# @return [Undef]
575+
dispatch :foo do
576+
end
577+
end
578+
SOURCE
579+
}
580+
581+
it 'should output the name correctly as a symbol' do
582+
expect(subject.size).to eq(1)
583+
expect(subject.first.name).to eq(:'foo::bar::baz')
584+
end
585+
end
586+
570587
describe 'parsing a function with a missing parameter' do
571588
let(:source) { <<-SOURCE
572589
# An example 4.x function.

spec/unit/puppet-strings/yard/handlers/ruby/provider_handler_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@
106106
end
107107
end
108108

109+
describe 'parsing a provider definition with a string based name' do
110+
let(:source) { <<-SOURCE
111+
Puppet::Type.type(:'custom').provide :'linux' do
112+
desc 'An example provider on Linux.'
113+
end
114+
SOURCE
115+
}
116+
117+
it 'should register a provider object' do
118+
expect(subject.size).to eq(1)
119+
object = subject.first
120+
expect(object).to be_a(PuppetStrings::Yard::CodeObjects::Provider)
121+
expect(object.namespace).to eq(PuppetStrings::Yard::CodeObjects::Providers.instance('custom'))
122+
expect(object.name).to eq(:linux)
123+
expect(object.type_name).to eq('custom')
124+
expect(object.docstring).to eq('An example provider on Linux.')
125+
end
126+
end
127+
109128
describe 'parsing a provider with a summary' do
110129
context 'when the summary has fewer than 140 characters' do
111130
let(:source) { <<-SOURCE

spec/unit/puppet-strings/yard/handlers/ruby/type_handler_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@
223223
end
224224
end
225225

226+
describe 'parsing a valid type with string based name' do
227+
let(:source) { <<-SOURCE
228+
Puppet::Type.newtype(:'database') do
229+
desc 'An example database server resource type.'
230+
ensurable
231+
end
232+
SOURCE
233+
}
234+
235+
it 'should register a type object with default ensure values' do
236+
expect(subject.size).to eq(1)
237+
object = subject.first
238+
expect(object.name).to eq(:database)
239+
end
240+
end
241+
226242
describe 'parsing an ensurable type with default ensure values' do
227243
let(:source) { <<-SOURCE
228244
Puppet::Type.newtype(:database) do

0 commit comments

Comments
 (0)