Commit 231c41a4 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Allow default value to be anything at all.

One can now write:

    @param {Object} [scope=this]

And the default value will be `this`.
parent e2c78681
Loading
Loading
Loading
Loading
+15 −4
Original line number Diff line number Diff line
@@ -371,7 +371,7 @@ module JsDuck
      end
    end

    # matches: <ident-chain> | "[" <ident-chain> [ "=" <literal> ] "]"
    # matches: <ident-chain> | "[" <ident-chain> [ "=" <default-value> ] "]"
    def maybe_name_with_default
      skip_horiz_white
      if look(/\[/)
@@ -381,7 +381,7 @@ module JsDuck
        if look(/=/)
          match(/=/)
          skip_horiz_white
          @current_tag[:default] = literal
          @current_tag[:default] = default_value
        end
        skip_horiz_white
        match(/\]/)
@@ -425,9 +425,20 @@ module JsDuck
      end
    end

    def literal
    # attempts to match javascript literal,
    # when it fails grabs anything up to closing "]"
    def default_value
      start_pos = @input.pos
      lit = JsLiteralParser.new(@input).literal
      lit ? JsLiteralBuilder.new.to_s(lit) : nil
      if lit && look(/ *]/)
        # When lital matched and there's nothing after it up to the closing "]"
        JsLiteralBuilder.new.to_s(lit)
      else
        # Otherwise reset parsing position to where we started
        # and rescan up to "]" using simple regex.
        @input.pos = start_pos
        match(/[^\]]*/)
      end
    end

    # matches {...} and returns text inside brackets
+23 −10
Original line number Diff line number Diff line
@@ -169,6 +169,19 @@ describe JsDuck::Aggregator do
    end
  end

  describe "cfg with this as default value" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @cfg {Number} [foo=this] Something
         */
      EOS
    end
    it "has this as default value" do
      @doc[:default].should == 'this'
    end
  end

  describe "cfg with rubbish as default value" do
    before do
      @doc = parse(<<-EOS)[0]
@@ -177,8 +190,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has no default value" do
      @doc[:default].should == nil
    it "has the rubbish as default value" do
      @doc[:default].should == '!haa'
    end
  end

@@ -190,8 +203,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has a correct default value" do
      @doc[:default].should == '7'
    it "has everything up to ] as default value" do
      @doc[:default].should == '7 and me too'
    end
  end

@@ -203,8 +216,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has nil as default value" do
      @doc[:default].should == nil
    it "has everything up to ] as default value" do
      @doc[:default].should == '[ho, ho'
    end
  end

@@ -216,8 +229,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has nil as default value" do
      @doc[:default].should == nil
    it "has the bogus object literla as default value" do
      @doc[:default].should == '{ho:5, ho}'
    end
  end

@@ -229,8 +242,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has nil as default value" do
      @doc[:default].should == nil
    it "has the unfinish object literal as default value" do
      @doc[:default].should == '{ho:5'
    end
  end