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

Support type arguments notation in TypeParser.

In supporting Google Closure Compiler syntax.
parent a386e961
Loading
Loading
Loading
Loading
+28 −1
Original line number Diff line number Diff line
@@ -111,7 +111,11 @@ module JsDuck
    end

    #
    #     <type-name> ::= <ident-chain> | "*"
    #     <type-name> ::= <type-application> | "*"
    #
    #     <type-application> ::= <ident-chain> [ "." "<" <type-arguments> ">" ]
    #
    #     <type-arguments> ::= <alteration-type> [ "," <alteration-type> ]*
    #
    #     <ident-chain> ::= <ident> [ "." <ident> ]*
    #
@@ -131,6 +135,29 @@ module JsDuck
        return false
      end

      # All type names besides * can be followed by .<arguments>
      if name != "*" && @input.scan(/\.</)
        return false unless type_arguments
        return false unless @input.scan(/>/)
      end

      true
    end

    #
    #     <type-arguments> ::= <alteration-type> [ "," <alteration-type> ]*
    #
    def type_arguments
      # First argument is required
      return false unless alteration_type

      # Go through additional arguments, separated with ","
      while @input.check(/,/)
        @out << @input.scan(/,/)
        # Fail if there's no alteration-type after ","
        return false unless alteration_type
      end

      true
    end

+21 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ describe JsDuck::TypeParser do
      "String",
      "Number",
      "RegExp",
      "Array",
      "Ext.form.Panel",
      "Ext.Element",
      "Ext.fx2.Anim",
@@ -170,6 +171,26 @@ describe JsDuck::TypeParser do
      parse("String=").should == false
    end

    it "matches single type argument" do
      parse("Array.<Number>").should == true
    end

    it "matches multiple type arguments" do
      parse("Ext.Element.<String,Number>").should == true
    end

    it "matches nested type arguments" do
      parse("Array.<Array.<String>|Array.<Number>>").should == true
    end

    it "doesn't accept type arguments on type union" do
      parse("(Array,RegExp).<String>").should == false
    end

    it "doesn't accept empty type arguments block" do
      parse("Array.<>").should == false
    end

  end

end