Commit 54520abf authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Mark params autodetection in generic way.

For this make Ast detect no params as nil instead of [].
Similarly detect no explicit params as nil not [].
This allows the auto-merging to pick the one with some params
and use that plus mark params :autodetected when merged from code.

But the actual merging of params is still done in Param class
because of the complicated types merging logic.

Additionally refactored the param warnings creation to separate
method.
parent 4113fb40
Loading
Loading
Loading
Loading
+5 −1
Original line number Original line Diff line number Diff line
@@ -314,7 +314,7 @@ module JsDuck
        return {
        return {
          :tagname => :method,
          :tagname => :method,
          :name => name,
          :name => name,
          :params => make_params(ast),
          :params => empty_array_to_nil(make_params(ast)),
          :chainable => chainable?(ast) && name != "constructor",
          :chainable => chainable?(ast) && name != "constructor",
        }
        }
      end
      end
@@ -327,6 +327,10 @@ module JsDuck
        end
        end
      end
      end


      def empty_array_to_nil(arr)
        arr.length == 0 ? nil : arr
      end

      def chainable?(ast)
      def chainable?(ast)
        if ast.function? && !ast.ext_empty_fn?
        if ast.function? && !ast.ext_empty_fn?
          Js::Function.return_types(ast.raw) == [:this]
          Js::Function.return_types(ast.raw) == [:this]
+22 −25
Original line number Original line Diff line number Diff line
@@ -28,14 +28,11 @@ module JsDuck::Tag


    def process_doc(h, tags, pos)
    def process_doc(h, tags, pos)
      h[:params] = JsDuck::Doc::Subproperties.nest(tags, pos)
      h[:params] = JsDuck::Doc::Subproperties.nest(tags, pos)
      h[:params] = nil if h[:params].length == 0
    end
    end


    def merge(h, docs, code)
    def merge(h, docs, code)
      h[:params] = merge_params(docs, code, h[:files].first)
      h[:params] = merge_params(docs, code, h[:files].first)

      if only_autodetected_params?(docs, code)
        JsDuck::DocsCodeComparer.mark_autodetected(h, :params)
      end
    end
    end


    def format(m, formatter)
    def format(m, formatter)
@@ -48,13 +45,30 @@ module JsDuck::Tag


    private
    private


    def only_autodetected_params?(docs, code)
      (docs[:params] || []).length == 0 && (code[:params] || []).length > 0
    end

    def merge_params(docs, code, file)
    def merge_params(docs, code, file)
      explicit = docs[:params] || []
      explicit = docs[:params] || []
      implicit = JsDuck::DocsCodeComparer.matches?(docs, code) ? (code[:params] || []) : []
      implicit = JsDuck::DocsCodeComparer.matches?(docs, code) ? (code[:params] || []) : []
      print_warnings(explicit, implicit, file)

      # Override implicit parameters with explicit ones
      # But if explicit ones exist, don't append the implicit ones.
      params = []
      (explicit.length > 0 ? explicit.length : implicit.length).times do |i|
        im = implicit[i] || {}
        ex = explicit[i] || {}
        params << {
          :type => ex[:type] || im[:type] || "Object",
          :name => ex[:name] || im[:name] || "",
          :doc => ex[:doc] || im[:doc] || "",
          :optional => ex[:optional] || false,
          :default => ex[:default],
          :properties => ex[:properties] || [],
        }
      end
      params
    end

    def print_warnings(explicit, implicit, file)
      ex_len = explicit.length
      ex_len = explicit.length
      im_len = implicit.length
      im_len = implicit.length


@@ -75,23 +89,6 @@ module JsDuck::Tag
        str = ex_names.zip(im_names).map {|p| ex, im = p; ex == im ? ex : (ex||"")+"/"+(im||"") }.join(", ")
        str = ex_names.zip(im_names).map {|p| ex, im = p; ex == im ? ex : (ex||"")+"/"+(im||"") }.join(", ")
        JsDuck::Logger.warn(:param_count, "Documented and auto-detected params don't match: #{str}", file)
        JsDuck::Logger.warn(:param_count, "Documented and auto-detected params don't match: #{str}", file)
      end
      end

      # Override implicit parameters with explicit ones
      # But if explicit ones exist, don't append the implicit ones.
      params = []
      (ex_len > 0 ? ex_len : im_len).times do |i|
        im = implicit[i] || {}
        ex = explicit[i] || {}
        params << {
          :type => ex[:type] || im[:type] || "Object",
          :name => ex[:name] || im[:name] || "",
          :doc => ex[:doc] || im[:doc] || "",
          :optional => ex[:optional] || false,
          :default => ex[:default],
          :properties => ex[:properties] || [],
        }
      end
      params
    end
    end


  end
  end
+2 −2
Original line number Original line Diff line number Diff line
@@ -66,11 +66,11 @@ describe "JsDuck::Js::Ast detects method with" do


  describe "no params in" do
  describe "no params in" do
    it "function declaration without params" do
    it "function declaration without params" do
      detect("/** */ function foo() {}")[:params].length.should == 0
      detect("/** */ function foo() {}")[:params].should == nil
    end
    end


    it "Ext.emptyFn assignment" do
    it "Ext.emptyFn assignment" do
      detect("/** */ foo = Ext.emptyFn")[:params].length.should == 0
      detect("/** */ foo = Ext.emptyFn")[:params].should == nil
    end
    end
  end
  end