Commit d43b707e authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Let EsprimaParser report comment type.

Two possible types:

- :doc_comment - comments beginning with /**
- :plain_comment - all other comments

Additionally we strip "*" from the beginning of doc-comment.
parent f1b40216
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ module JsDuck
    #         :comment => "The contents of the comment",
    #         :code => {...AST data structure for code following the comment...},
    #         :linenr => 12,  // Beginning with 1
    #         :type => :doc_comment, // or :plain_comment
    #     }
    #
    def parse(input)
@@ -80,10 +81,20 @@ module JsDuck
      @start_linenr = 1

      @ast["comments"].map do |comment|
        # Detect comment type and strip * at the beginning of doc-comment
        value = comment["value"]
        if comment["type"] == "Block" && value =~ /\A\*/
          type = :doc_comment
          value = value.slice(1, value.length-1)
        else
          type = :plain_comment
        end

        {
          :comment => comment["value"],
          :comment => value,
          :code => stuff_after(comment),
          :linenr => line_number(comment["range"][0]),
          :type => type,
        }
      end
    end
+38 −0
Original line number Diff line number Diff line
@@ -25,6 +25,44 @@ describe JsDuck::EsprimaParser do
    end
  end

  describe "parsing line comment" do
    before do
      @docs = parse("// Hello world")
    end

    it "results in plain comment" do
      @docs[0][:type].should == :plain_comment
    end
  end

  describe "parsing block comment" do
    before do
      @docs = parse("/* Hello world */")
    end

    it "results in plain comment" do
      @docs[0][:type].should == :plain_comment
    end

    it "doesn't strip anything from the beginning of comment" do
      @docs[0][:comment].should == " Hello world "
    end
  end

  describe "parsing block comment beginning with /**" do
    before do
      @docs = parse("/** Hello world */")
    end

    it "results in doc comment" do
      @docs[0][:type].should == :doc_comment
    end

    it "strips * at the beginning of comment" do
      @docs[0][:comment].should == " Hello world "
    end
  end

  describe "parsing comment after function" do
    before do
      @docs = parse(<<-EOS)