Commit 8ca7c33a authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for @class doc-comment right before object literal.

This allows for a way to document classes that aren't created with
a standard Ext.define:

    createClass("Blah", /** @class Blah */ {
        foo: 17,
        doSometing: function() {},
        doOtherThing: function() {}
    });

All the members inside the object literal will end up inside the class.

Additionally this is equivalent to the functionality of @lends tag in
jsdoc-toolkit, but we achieve the same without having a separate tag.
parent 57aaea48
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -88,6 +88,10 @@ module JsDuck
      elsif function?(ast) && class_name?(to_s(ast["id"]))
        make_class(to_s(ast["id"]))

      # { ... }
      elsif object?(ast)
        make_class("", ast)

      # function foo() {}
      elsif function?(ast)
        make_method(to_s(ast["id"]), ast)
@@ -183,6 +187,10 @@ module JsDuck
      ast["type"] == "Literal" && ast["value"].is_a?(String)
    end

    def object?(ast)
      ast["type"] == "ObjectExpression"
    end

    # Class name begins with upcase char
    def class_name?(name)
      return name.split(/\./).last =~ /\A[A-Z]/
@@ -199,12 +207,12 @@ module JsDuck
        if ext_extend?(ast)
          args = ast["arguments"]
          cls[:extends] = to_s(args[0])
          if args.length == 2 && args[1]["type"] == "ObjectExpression"
          if args.length == 2 && object?(args[1])
            detect_class_members_from_object(cls, args[1])
          end
        elsif ext_define?(ast)
          detect_ext_define(cls, ast)
        elsif ast["type"] == "ObjectExpression"
        elsif object?(ast)
          detect_class_members_from_object(cls, ast)
        elsif ast["type"] == "ArrayExpression"
          detect_class_members_from_array(cls, ast)
@@ -461,7 +469,7 @@ module JsDuck
    # are turned into strings, but values are left as is for further
    # processing.
    def each_pair_in_object_expression(ast)
      return unless ast && ast["type"] == "ObjectExpression"
      return unless ast && object?(ast)

      ast["properties"].each do |p|
        yield(key_value(p["key"]), p["value"], p)
+16 −0
Original line number Diff line number Diff line
@@ -292,4 +292,20 @@ describe JsDuck::Aggregator do
    end
  end

  describe "method inside object literal marked with @class" do
    let(:method) do
      parse(<<-EOS)[0][:members][0]
        /**
         * @class MyClass
         * Some documentation.
         */
        createClass("MyClass", /** @class MyClass */ {
            foo: function(){}
        });
      EOS
    end

    it_should_behave_like "auto detected method"
  end

end
+8 −0
Original line number Diff line number Diff line
@@ -24,6 +24,14 @@ describe JsDuck::Ast do
      detect("/** */ var MyClass = function() {}").should == :class
    end

    it "object literal assignment to uppercase name" do
      detect("/** */ MyClass = {};").should == :class
    end

    it "doc-comment right before object literal" do
      detect("MyClass = makeClass( /** */ {} );").should == :class
    end

    it "Ext.extend()" do
      detect("/** */ MyClass = Ext.extend(Your.Class, {  });").should == :class
    end