Commit 6580f12b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for extra whitespace in type annotations.

parent e22fd11a
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -54,14 +54,20 @@ module JsDuck
    #     <alteration-type> ::= <varargs-type> [ ("/" | "|") <varargs-type> ]*
    #
    def alteration_type
      skip_whitespace

      # Return immediately if varargs-type doesn't match
      return false unless varargs_type

      skip_whitespace

      # Go through enumeration of types, separated with "/" or "|"
      while @input.check(/[\/|]/)
        @out << @input.scan(/[\/|]/)
        # Fail if there's no varargs-type after / or |

        skip_whitespace
        return false unless varargs_type
        skip_whitespace
      end

      true
@@ -148,19 +154,29 @@ module JsDuck
    #     <type-arguments> ::= <alteration-type> [ "," <alteration-type> ]*
    #
    def type_arguments
      skip_whitespace

      # First argument is required
      return false unless alteration_type

      skip_whitespace

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

        skip_whitespace
        return false unless alteration_type
        skip_whitespace
      end

      true
    end

    def skip_whitespace
      @input.scan(/\s*/)
    end

  end

end
+12 −0
Original line number Diff line number Diff line
@@ -147,6 +147,10 @@ describe JsDuck::TypeParser do
      parse("String|Number|RegExp").should == true
    end

    it "matches alteration with extra spacing" do
      parse(" String | Number ").should == true
    end

    it "matches union of one simple type" do
      parse("(String)").should == true
    end
@@ -163,6 +167,10 @@ describe JsDuck::TypeParser do
      parse("(String|(Number|RegExp))").should == true
    end

    it "matches union with extra spacing" do
      parse("( String | Number )").should == true
    end

    # This is handled inside DocParser, when it's detected over there
    # the "=" is removed from the end of type definition, so it should
    # never reach TypeParser if there is just one "=" at the end of
@@ -179,6 +187,10 @@ describe JsDuck::TypeParser do
      parse("Ext.Element.<String,Number>").should == true
    end

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

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