Commit 2b865b57 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Set return type of constructors to owner class.

No more simply labeling constructors with "Object" return type -
instead the ReturnValues class assigns the actual class as return type.
parent f41e6750
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -394,7 +394,7 @@ module JsDuck
        :tagname => :method,
        :name => name,
        :params => make_params(ast),
        :chainable => chainable?(ast),
        :chainable => chainable?(ast) && name != "constructor",
      }
    end

+4 −5
Original line number Diff line number Diff line
@@ -58,14 +58,13 @@ module JsDuck

    def create_method(docs)
      doc_map = build_doc_map(docs)
      name = detect_name(:method, doc_map)
      return add_shared({
        :tagname => :method,
        :name => name,
        :name => detect_name(:method, doc_map),
        :owner => detect_owner(doc_map),
        :doc => detect_doc(docs),
        :params => detect_params(doc_map),
        :return => detect_return(doc_map, name == "constructor" ? "Object" : "undefined"),
        :return => detect_return(doc_map),
        :throws => detect_throws(doc_map),
      }, doc_map)
    end
@@ -257,10 +256,10 @@ module JsDuck
      items
    end

    def detect_return(doc_map, default_type="undefined")
    def detect_return(doc_map)
      ret = extract(doc_map, :return) || {}
      return {
        :type => ret[:type] || default_type,
        :type => ret[:type] || "undefined",
        :name => ret[:name] || "return",
        :doc => ret[:doc] || "",
        :properties => doc_map[:return] ? detect_subproperties(:return, doc_map[:return]) : []
+16 −1
Original line number Diff line number Diff line
@@ -28,13 +28,19 @@ module JsDuck
    private

    def process(m)
      if chainable?(m)
      if constructor?(m)
        add_return_new(m)
      elsif chainable?(m)
        add_return_this(m)
      elsif returns_this?(m)
        add_chainable(m)
      end
    end

    def constructor?(m)
      m[:name] == "constructor"
    end

    def chainable?(m)
      m[:meta][:chainable]
    end
@@ -52,6 +58,15 @@ module JsDuck
        m[:return] = {:type => @cls[:name], :doc => "this"}
      end
    end

    def add_return_new(m)
      if m[:return][:type] == "undefined"
        # Create a whole new :return hash.
        # If we were to just change the :type field it would modify
        # the type of all the inherited constructor docs.
        m[:return] = {:type => @cls[:name], :doc => m[:return][:doc]}
      end
    end
  end

end
+83 −0
Original line number Diff line number Diff line
@@ -180,4 +180,87 @@ describe JsDuck::Aggregator do
    end
  end

  describe "constructor with no @return" do
    let(:cls) do
      parse(<<-EOS)["MyClass"]
        /** */
        Ext.define("MyClass", {
            /** */
            constructor: function() {}
        });
      EOS
    end

    it "sets return type to owner class" do
      cls[:members][0][:return][:type].should == "MyClass"
    end
  end

  describe "constructor with simple @return" do
    let(:cls) do
      parse(<<-EOS)["MyClass"]
        /** */
        Ext.define("MyClass", {
            /**
             * @return new instance
             */
            constructor: function() {}
        });
      EOS
    end

    it "sets return type to owner class" do
      cls[:members][0][:return][:type].should == "MyClass"
    end
  end

  describe "constructor with @constructor tag" do
    let(:cls) do
      parse(<<-EOS)["MyClass"]
        /** */
        Ext.define("MyClass", {
            /**
             * @constructor
             */
            constructor: function() {}
        });
      EOS
    end

    it "sets return type to owner class" do
      cls[:members][0][:return][:type].should == "MyClass"
    end
  end

  describe "constructor containing 'return this;'" do
    let(:cls) do
      parse(<<-EOS)["MyClass"]
        /** */
        Ext.define("MyClass", {
            /** */
            constructor: function() {return this;}
        });
      EOS
    end

    it "doesn't get @chainable tag" do
      cls[:members][0][:meta][:chainable].should_not == true
    end
  end

  describe "constructor with some other explicit return type" do
    let(:cls) do
      parse(<<-EOS)["MyClass"]
        /** */
        Ext.define("MyClass", {
            /** @return {OtherClass} new instance */
            constructor: function() {}
        });
      EOS
    end

    it "keeps the explicit return type" do
      cls[:members][0][:return][:type].should == "OtherClass"
    end
  end
end
+0 −4
Original line number Diff line number Diff line
@@ -20,10 +20,6 @@ describe JsDuck::Aggregator do
    it "has method with needed parameters" do
      methods[0][:params].length.should == 1
    end

    it "has method with default return type Object" do
      methods[0][:return][:type].should == "Object"
    end
  end

  describe "class with @constructor" do