Commit a6ed7b63 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for basic function types in TypeParser.

In supporting Google Closure compiler syntax.
parent f0121558
Loading
Loading
Loading
Loading
+57 −17
Original line number Diff line number Diff line
@@ -84,41 +84,81 @@ module JsDuck
    #
    #     <varargs-type> ::= [ "..." ] <null-type> | <null-type> [ "..." ]
    #
    #     <null-type> ::= [ "?" | "!" ] <array-type>
    #
    #     <array-type> ::= <atomic-type> [ "[]" ]*
    #
    #     <atomic-type> ::= <type-name> | <union-type>
    #
    #     <union-type> ::= "(" <alteration-type> ")"
    #
    def varargs_type
      if @input.scan(/\.\.\./)
        varargs = true
        @out << "..."
      end

      return false unless null_type

      if !varargs
        @out << "..." if @input.scan(/\.\.\./)
      end

      true
    end

    #
    #     <null-type> ::= [ "?" | "!" ] <array-type>
    #
    #     <array-type> ::= <atomic-type> [ "[]" ]*
    #
    #     <atomic-type> ::= <type-name> | <union-type> | <function-type>
    #
    def null_type
      if nullability = @input.scan(/[?!]/)
        @out << nullability
      end

      if @input.scan(/\(/)
        @out << "("
      if @input.check(/\(/)
        return false unless union_type
      elsif @input.check(/function\(/)
        return false unless function_type
      else
        return false unless type_name
      end

      while @input.scan(/\[\]/)
        @out << "[]"
      end

      true
    end

    #
    #     <union-type> ::= "(" <alteration-type> ")"
    #
    def union_type
      @out << @input.scan(/\(/)

      return false unless alteration_type

      return false unless @input.scan(/\)/)
      @out << ")"
      else
        return false unless type_name

      true
    end

      while @input.scan(/\[\]/)
        @out << "[]"
    #
    #     <function-type> ::= "function(" <type-arguments> ")" [ ":" <null-type> ]
    #
    def function_type
      @out << @input.scan(/function\(/)

      skip_whitespace
      if !@input.check(/\)/)
        return false unless type_arguments
      end

      if !varargs
        @out << "..." if @input.scan(/\.\.\./)
      return false unless @input.scan(/\)/)
      @out << ")"

      skip_whitespace
      if @input.scan(/:/)
        @out << ":"
        skip_whitespace
        return false unless null_type
      end

      true
+16 −0
Original line number Diff line number Diff line
@@ -205,6 +205,22 @@ describe JsDuck::TypeParser do
      parse("Array.<>").should == false
    end

    it "matches empty function type" do
      parse("function()").should == true
    end

    it "matches function type with arguments" do
      parse("function(String,Number)").should == true
    end

    it "matches function type with return type" do
      parse("function():Number").should == true
    end

    it "matches function type with extra whitespace" do
      parse("function(  ) : Array").should == true
    end

    it "always matches primitive types" do
      parse("boolean").should == true
      parse("number").should == true