Commit 66e50725 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support strange ...[] varargs syntax.

It completely puzzles me why Google Closure Compiler requires
varargs type name in function argument context to be wrapped inside
square braces, while in normal context no square brackets are needed.

But here we go...
parent b73e984b
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -33,11 +33,10 @@ module JsDuck
  #     function(new:goog.ui.Menu, string)
  #     function(this:goog.ui.Menu, string)
  #     function(?string=, number=)
  #     function(string, ...[number])
  #
  # Currently not supported:
  #
  #     function(string, ...[number])
  #
  #     {myNum: number, myObject}
  #
  class TypeParser
@@ -110,12 +109,19 @@ module JsDuck
    end

    #
    #     <varargs-type> ::= [ "..." ] <null-type> | <null-type> [ "..." ]
    #     <varargs-type> ::= "..." <null-type>
    #                      | "..." "[" <null-type> "]"
    #                      | <null-type> "..."
    #                      | <null-type>
    #
    def varargs_type
      if @input.scan(/\.\.\./)
        varargs = true
        @out << "..."
        if @input.scan(/\[/)
          varargs_bracketed = true
          @out << "["
        end
      end

      return false unless null_type
@@ -124,6 +130,11 @@ module JsDuck
        @out << "..." if @input.scan(/\.\.\./)
      end

      if varargs_bracketed
        return false unless @input.scan(/]/)
        @out << "]"
      end

      true
    end

+16 −8
Original line number Diff line number Diff line
@@ -120,17 +120,19 @@ describe JsDuck::TypeParser do
      parse("*").should == true
    end

    it "matches the varargs notation at the beginning" do
    describe "varargs" do
      it "matches the notation at the beginning" do
        parse("...String").should == true
      end

    it "doesn't accept varargs notation without a type name" do
      it "doesn't accept notation without a type name" do
        parse("...").should == false
      end

    it "doesn't accept both varargs notations at the same time" do
      it "doesn't accept both notations at the same time" do
        parse("...*...").should == false
      end
    end

    it "matches the nullable notation" do
      parse("?String").should == true
@@ -231,6 +233,12 @@ describe JsDuck::TypeParser do
        parse("function(...Number)").should == true
      end

      # For some reason Google Closure Compiler requires varargs type
      # in function argument context to be wrapped inside [] brackets.
      it "matches ...[] varargs syntax" do
        parse("function(...[String])").should == true
      end

      it "matches nullable/non-nullable arguments" do
        parse("function(!String, ?Number)").should == true
      end