Commit 79dcd0a9 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Detect simple default values from code.

First off supporting String, Number, RegExp, and Boolean.
parent 4f1f6a6b
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -161,13 +161,13 @@ module JsDuck
      elsif look("Ext", ".", "extend")
        ext_extend
      elsif look(:string)
        {:type => :literal, :class => "String"}
        {:type => :literal, :class => "String", :value => '"' + match(:string) + '"'}
      elsif look("true") || look("false")
        {:type => :literal, :class => "Boolean"}
        {:type => :literal, :class => "Boolean", :value => match(:ident)}
      elsif look(:number)
        {:type => :literal, :class => "Number"}
        {:type => :literal, :class => "Number", :value => match(:number)}
      elsif look(:regex)
        {:type => :literal, :class => "RegExp"}
        {:type => :literal, :class => "RegExp", :value => match(:regex)}
      end
    end

+4 −2
Original line number Diff line number Diff line
@@ -164,7 +164,7 @@ module JsDuck
        :owner => detect_owner(doc_map) || owner,
        :type => detect_type(:cfg, doc_map, code),
        :doc => detect_doc(docs),
        :default => detect_default(:cfg, doc_map),
        :default => detect_default(:cfg, doc_map, code),
        :properties => detect_subproperties(docs, :cfg),
      }, doc_map)
    end
@@ -272,10 +272,12 @@ module JsDuck
      end
    end

    def detect_default(tagname, doc_map)
    def detect_default(tagname, doc_map, code)
      main_tag = doc_map[tagname] ? doc_map[tagname].first : {}
      if main_tag[:default]
        main_tag[:default]
      elsif code[:type] == :assignment && code[:right]
        code[:right][:value]
      end
    end

+42 −0
Original line number Diff line number Diff line
@@ -234,4 +234,46 @@ describe JsDuck::Aggregator do
    end
  end

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

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

  describe "cfg with implicit default regex value" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @cfg {Number} foo Something
         */
        foo: /[a-z]/
      EOS
    end
    it "detects the default value" do
      @doc[:default].should == '/[a-z]/'
    end
  end

end