Commit 5c3e7b4b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for @class.

Also, when function name begins with uppercase letter, it's assumed to be
class name.
parent 970e8612
Loading
Loading
Loading
Loading
+25 −8
Original line number Diff line number Diff line
@@ -100,10 +100,14 @@ module JsDuck
      parse(purify(input))
    end

    # sets the name and properties of the default at-tag
    def set_default(tagname, attrs={})
      if !@tags[tagname] then
        @tags[tagname] = attrs
    # Sets the name property of the default at-tag.
    # When name begins with uppercase it's considered to be class name,
    # otherwise a function name.
    def set_default_name(name)
      tagname = (name[0,1] == name[0,1].upcase) ? :class : :function
      
      if !@tags[:class] && !@tags[:function] then
        @tags[tagname] = {:name => name}
        @tags[tagname][:doc] = @tags[:default][:doc]
      end
    end
@@ -154,6 +158,8 @@ module JsDuck
          at_param
        elsif look(/@function\b/) then
          at_function
        elsif look(/@class\b/) then
          at_class
        elsif look(/@/) then
          @current_tag[:doc] += @input.scan(/@/)
        elsif look(/[^@]/) then
@@ -204,6 +210,17 @@ module JsDuck
      skip_white
    end

    # matches @class name ...
    def at_class
      match(/@class/)
      @current_tag = @tags[:class] = {:doc => ""}
      skip_white
      if look(/\w/) then
        @current_tag[:name] = ident
      end
      skip_white
    end

    # matches {...} and returns text inside brackets
    def typedef
      match(/\{/)
@@ -248,19 +265,19 @@ module JsDuck
          if @lex.look("function", :ident) then
            @lex.next
            # function name(){
            doc.set_default(:function, {:name => @lex.next})
            doc.set_default_name(@lex.next)
            doc.set_default_params(parse_params)
          elsif @lex.look("var", :ident, "=", "function") then
            @lex.next
            # var name = function(){
            doc.set_default(:function, {:name => @lex.next})
            doc.set_default_name(@lex.next)
            @lex.next # =
            doc.set_default_params(parse_anonymous_function_params)
          elsif @lex.look(:ident, "=", "function") ||
              @lex.look(:ident, ":", "function") ||
              @lex.look(:string, ":", "function") then
            # name: function(){
            doc.set_default(:function, {:name => @lex.next})
            doc.set_default_name(@lex.next)
            @lex.next # : or =
            doc.set_default_params(parse_anonymous_function_params)
          elsif @lex.look(:ident, ".") then
@@ -270,7 +287,7 @@ module JsDuck
              @lex.next
              name = @lex.next
              if @lex.look("=", "function") then
                doc.set_default(:function, {:name => name})
                doc.set_default_name(name)
                @lex.next # =
                doc.set_default_params(parse_anonymous_function_params)
              end
+23 −0
Original line number Diff line number Diff line
@@ -202,5 +202,28 @@ function f(foo, bar){}
    assert_equal("Number", params[1][:type])
  end

  def test_explicit_class_name
    docs = JsDuck.parse("
/**
 * @class Foo
 * My class
 */
function Bar(){}
")
    assert_equal("Foo", docs[0][:class][:name])
    assert_equal("My class", docs[0][:class][:doc])
  end

  def test_uppercase_function_name_implies_class_name
    docs = JsDuck.parse("
/**
 * My class
 */
function Foo(){}
")
    assert_equal("Foo", docs[0][:class][:name])
    assert_equal("My class", docs[0][:class][:doc])
  end

end