Loading lib/jsduck/type_parser.rb +34 −15 Original line number Diff line number Diff line Loading @@ -72,15 +72,12 @@ module JsDuck # # <null-type> ::= [ "?" | "!" ] <array-type> # # <array-type> ::= <type-name> [ "[]" ]* # <array-type> ::= <atomic-type> [ "[]" ]* # # <type-name> ::= <ident-chain> | "*" # # <ident-chain> ::= <ident> [ "." <ident> ]* # <atomic-type> ::= <type-name> | <union-type> # # <ident> ::= [a-zA-Z0-9_]+ # <union-type> ::= "(" <alteration-type> ")" # # dot-separated identifiers followed by optional "[]" def varargs_type if @input.scan(/\.\.\./) varargs = true Loading @@ -91,17 +88,15 @@ module JsDuck @out << nullability end type = @input.scan(/[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*|\*/) if @input.scan(/\(/) @out << "(" if !type return false elsif @relations[type] @out << @formatter.link(type, nil, type) elsif @relations.ignore?(type) || type == "undefined" || type == "*" @out << type return false unless alteration_type return false unless @input.scan(/\)/) @out << ")" else @error = :name return false return false unless type_name end while @input.scan(/\[\]/) Loading @@ -115,6 +110,30 @@ module JsDuck true end # # <type-name> ::= <ident-chain> | "*" # # <ident-chain> ::= <ident> [ "." <ident> ]* # # <ident> ::= [a-zA-Z0-9_]+ # def type_name name = @input.scan(/[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*|\*/) if !name return false elsif @relations[name] @out << @formatter.link(name, nil, name) elsif @relations.ignore?(name) || name == "undefined" || name == "*" @out << name else @error = :name return false end true end end end spec/type_parser_spec.rb +16 −0 Original line number Diff line number Diff line Loading @@ -146,6 +146,22 @@ describe JsDuck::TypeParser do parse("String|Number|RegExp").should == true end it "matches union of one simple type" do parse("(String)").should == true end it "matches union of two simple types" do parse("(String|Number)").should == true end it "matches union type in varargs context" do parse("...(String|Number)").should == true end it "matches nested union type" do parse("(String|(Number|RegExp))").should == true end end end Loading Loading
lib/jsduck/type_parser.rb +34 −15 Original line number Diff line number Diff line Loading @@ -72,15 +72,12 @@ module JsDuck # # <null-type> ::= [ "?" | "!" ] <array-type> # # <array-type> ::= <type-name> [ "[]" ]* # <array-type> ::= <atomic-type> [ "[]" ]* # # <type-name> ::= <ident-chain> | "*" # # <ident-chain> ::= <ident> [ "." <ident> ]* # <atomic-type> ::= <type-name> | <union-type> # # <ident> ::= [a-zA-Z0-9_]+ # <union-type> ::= "(" <alteration-type> ")" # # dot-separated identifiers followed by optional "[]" def varargs_type if @input.scan(/\.\.\./) varargs = true Loading @@ -91,17 +88,15 @@ module JsDuck @out << nullability end type = @input.scan(/[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*|\*/) if @input.scan(/\(/) @out << "(" if !type return false elsif @relations[type] @out << @formatter.link(type, nil, type) elsif @relations.ignore?(type) || type == "undefined" || type == "*" @out << type return false unless alteration_type return false unless @input.scan(/\)/) @out << ")" else @error = :name return false return false unless type_name end while @input.scan(/\[\]/) Loading @@ -115,6 +110,30 @@ module JsDuck true end # # <type-name> ::= <ident-chain> | "*" # # <ident-chain> ::= <ident> [ "." <ident> ]* # # <ident> ::= [a-zA-Z0-9_]+ # def type_name name = @input.scan(/[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*|\*/) if !name return false elsif @relations[name] @out << @formatter.link(name, nil, name) elsif @relations.ignore?(name) || name == "undefined" || name == "*" @out << name else @error = :name return false end true end end end
spec/type_parser_spec.rb +16 −0 Original line number Diff line number Diff line Loading @@ -146,6 +146,22 @@ describe JsDuck::TypeParser do parse("String|Number|RegExp").should == true end it "matches union of one simple type" do parse("(String)").should == true end it "matches union of two simple types" do parse("(String|Number)").should == true end it "matches union type in varargs context" do parse("...(String|Number)").should == true end it "matches nested union type" do parse("(String|(Number|RegExp))").should == true end end end Loading