diff --git a/lib/jsduck/type_parser.rb b/lib/jsduck/type_parser.rb index 67d3cce0a02d7d7d7970c5267a0178abb3d0c83e..08b0094c40fea2cc85db3e71fa1026c6728528e8 100644 --- a/lib/jsduck/type_parser.rb +++ b/lib/jsduck/type_parser.rb @@ -59,7 +59,7 @@ module JsDuck end # - # ::= [ ("/" | "|") ]* + # ::= [ ("/" | "|") ]* [ "=" ] # def alteration_type skip_whitespace @@ -78,6 +78,9 @@ module JsDuck skip_whitespace end + @out << "=" if @input.scan(/=/) + skip_whitespace + true end diff --git a/spec/type_parser_spec.rb b/spec/type_parser_spec.rb index c88d277e3ca562e2f58bd156028b7704db6292eb..78872ea83b15d4fc8077b7dc64927166a42f27e0 100644 --- a/spec/type_parser_spec.rb +++ b/spec/type_parser_spec.rb @@ -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