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

Avoid too greedy auto-detection of default values.

Don't auto-detect type and default value when documented config/property
name doesn't match up with name from code.
parent 750b5d3c
Loading
Loading
Loading
Loading
+21 −14
Original line number Diff line number Diff line
@@ -246,23 +246,22 @@ module JsDuck
    def detect_type(tagname, doc_map, code)
      main_tag = doc_map[tagname] ? doc_map[tagname].first : {}
      if main_tag[:type]
        main_tag[:type]
        return main_tag[:type]
      elsif doc_map[:type]
        doc_map[:type].first[:type]
      elsif code[:type] == :function
        "Function"
        return doc_map[:type].first[:type]
      elsif code_matches_doc?(tagname, doc_map, code)
        if code[:type] == :function
          return "Function"
        elsif code[:type] == :assignment && code[:right]
          if code[:right][:type] == :function
          "Function"
            return "Function"
          elsif code[:right][:type] == :literal
          code[:right][:class]
        else
          "Object"
            return code[:right][:class]
          end
      else
        "Object"
        end
      end
      return "Object"
    end

    def detect_extends(doc_map, code)
      if doc_map[:extends]
@@ -279,11 +278,19 @@ module JsDuck
      main_tag = doc_map[tagname] ? doc_map[tagname].first : {}
      if main_tag[:default]
        main_tag[:default]
      elsif code[:type] == :assignment && code[:right]
      elsif code_matches_doc?(tagname, doc_map, code) && code[:type] == :assignment && code[:right]
        code[:right][:value]
      end
    end

    # True if the name detected from code matches with explicitly documented name.
    # Also true when no explicit name documented.
    def code_matches_doc?(tagname, doc_map, code)
      explicit_name = detect_name(tagname, doc_map, {})
      implicit_name = detect_name(tagname, {}, code)
      return explicit_name == "" || explicit_name == implicit_name
    end

    def detect_optional(tagname, doc_map)
      main_tag = doc_map[tagname] ? doc_map[tagname].first : {}
      return main_tag[:optional] != false
+34 −0
Original line number Diff line number Diff line
@@ -304,6 +304,40 @@ describe JsDuck::Aggregator do
    end
  end

  describe "cfg with implicit name followed by code field with another name" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @cfg foo
         */
        bar: true
      EOS
    end
    it "doesn't get the default value from code" do
      @doc[:default].should == nil
    end
    it "doesn't get the type from code" do
      @doc[:type].should == "Object"
    end
  end

  describe "cfg without implicit name followed by code" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @cfg
         */
        bar: true
      EOS
    end
    it "gets default value from code" do
      @doc[:default].should == "true"
    end
    it "gets the type from code" do
      @doc[:type].should == "Boolean"
    end
  end

  describe "a normal config option" do
    before do
      @doc = parse(<<-EOS)[0]