From 02c3ae67ae8ff2d9eb9f5369dcde3c7b311bc4a7 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Fri, 4 Jan 2013 15:41:16 +0200 Subject: [PATCH] Extract Subproperties class from DocAst. --- lib/jsduck/doc_ast.rb | 37 ++++------------------- lib/jsduck/subproperties.rb | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 lib/jsduck/subproperties.rb diff --git a/lib/jsduck/doc_ast.rb b/lib/jsduck/doc_ast.rb index 088269ce..cdf34a83 100644 --- a/lib/jsduck/doc_ast.rb +++ b/lib/jsduck/doc_ast.rb @@ -1,5 +1,5 @@ -require 'jsduck/logger' require 'jsduck/tag_registry' +require 'jsduck/subproperties' module JsDuck @@ -195,43 +195,16 @@ module JsDuck end def detect_params(doc_map) - combine_properties(doc_map[:param] || []) + nest_properties(doc_map[:param] || []) end def detect_subproperties(tagname, docs) prop_docs = docs.find_all {|tag| tag[:tagname] == tagname} - prop_docs.length > 0 ? combine_properties(prop_docs)[0][:properties] : [] + prop_docs.length > 0 ? nest_properties(prop_docs)[0][:properties] : [] end - def combine_properties(raw_items) - # First item can't be namespaced, if it is ignore the rest. - if raw_items[0] && raw_items[0][:name] =~ /\./ - return [raw_items[0]] - end - - # build name-index of all items - index = {} - raw_items.each {|it| index[it[:name]] = it } - - # If item name has no dots, add it directly to items array. - # Otherwise look up the parent of item and add it as the - # property of that parent. - items = [] - raw_items.each do |it| - if it[:name] =~ /^(.+)\.([^.]+)$/ - it[:name] = $2 - parent = index[$1] - if parent - parent[:properties] = [] unless parent[:properties] - parent[:properties] << it - else - Logger.warn(:subproperty, "Ignoring subproperty #{$1}.#{$2}, no parent found with name '#{$1}'.", @filename, @linenr) - end - else - items << it - end - end - items + def nest_properties(raw_items) + Subproperties.nest(raw_items, @filename, @linenr) end def detect_return(doc_map) diff --git a/lib/jsduck/subproperties.rb b/lib/jsduck/subproperties.rb new file mode 100644 index 00000000..372a0a21 --- /dev/null +++ b/lib/jsduck/subproperties.rb @@ -0,0 +1,58 @@ +require 'jsduck/logger' +require 'jsduck/util/singleton' + +module JsDuck + + # Detects nested structure of subproperties. + class Subproperties + include Util::Singleton + + # Given array of e.g. @param tags from DocParser with names + # containing dots: + # + # {:name => "foo"}, + # {:name => "foo.bar"}, + # {:name => "foo.baz"}, + # {:name => "zap"}, + # + # Produces nested structure: + # + # {:name => "foo", :properties => [ + # {:name => "bar"}, + # {:name => "baz"}]}, + # {:name => "zap"}, + # + def nest(raw_items, filename, linenr) + # First item can't be namespaced, if it is ignore the rest. + if raw_items[0] && raw_items[0][:name] =~ /\./ + return [raw_items[0]] + end + + # build name-index of all items + index = {} + raw_items.each {|it| index[it[:name]] = it } + + # If item name has no dots, add it directly to items array. + # Otherwise look up the parent of item and add it as the + # property of that parent. + items = [] + raw_items.each do |it| + if it[:name] =~ /^(.+)\.([^.]+)$/ + it[:name] = $2 + parent = index[$1] + if parent + parent[:properties] = [] unless parent[:properties] + parent[:properties] << it + else + Logger.warn(:subproperty, "Ignoring subproperty #{$1}.#{$2}, no parent found with name '#{$1}'.", filename, linenr) + end + else + items << it + end + end + items + end + + end + +end -- GitLab