Skip to content

Commit f98873d

Browse files
authored
Merge pull request #315 from danielparks/fix_default_expressions
(#240) Fix output of default values that are expressions
2 parents ece8be7 + 2088d38 commit f98873d

File tree

5 files changed

+83
-15
lines changed

5 files changed

+83
-15
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ def literal_Object(o)
152152

153153
def literal_AccessExpression(o)
154154
# Extract the raw text of the Access Expression
155-
::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
155+
PuppetStrings::Yard::Util.ast_to_text(o)
156156
end
157157

158158
def literal_QualifiedReference(o)
159159
# Extract the raw text of the Qualified Reference
160-
::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
160+
PuppetStrings::Yard::Util.ast_to_text(o)
161161
end
162162

163163
# ----- The following methods are the same as the original Literal_evaluator

lib/puppet-strings/yard/parsers/puppet/statement.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ class Statement
2121
def initialize(object, file)
2222
@file = file
2323

24-
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(object)
25-
@source = adapter.extract_text
26-
@line = adapter.line
24+
@source = PuppetStrings::Yard::Util.ast_to_text(object)
25+
@line = object.line
2726
@comments_range = nil
2827
end
2928

@@ -86,13 +85,11 @@ def initialize(parameter)
8685
@name = parameter.name
8786
# Take the exact text for the type expression
8887
if parameter.type_expr
89-
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(parameter.type_expr)
90-
@type = adapter.extract_text
88+
@type = PuppetStrings::Yard::Util.ast_to_text(parameter.type_expr)
9189
end
9290
# Take the exact text for the default value expression
9391
return unless parameter.value
94-
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(parameter.value)
95-
@value = adapter.extract_text
92+
@value = PuppetStrings::Yard::Util.ast_to_text(parameter.value)
9693
end
9794
end
9895

@@ -149,8 +146,7 @@ def initialize(object, file)
149146
return unless object.respond_to? :return_type
150147
type = object.return_type
151148
return unless type
152-
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(type)
153-
@type = adapter.extract_text.gsub('>> ', '')
149+
@type = PuppetStrings::Yard::Util.ast_to_text(type).gsub('>> ', '')
154150
end
155151
end
156152

@@ -182,12 +178,11 @@ def initialize(object, file)
182178
case type_expr
183179
when Puppet::Pops::Model::AccessExpression
184180
# TODO: I don't like rebuilding the source from the AST, but AccessExpressions don't expose the original source
185-
@alias_of = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(type_expr.left_expr).extract_text + '['
186-
@alias_of << type_expr.keys.map { |key| ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(key).extract_text }.join(', ')
181+
@alias_of = PuppetStrings::Yard::Util.ast_to_text(type_expr.left_expr) + '['
182+
@alias_of << type_expr.keys.map { |key| PuppetStrings::Yard::Util.ast_to_text(key) }.join(', ')
187183
@alias_of << ']'
188184
else
189-
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(type_expr)
190-
@alias_of = adapter.extract_text
185+
@alias_of = PuppetStrings::Yard::Util.ast_to_text(type_expr)
191186
end
192187
@name = object.name
193188
end

lib/puppet-strings/yard/util.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,11 @@ def self.docstring_to_hash(docstring, select_tags = nil)
7979

8080
hash
8181
end
82+
83+
# Convert Puppet AST to text.
84+
# @param [Puppet::Pops::Model::PopsObject] ast The Puppet AST to convert to text.
85+
# @return [String] Returns a string of Puppet code.
86+
def self.ast_to_text(ast)
87+
ast.locator.extract_tree_text(ast)
88+
end
8289
end

spec/unit/puppet-strings/yard/parsers/puppet/parser_spec.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,63 @@ class bar {
296296
end
297297
end
298298
end
299+
300+
[
301+
'undef',
302+
'true',
303+
'-1',
304+
'0.34',
305+
'bareword',
306+
"'single quotes'",
307+
'"double quotes"',
308+
'[]',
309+
'[1]',
310+
'{}',
311+
'{ a => 1 }',
312+
'$param1',
313+
'1 + 1',
314+
'func()',
315+
'$param1.foo(1)',
316+
'$param1.foo($param2 + $param3.bar())',
317+
].each do |value|
318+
describe "parsing parameter with #{value} default value" do
319+
let(:source) { <<~PUPPET }
320+
class foo (
321+
$param1 = #{value},
322+
) {}
323+
PUPPET
324+
325+
it 'finds correct value' do
326+
spec_subject.parse
327+
statement = spec_subject.enumerator.first
328+
expect(statement.parameters.size).to eq(1)
329+
expect(statement.parameters[0].value).to eq(value)
330+
end
331+
end
332+
end
333+
334+
[
335+
'$param1.foo()',
336+
"$facts['kernel'] ? {
337+
'Linux' => 'linux',
338+
'Darwin' => 'darwin',
339+
default => $facts['kernel'],
340+
}",
341+
].each do |value|
342+
describe "parsing parameter with #{value} default value" do
343+
let(:source) { <<~PUPPET }
344+
class foo (
345+
$param1 = #{value},
346+
) {}
347+
PUPPET
348+
349+
it 'finds correct value' do
350+
skip('Broken by https://tickets.puppetlabs.com/browse/PUP-11632')
351+
spec_subject.parse
352+
statement = spec_subject.enumerator.first
353+
expect(statement.parameters.size).to eq(1)
354+
expect(statement.parameters[0].value).to eq(value)
355+
end
356+
end
357+
end
299358
end

spec/unit/puppet-strings/yard/util_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@
4747
expect(spec_subject.github_to_yard_links(str)).to eq('<a href="#label-Module+description"> module-description')
4848
end
4949
end
50+
51+
describe 'ast_to_text' do
52+
it 'converts a simple AST correctly' do
53+
model = Puppet::Pops::Parser::Parser.new.parse_string('class test {}').model
54+
expect(described_class.ast_to_text(model.body)).to eq('class test {}')
55+
end
56+
end
5057
end

0 commit comments

Comments
 (0)