Commit 0f94aa5c authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Auto-detection of event name from this.fireEvent().

One can now write doc-comment for event before the this.fireEvent()
and the event name will be picked up automatically, without any need
to add @event tag.
parent ac76529a
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -106,6 +106,10 @@ module JsDuck
      elsif property?(ast) && function?(ast["value"])
        make_method(key_value(ast["key"]), ast["value"])

      # this.fireEvent("foo", ...)
      elsif exp && fire_event?(exp)
        make_event(to_value(exp["arguments"][0]))

      # foo = ...
      elsif exp && assignment?(exp)
        make_property(to_s(exp["left"]), exp["right"])
@@ -173,6 +177,10 @@ module JsDuck
      @ext_patterns.matches?(pattern, to_s(ast))
    end

    def fire_event?(ast)
      call?(ast) && to_s(ast["callee"]) == "this.fireEvent"
    end

    def var?(ast)
      ast["type"] == "VariableDeclaration"
    end
@@ -437,6 +445,13 @@ module JsDuck
      end
    end

    def make_event(name)
      return {
        :tagname => :event,
        :name => name,
      }
    end

    def make_property(name=nil, ast=nil, tagname=:property)
      return {
        :tagname => tagname,
+31 −0
Original line number Diff line number Diff line
@@ -75,4 +75,35 @@ describe JsDuck::Aggregator do
    it_should_behave_like "event"
  end

  describe "implicit event name inside this.fireEvent()" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * Fires when needed.
         */
        this.fireEvent("mousedown", foo, 7);
      EOS
    end
    it_should_behave_like "event"
  end

  describe "doc-comment followed by this.fireEvent without event name" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * Fires when needed.
         */
        this.fireEvent(foo, 7);
      EOS
    end

    it "creates event" do
      @doc[:tagname].should == :event
    end

    it "leaves the name of event empty" do
      @doc[:name].should == ""
    end
  end

end