Extract class parameters from parser output

Re-worked the YARD transformer and handlers to attach parameters to the
resulting code objects. Some architectural debt was incurred in the interests
of producing a working prototype quickly.
This commit is contained in:
Charlie Sharpsteen 2014-05-28 23:53:12 -07:00
parent e98761100d
commit 005a8521bd
4 changed files with 33 additions and 7 deletions

View File

@ -1,3 +1,4 @@
require 'ostruct'
require 'puppet/pops' require 'puppet/pops'
require_relative '../../yardoc' require_relative '../../yardoc'
@ -5,10 +6,17 @@ require_relative '../../yardoc'
module Puppetx::Yardoc::Pops module Puppetx::Yardoc::Pops
# An adapter class that conforms a Pops model instance + adapters to the # An adapter class that conforms a Pops model instance + adapters to the
# interface expected by YARD handlers. # interface expected by YARD handlers.
class YARDStatement #
# FIXME: Inhertiting from OpenStruct is a bit of a hack. It allows attributes
# to be declared as needed but in the long run understandibility of the code
# would be improved by having a concrete model.
class YARDStatement < OpenStruct
attr_reader :pops_obj, :comments attr_reader :pops_obj, :comments
def initialize(pops_obj) def initialize(pops_obj)
# Initialize OpenStruct
super({})
unless pops_obj.is_a? Puppet::Pops::Model::PopsObject unless pops_obj.is_a? Puppet::Pops::Model::PopsObject
raise ArgumentError, "A YARDStatement can only be initialized from a PopsObject. Got a: #{pops_obj.class}" raise ArgumentError, "A YARDStatement can only be initialized from a PopsObject. Got a: #{pops_obj.class}"
end end

View File

@ -29,12 +29,21 @@ module Puppetx::Yardoc::Pops
o.definitions.map{|d| transform(d)} o.definitions.map{|d| transform(d)}
end end
# Extract comments from "Definition" objects. That is: nodes definitions, # Extract comments from type definitions and class definitions. Wrap them
# type definitions and class definitions. Wrap them into YARDStatement # into YARDStatement objects that provide an interface for YARD handlers.
# objects that provide an interface for YARD handlers. def transform_NamedDefinition(o)
def transform_Definition(o) obj = YARDStatement.new(o)
YARDStatement.new(o) obj.parameters = o.parameters.map do |p|
param_tuple = [transform(p)]
param_tuple << ( p.value.nil? ? nil : transform(p.value) )
end end
obj
end
# Catch-all visitor.
def transform_Positioned(o)
YARDStatement.new(o)
end
end end
end end

View File

@ -9,6 +9,10 @@ module Puppetx::Yardoc::YARD::CodeObjects
# @return [HostClassObject, Proxy, nil] # @return [HostClassObject, Proxy, nil]
attr_accessor :parent_class attr_accessor :parent_class
# A list of parameters attached to this class.
# @return [Array<Array(String, String)>]
attr_accessor :parameters
# The `YARD::Codeobjects::Base` class pulls a bunch of shenanigans to # The `YARD::Codeobjects::Base` class pulls a bunch of shenanigans to
# insert proxy namespaces. Unfortunately, said shenanigans pick up on the # insert proxy namespaces. Unfortunately, said shenanigans pick up on the
# `::` in Puppet names and start to mangle things based on rules for the # `::` in Puppet names and start to mangle things based on rules for the

View File

@ -5,7 +5,12 @@ module Puppetx::Yardoc::YARD::Handlers
handles HostClassDefinition handles HostClassDefinition
process do process do
obj = HostClassObject.new(:root, statement.pops_obj.name) obj = HostClassObject.new(:root, statement.pops_obj.name) do |o|
o.parameters = statement.parameters.map do |a|
param_tuple = [a[0].pops_obj.name]
param_tuple << ( a[1].nil? ? nil : a[1].source )
end
end
statement.pops_obj.tap do |o| statement.pops_obj.tap do |o|
if o.parent_class if o.parent_class