Commit 37c4f438 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Basic implementation of @fires tag.

For documenting events thrown by a method.

Related to issue: #364
parent 3b9929f4
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
require "jsduck/tag/tag"

module JsDuck::Tag
  class Fires < Tag
    def initialize
      @pattern = "fires"
      @tagname = :fires
      @repeatable = true
      @html_position = POS_FIRES
    end

    # @fires eventname
    def parse_doc(p, pos)
      {:tagname => :fires, :events => ident_list(p)}
    end

    # matches <ident> <ident> ... until line end
    def ident_list(p)
      list = []
      while ident = p.hw.ident
        list << ident
      end
      list
    end

    def process_doc(h, tags, pos)
      h[:fires] = tags.map {|t| t[:events] }.flatten
    end

    def to_html(m)
      return [
        "<h3 class='pa'>Fires</h3>",
        "<ul>",
          m[:fires].map {|f| "<li>#{f}</li>" },
        "</ul>",
      ]
    end
  end
end
+2 −1
Original line number Diff line number Diff line
@@ -129,7 +129,8 @@ module JsDuck::Tag
    POS_SUBPROPERTIES = 12
    POS_RETURN = 13
    POS_THROWS = 14
    POS_OVERRIDES = 15
    POS_FIRES = 15
    POS_OVERRIDES = 16

    # Called before #to_html to allow rendering of Markdown content.
    # For this an instance of DocFormatter is passed in, on which one
+73 −0
Original line number Diff line number Diff line
require "mini_parser"

describe JsDuck::Aggregator do
  def parse(string)
    Helper::MiniParser.parse(string)
  end

  def parse_member(string)
    parse(string)["global"][:members][0]
  end

  describe "@fires with single event" do
    before do
      @doc = parse_member(<<-EOS)
        /**
         * Some function
         * @fires click
         */
        function bar() {}
      EOS
    end

    it "detects one fired event" do
      @doc[:fires].length.should == 1
    end

    it "detects event name that's fired" do
      @doc[:fires][0].should == "click"
    end
  end

  describe "@fires with multiple events" do
    before do
      @doc = parse_member(<<-EOS)
        /**
         * @fires click dblclick
         */
        function bar() {}
      EOS
    end

    it "detects two events" do
      @doc[:fires].length.should == 2
    end

    it "detects event names" do
      @doc[:fires][0].should == "click"
      @doc[:fires][1].should == "dblclick"
    end
  end

  describe "multiple @fires tags" do
    before do
      @doc = parse_member(<<-EOS)
        /**
         * @fires click
         * @fires dblclick
         */
        function bar() {}
      EOS
    end

    it "detects two events" do
      @doc[:fires].length.should == 2
    end

    it "detects event names" do
      @doc[:fires][0].should == "click"
      @doc[:fires][1].should == "dblclick"
    end
  end

end