Commit 005bd5b0 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for object literal and emptyFn detection.

parent 642e4a81
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -48,6 +48,8 @@ module JsDuck
          :method
        elsif var && function?(var["init"])
          :method
        elsif property?(ast) && function?(ast["value"])
          :method
        elsif doc_map[:return] || doc_map[:param]
          :method
        else
@@ -75,13 +77,21 @@ module JsDuck
    end

    def function?(ast)
      ast["type"] == "FunctionDeclaration" || ast["type"] == "FunctionExpression"
      ast["type"] == "FunctionDeclaration" || ast["type"] == "FunctionExpression" || empty_fn?(ast)
    end

    def empty_fn?(ast)
      ast["type"] == "MemberExpression" && to_s(ast) == "Ext.emptyFn"
    end

    def var?(ast)
      ast["type"] == "VariableDeclaration"
    end

    def property?(ast)
      ast["type"] == "Property"
    end

    # Class name begins with upcase char
    def class_name?(ast)
      return to_s(ast).split(/\./).last =~ /\A[A-Z]/
+36 −0
Original line number Diff line number Diff line
@@ -79,9 +79,45 @@ describe JsDuck::DocType do
      detect("/** */ foo = function() {}").should == :method
    end

    it "assignment of Ext.emptyFn" do
      detect("/** */ foo = Ext.emptyFn").should == :method
    end

    it "var initialized with function" do
      detect("/** */ var foo = function() {}").should == :method
    end

    it "vari initialized with Ext.emptyFn" do
      detect("/** */ var foo = Ext.emptyFn").should == :method
    end

    it "object property initialized with function" do
      detect(<<-EOS).should == :method
        Foo = {
            /** */
            bar: function(){}
        };
      EOS
    end

    it "object property in comma-first notation initialized with function" do
      detect(<<-EOS).should == :method
        Foo = {
            foo: 5
            /** */
            , bar: function(){}
        };
      EOS
    end

    it "object property initialized with Ext.emptyFn" do
      detect(<<-EOS).should == :method
        Foo = {
            /** */
            bar: Ext.emptyFn
        };
      EOS
    end
  end

  describe "detects as event" do