Commit 8614ee52 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement @inheritdoc in class context.

parent 2b85a48f
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -33,10 +33,16 @@ module JsDuck
      @doc = doc
    end

    # Accessor to internal hash
    def [](key)
      @doc[key]
    end

    # Assignment to internal hash keys
    def []=(key, value)
      @doc[key] = value
    end

    # Returns instance of parent class, or nil if there is none
    def parent
      @doc[:extends] ? lookup(@doc[:extends]) : nil
+32 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ module JsDuck
    # 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)
@@ -82,6 +83,37 @@ module JsDuck
      end
    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)
      context = cls[:files][0]
      inherit = cls[:inheritdoc]

      if inherit[:cls]
        parent = @relations[inherit[:cls]]
        unless parent
          warn("@inheritdoc #{inherit[:cls]} - class not found", context)
          return cls
        end
      else
        parent = cls.parent
        if !parent
          warn("@inheritdoc - parent class not found", context)
          return cls
        end
      end

      if parent[:inheritdoc]
        find_class_parent(parent)
      else
        parent
      end
    end

    def warn(msg, context)
      Logger.instance.warn(:inheritdoc, msg, context[:filename], context[:linenr])
    end
+43 −0
Original line number Diff line number Diff line
@@ -444,5 +444,48 @@ describe JsDuck::Aggregator do
    end
  end

  describe "@inheritdoc with class name in class" do
    before do
      @docs = parse(<<-EOF)
        /**
         * @class Parent
         * Original comment.
         */
        /**
         * @class Child
         * New comment.
         * @inheritdoc Parent
         */
      EOF
      @cls = @docs["Child"]
    end

    it "combines docs from referenced class and current class" do
      @cls[:doc].should == "New comment.\n\nOriginal comment."
    end
  end

  describe "plain @inheritdoc in class" do
    before do
      @docs = parse(<<-EOF)
        /**
         * @class Parent
         * Original comment.
         */
        /**
         * @class Child
         * @extends Parent
         * New comment.
         * @inheritdoc
         */
      EOF
      @cls = @docs["Child"]
    end

    it "combines docs from parent and child" do
      @cls[:doc].should == "New comment.\n\nOriginal comment."
    end
  end

end