lib/jsduck/inherit_doc.rb in jsduck-3.6.1 vs lib/jsduck/inherit_doc.rb in jsduck-3.7.0
- old
+ new
@@ -10,10 +10,11 @@
end
# Performs all inheriting
def resolve_all
@relations.each do |cls|
+ resolve_class(cls) if cls[:inheritdoc]
cls.all_local_members.each do |member|
if member[:inheritdoc]
resolve(member)
end
end
@@ -31,60 +32,83 @@
# Finds parent member of the given member. When @inheritdoc names
# a member to inherit from, finds that member instead.
#
# If the parent also has @inheritdoc, continues recursively.
def find_parent(m)
- context = m[:files][0]
- inherit = m[:inheritdoc]
+ if m[:inheritdoc][:cls]
+ # @inheritdoc MyClass#member
+ parent_cls = @relations[m[:inheritdoc][:cls]]
+ return warn("class not found", m) unless parent_cls
- if inherit[:cls]
- parent_cls = @relations[inherit[:cls]]
- unless parent_cls
- warn("@inheritdoc #{inherit[:cls]}##{inherit[:member]} - class not found", context)
- return m
- end
- parent = parent_cls.get_members(inherit[:member], inherit[:type] || m[:tagname], inherit[:static] || m[:meta][:static])[0]
- unless parent
- warn("@inheritdoc #{inherit[:cls]}##{inherit[:member]} - member not found", context)
- return m
- end
+ parent = lookup_member(parent_cls, m)
+ return warn("member not found", m) unless parent
+
+ elsif m[:inheritdoc][:member]
+ # @inheritdoc #member
+ parent = lookup_member(@relations[m[:owner]], m)
+ return warn("member not found", m) unless parent
+
else
+ # @inheritdoc
parent_cls = @relations[m[:owner]].parent
mixins = @relations[m[:owner]].mixins
+
# Warn when no parent or mixins at all
- if !parent_cls && mixins.length == 0
- warn("@inheritdoc - parent class not found", context)
- return m
- end
+ return warn("parent class not found", m) unless parent_cls || mixins.length > 0
+
# First check for the member in all mixins, because members
# from mixins override those from parent class. Looking first
# from mixins is probably a bit slower, but it's the correct
# order to do things.
if mixins.length > 0
- parent = mixins.map do |mix|
- mix.get_members(m[:name], m[:tagname], m[:meta][:static])[0]
- end.compact.first
+ parent = mixins.map {|mix| lookup_member(mix, m) }.compact.first
end
+
# When not found, try looking from parent class
if !parent && parent_cls
- parent = parent_cls.get_members(m[:name], m[:tagname], m[:meta][:static])[0]
+ parent = lookup_member(parent_cls, m)
end
+
# Only when both parent and mixins fail, throw warning
- if !parent
- warn("@inheritdoc - parent member not found", context)
- return m
- end
+ return warn("parent member not found", m) unless parent
end
- if parent[:inheritdoc]
- find_parent(parent)
+ return parent[:inheritdoc] ? find_parent(parent) : parent
+ end
+
+ def lookup_member(cls, m)
+ inherit = m[:inheritdoc]
+ cls.get_members(inherit[:member] || m[:name], inherit[:type] || m[:tagname], inherit[:static] || m[:meta][:static])[0]
+ end
+
+ # Copy over doc from parent class.
+ def resolve_class(cls)
+ parent = find_class_parent(cls)
+ cls[:doc] = (cls[:doc] + "\n\n" + parent[:doc]).strip
+ end
+
+ def find_class_parent(cls)
+ if cls[:inheritdoc][:cls]
+ # @inheritdoc MyClass
+ parent = @relations[cls[:inheritdoc][:cls]]
+ return warn("class not found", cls) unless parent
else
- parent
+ # @inheritdoc
+ parent = cls.parent
+ return warn("parent class not found", cls) unless parent
end
+
+ return parent[:inheritdoc] ? find_class_parent(parent) : parent
end
- def warn(msg, context)
+ def warn(msg, item)
+ context = item[:files][0]
+ i_member = item[:inheritdoc][:member]
+
+ msg = "@inheritdoc #{item[:inheritdoc][:cls]}"+ (i_member ? "#" + i_member : "") + " - " + msg
Logger.instance.warn(:inheritdoc, msg, context[:filename], context[:linenr])
+
+ return item
end
end
end