From c9ad1d46a0daecf389e5bd61dda025e16318e982 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Tue, 13 Mar 2012 16:14:42 +0200 Subject: [PATCH] Support this: and new: inside function type. Also now only allow optionality suffix "=" be used inside function type arguments. --- lib/jsduck/type_parser.rb | 54 ++++++++++++++++++++++++++++++++++----- spec/type_parser_spec.rb | 31 +++++++++++++++++----- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/lib/jsduck/type_parser.rb b/lib/jsduck/type_parser.rb index 08b0094c..d01c829f 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,9 +78,6 @@ module JsDuck skip_whitespace end - @out << "=" if @input.scan(/=/) - skip_whitespace - true end @@ -144,14 +141,14 @@ module JsDuck end # - # ::= "function(" ")" [ ":" ] + # ::= "function(" ")" [ ":" ] # def function_type @out << @input.scan(/function\(/) skip_whitespace if !@input.check(/\)/) - return false unless type_arguments + return false unless function_type_arguments end return false unless @input.scan(/\)/) @@ -167,6 +164,51 @@ module JsDuck true end + # + # ::= [ "," ]* + # + # ::= "new" ":" + # | "this" ":" + # | + # + def function_type_arguments + skip_whitespace + + # First argument is special + if s = @input.scan(/new\s*:\s*/) + @out << s + return false unless type_name + elsif s = @input.scan(/this\s*:\s*/) + @out << s + return false unless type_name + else + return false unless ftype_arg + end + + skip_whitespace + + # Go through additional arguments, separated with "," + while @input.check(/,/) + @out << @input.scan(/,/) + return false unless ftype_arg + end + + true + end + + # + # ::= [ "=" ] + # + def ftype_arg + return false unless alteration_type + + # Each argument can be optional (ending with "=") + @out << "=" if @input.scan(/[=]/) + skip_whitespace + + true + end + # # ::= | "*" # diff --git a/spec/type_parser_spec.rb b/spec/type_parser_spec.rb index 78872ea8..67f03a5c 100644 --- a/spec/type_parser_spec.rb +++ b/spec/type_parser_spec.rb @@ -173,12 +173,15 @@ describe JsDuck::TypeParser do parse("( String | Number )").should == true end - # 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 + # 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. + # + # We do support the optional notation inside function type + # parameter lists (see below). + it "doesn't accept optional parameter notation" do + parse("String=").should == false end it "matches single type argument" do @@ -229,6 +232,22 @@ describe JsDuck::TypeParser do parse("function(Number=)").should == true end + it "matches function type with this: argument" do + parse("function(this:Array, Number)").should == true + end + + it "matches function type with new: argument" do + parse("function(new:Array)").should == true + end + + it "matches function type with this: argument + ws" do + parse("function(this : Array, Number)").should == true + end + + it "matches function type with new: argument + ws" do + parse("function(new : Array)").should == true + end + it "matches function type with extra whitespace" do parse("function( ) : Array").should == true end -- GitLab