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

Support for optional parameter notation in TypeParser.

Needed for function type parameter types.
parent a85244e5
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ module JsDuck
    end

    #
    #     <alteration-type> ::= <varargs-type> [ ("/" | "|") <varargs-type> ]*
    #     <alteration-type> ::= <varargs-type> [ ("/" | "|") <varargs-type> ]*  [ "=" ]
    #
    def alteration_type
      skip_whitespace
@@ -78,6 +78,9 @@ module JsDuck
        skip_whitespace
      end

      @out << "=" if @input.scan(/=/)
      skip_whitespace

      true
    end

+18 −6
Original line number Diff line number Diff line
@@ -173,12 +173,12 @@ describe JsDuck::TypeParser 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
    # type definition.
    it "doesn't accept optional parameter notation" do
      parse("String=").should == false
    # This is handled mainly inside DocParser, when it's detected over
    # there the "=" is removed from the end of type definition, so it
    # should never reach TypeParser.  But additionally it can be used
    # inside function type parameter list, so we need to support it.
    it "matches optional parameter notation" do
      parse("String=").should == true
    end

    it "matches single type argument" do
@@ -217,6 +217,18 @@ describe JsDuck::TypeParser do
      parse("function():Number").should == true
    end

    it "matches function type with varargs" do
      parse("function(...Number)").should == true
    end

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

    it "matches function type with optional argument" do
      parse("function(Number=)").should == true
    end

    it "matches function type with extra whitespace" do
      parse("function(  ) : Array").should == true
    end