Skip to content

Commit

Permalink
(puppetlabs#240) Fix output of default values that are expressions
Browse files Browse the repository at this point in the history
Previously, parameters with a default value that was an expression were
not outputted into the documentation correctly. For example,

    Integer $param = 1 + 1,

Would be shown in the documentation to have a default value of “+”.

This switches to using `extract_tree_text` instead of `extract_text` to
get the text representation of the parsed Puppet code.

This also gets rid of the dependency on
`Puppet::Pops::Adapters::SourcePosAdapter`, which was [deprecated in
2017](puppetlabs/puppet@68498ad).
  • Loading branch information
danielparks committed Sep 25, 2022
1 parent 2a63064 commit 4b9a5f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ def literal_Object(o)

def literal_AccessExpression(o)
# Extract the raw text of the Access Expression
::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
PuppetStrings::Yard::Util.ast_to_text(o)
end

def literal_QualifiedReference(o)
# Extract the raw text of the Qualified Reference
::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
PuppetStrings::Yard::Util.ast_to_text(o)
end

# ----- The following methods are the same as the original Literal_evaluator
Expand Down
21 changes: 8 additions & 13 deletions lib/puppet-strings/yard/parsers/puppet/statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class Statement
def initialize(object, file)
@file = file

adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(object)
@source = adapter.extract_text
@line = adapter.line
@source = PuppetStrings::Yard::Util.ast_to_text(object)
@line = object.line
@comments_range = nil
end

Expand Down Expand Up @@ -85,13 +84,11 @@ def initialize(parameter)
@name = parameter.name
# Take the exact text for the type expression
if parameter.type_expr
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(parameter.type_expr)
@type = adapter.extract_text
@type = PuppetStrings::Yard::Util.ast_to_text(parameter.type_expr)
end
# Take the exact text for the default value expression
if parameter.value
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(parameter.value)
@value = adapter.extract_text
@value = PuppetStrings::Yard::Util.ast_to_text(parameter.value)
end
end
end
Expand Down Expand Up @@ -149,8 +146,7 @@ def initialize(object, file)
if object.respond_to? :return_type
type = object.return_type
if type
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(type)
@type = adapter.extract_text.gsub('>> ', '')
@type = PuppetStrings::Yard::Util.ast_to_text(type).gsub('>> ', '')
end
end
end
Expand Down Expand Up @@ -184,12 +180,11 @@ def initialize(object, file)
case type_expr
when Puppet::Pops::Model::AccessExpression
# TODO: I don't like rebuilding the source from the AST, but AccessExpressions don't expose the original source
@alias_of = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(type_expr.left_expr).extract_text + '['
@alias_of << type_expr.keys.map { |key| ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(key).extract_text }.join(', ')
@alias_of = PuppetStrings::Yard::Util.ast_to_text(type_expr.left_expr) + '['
@alias_of << type_expr.keys.map { |key| PuppetStrings::Yard::Util.ast_to_text(key) }.join(', ')
@alias_of << ']'
else
adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(type_expr)
@alias_of = adapter.extract_text
@alias_of = PuppetStrings::Yard::Util.ast_to_text(type_expr)
end
@name = object.name
end
Expand Down
7 changes: 7 additions & 0 deletions lib/puppet-strings/yard/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ def self.docstring_to_hash(docstring, select_tags=nil)

hash
end

# Convert Puppet AST to text.
# @param [] ast The Puppet AST to convert to text.
# @return [String] Returns a string of Puppet code.
def self.ast_to_text(ast)
ast.locator.extract_tree_text(ast)
end
end
10 changes: 5 additions & 5 deletions spec/unit/puppet-strings/yard/parsers/puppet/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class foo {
# @param param1 First param.
# @param param2 Second param.
# @param param3 Third param.
class foo(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
class foo(String $param1, $param2, Integer $param3 = 1 + 1) inherits foo::bar {
file { '/tmp/foo':
ensure => present
}
Expand All @@ -55,22 +55,22 @@ class foo(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
expect(subject.enumerator.size).to eq(1)
statement = subject.enumerator.first
expect(statement).to be_a(PuppetStrings::Yard::Parsers::Puppet::ClassStatement)
expect(statement.source).to eq("class foo(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {\n file { '/tmp/foo':\n ensure => present\n }\n}")
expect(statement.source).to eq(source.sub(/\A.*\nclass/m, 'class').chomp)
expect(statement.file).to eq(file)
expect(statement.line).to eq(6)
expect(statement.docstring).to eq('A simple foo class.')
expect(statement.name).to eq('foo')
expect(statement.parent_class).to eq('foo::bar')
expect(statement.parameters.size).to eq(3)
expect(statement.parameters[0].name).to eq('param1')
expect(statement.parameters[0].type).to eq('Integer')
expect(statement.parameters[0].type).to eq('String')
expect(statement.parameters[0].value).to be_nil
expect(statement.parameters[1].name).to eq('param2')
expect(statement.parameters[1].type).to be_nil
expect(statement.parameters[1].value).to be_nil
expect(statement.parameters[2].name).to eq('param3')
expect(statement.parameters[2].type).to eq('String')
expect(statement.parameters[2].value).to eq('hi')
expect(statement.parameters[2].type).to eq('Integer')
expect(statement.parameters[2].value).to eq('1 + 1')
end
end

Expand Down

0 comments on commit 4b9a5f0

Please sign in to comment.