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

Auto-detection of Ext.override().

One can now just add an empty doc-comment before Ext.override and
all the overrides will get detected:

    /** */
    Ext.override(Some.Class, {
        foo: 1,
	doSomething: function(){}
    });
parent 6282582a
Loading
Loading
Loading
Loading
+25 −9
Original line number Diff line number Diff line
@@ -68,7 +68,11 @@ module JsDuck
      if exp && ext_define?(exp)
        make_class(to_value(exp["arguments"][0]), exp)

      # foo = Ext.extend("Parent", {})
      # Ext.override(Class, {})
      elsif exp && ext_override?(exp)
        make_class("", exp)

      # foo = Ext.extend(Parent, {})
      elsif exp && assignment?(exp) && ext_extend?(exp["right"])
        make_class(to_s(exp["left"]), exp["right"])

@@ -76,7 +80,7 @@ module JsDuck
      elsif exp && assignment?(exp) && class_name?(to_s(exp["left"]))
        make_class(to_s(exp["left"]), exp["right"])

      # var foo = Ext.extend("Parent", {})
      # var foo = Ext.extend(Parent, {})
      elsif var && var["init"] && ext_extend?(var["init"])
        make_class(to_s(var["id"]), var["init"])

@@ -163,6 +167,10 @@ module JsDuck
      call?(ast) && to_s(ast["callee"]) == "Ext.extend"
    end

    def ext_override?(ast)
      call?(ast) && to_s(ast["callee"]) == "Ext.override"
    end

    def function?(ast)
      ast["type"] == "FunctionDeclaration" || ast["type"] == "FunctionExpression" || empty_fn?(ast)
    end
@@ -204,14 +212,12 @@ module JsDuck

      # apply information from Ext.extend, Ext.define, or {}
      if ast
        if ext_extend?(ast)
          args = ast["arguments"]
          cls[:extends] = to_s(args[0])
          if args.length == 2 && object?(args[1])
            detect_class_members_from_object(cls, args[1])
          end
        elsif ext_define?(ast)
        if ext_define?(ast)
          detect_ext_define(cls, ast)
        elsif ext_extend?(ast)
          detect_ext_something(:extends, cls, ast)
        elsif ext_override?(ast)
          detect_ext_something(:override, cls, ast)
        elsif object?(ast)
          detect_class_members_from_object(cls, ast)
        elsif ast["type"] == "ArrayExpression"
@@ -222,6 +228,16 @@ module JsDuck
      return cls
    end

    # Detection of Ext.extend() or Ext.override().
    # The type parameter must be correspondingly either :extend or :override.
    def detect_ext_something(type, cls, ast)
      args = ast["arguments"]
      cls[type] = to_s(args[0])
      if args.length == 2 && object?(args[1])
        detect_class_members_from_object(cls, args[1])
      end
    end

    # Inspects Ext.define() and copies detected properties over to the
    # given cls Hash
    def detect_ext_define(cls, ast)
+27 −0
Original line number Diff line number Diff line
@@ -258,4 +258,31 @@ describe JsDuck::Aggregator do
      methods["foobar"][:doc].should == "**Overridden in blah.js.**"
    end
  end

  describe "override created with Ext.override" do
    let(:classes) do
      parse(<<-EOF)
        /** */
        Ext.define("Foo", {
            foobar: function(){}
        });

        /** */
        Ext.override(Foo, {
            bar: function(){ },
            foobar: function(){ return true; }
        });
      EOF
    end

    let(:methods) { create_members_map(classes["Foo"]) }

    it "adds member to overridden class" do
      methods["bar"].should_not == nil
    end

    it "adds note to docs about member being overridden" do
      methods["foobar"][:doc].should == "**Overridden in blah.js.**"
    end
  end
end