From d50c7156eca8aeebcdeb2804345fe7e6e7de7332 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Tue, 13 Mar 2012 15:39:15 +0200 Subject: [PATCH] Support for optional parameter notation in TypeParser. Needed for function type parameter types. --- lib/jsduck/type_parser.rb | 5 ++++- spec/type_parser_spec.rb | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/jsduck/type_parser.rb b/lib/jsduck/type_parser.rb index 67d3cce0..08b0094c 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 c88d277e..78872ea8 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 -- GitLab