Commit 509ab8a7 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for type unions in parenthesis.

In supporting Google Closure Compiler syntax.
parent 7e03d6ad
Loading
Loading
Loading
Loading
+34 −15
Original line number Diff line number Diff line
@@ -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
@@ -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(/\[\]/)
@@ -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
+16 −0
Original line number Diff line number Diff line
@@ -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