Commit 9623307a authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Auto-detection of methods inside Ext.define().

parent a6e75ae3
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -206,7 +206,7 @@ module JsDuck
      cls[:members] = []
      cls[:statics] = []

      each_pair_in_object_expression(ast["arguments"][1]) do |key, value|
      each_pair_in_object_expression(ast["arguments"][1]) do |key, value, pair|
        case key
        when "extend"
          cls[:extends] = make_extends(value)
@@ -234,6 +234,10 @@ module JsDuck
          cls[:statics] += make_statics(value)
        when "inheritableStatics"
          cls[:statics] += make_statics(value, {:inheritable => true})
        else
          if value["type"] == "FunctionExpression"
            cls[:members] += make_auto_method(key, value, pair)
          end
        end
      end
    end
@@ -332,6 +336,25 @@ module JsDuck
      statics
    end

    def make_auto_method(name, ast, pair)
      m = make_method(name, ast)

      docset = find_docset(pair)

      if !docset || docset[:type] != :doc_comment
        m[:inheritdoc] = {}
        m[:autodetected] = true
      end

      if docset
        docset[:code] = m
        return []
      else
        m[:linenr] = pair["range"][2]
        return [m]
      end
    end

    # Looks up docset associated with given AST node.
    # A dead-stupid and -slow implementation, but works.
    def find_docset(ast)
+54 −0
Original line number Diff line number Diff line
@@ -165,4 +165,58 @@ describe JsDuck::Aggregator do
      @docs.length.should == 0
    end
  end

  shared_examples_for "auto detected method" do
    it "detects a method" do
      method[:tagname].should == :method
    end

    it "detects method name" do
      method[:name].should == 'foo'
    end

    it "flags method with :inheritdoc" do
      method[:inheritdoc].should == {}
    end

    it "flags method as :autodetected" do
      method[:autodetected].should == true
    end
  end

  describe "method without comment inside Ext.define" do
    let(:method) do
      parse(<<-EOS)[0][:members][:method][0]
        /**
         * Some documentation.
         */
        Ext.define("MyClass", {
            foo: function() {}
        });
      EOS
    end

    it_should_behave_like "auto detected method"
  end

  describe "method with line comment inside Ext.define" do
    let(:method) do
      parse(<<-EOS)[0][:members][:method][0]
        /**
         * Some documentation.
         */
        Ext.define("MyClass", {
            // My docs
            foo: function() {}
        });
      EOS
    end

    it_should_behave_like "auto detected method"

    it "detects method documentation" do
      method[:doc].should == 'My docs'
    end
  end

end