Commit 12b12d39 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for optional parameter type definition.

To support Google Closure Compiler syntax for optional parameters.

    @param {Type=} name

This is detected at DocParser not in TypeParser like the rest of the
type definition syntax - all other way of defining optional parameters
are also implemented in DocParser, so it keeps things simpler to have it
all in there at one spot.
parent 509ab8a7
Loading
Loading
Loading
Loading
+15 −4
Original line number Diff line number Diff line
@@ -286,7 +286,9 @@ module JsDuck
      add_tag(:type)
      skip_horiz_white
      if look(/\{/)
        @current_tag[:type] = typedef
        tdf = typedef
        @current_tag[:type] = tdf[:type]
        @current_tag[:optional] = tdf[:optional]
      elsif look(/\S/)
        @current_tag[:type] = @input.scan(/\S+/)
      end
@@ -354,10 +356,13 @@ module JsDuck
    end

    # matches {type} if possible and sets it on @current_tag
    # Also checks for {optionality=} in type definition.
    def maybe_type
      skip_horiz_white
      if look(/\{/)
        @current_tag[:type] = typedef
        tdf = typedef
        @current_tag[:type] = tdf[:type]
        @current_tag[:optional] = tdf[:optional]
      end
    end

@@ -431,12 +436,18 @@ module JsDuck
      end
    end

    # matches {...} and returns text inside brackets
    # matches {...=} and returns text inside brackets
    def typedef
      match(/\{/)
      name = @input.scan(/[^}]+/)
      if name =~ /=$/
        name = name.chop
        optional = true
      else
        optional = false
      end
      match(/\}/)
      return name
      return {:type => name, :optional => optional}
    end

    # matches <ident_chain> <ident_chain> ... until line end
+17 −0
Original line number Diff line number Diff line
@@ -87,6 +87,23 @@ describe JsDuck::Aggregator do
    it_should_behave_like "optional parameter"
  end

  describe "parameter with optional param type annotation {Foo=}" do
    before do
      @param = parse(<<-EOS)[0][:params][0]
        /**
         * @param {Number=} foo Something
         */
        function foo() {}
      EOS
    end

    it_should_behave_like "optional parameter"

    it "doesn't include the optionality notation to type definition" do
      @param[:type].should == "Number"
    end
  end

  describe "parameter with explicit default value" do
    before do
      @param = parse(<<-EOS)[0][:params][0]
+8 −0
Original line number Diff line number Diff line
@@ -162,6 +162,14 @@ describe JsDuck::TypeParser do
      parse("(String|(Number|RegExp))").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
    end

  end

end