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

Handling of few likely object properties errors.

Mostly just ensuring that we don't crash if bogus input is given.
parent 896fb9d2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ module JsDuck
    # Pass in :css to be able to parse CSS doc-comments
    def initialize(mode = :js)
      @ident_pattern = (mode == :css) ? /\$[\w-]*/ : /\w+/
      @ident_chain_pattern = (mode == :css) ? /\$[\w.-]*/ : /[\w.]+/
      @ident_chain_pattern = (mode == :css) ? /\$[\w-]+(\.[\w-]+)*/ : /\w+(\.\w+)*/
    end

    def parse(input)
+5 −0
Original line number Diff line number Diff line
@@ -346,6 +346,11 @@ module JsDuck
    end

    def combine_properties(raw_items)
      # First item can't be namespaced, if it is ignore the rest.
      if raw_items[0] && raw_items[0][:name] =~ /\./
        return [raw_items[0]]
      end

      # build name-index of all items
      index = {}
      raw_items.each {|it| index[it[:name]] = it }
+70 −0
Original line number Diff line number Diff line
@@ -211,4 +211,74 @@ describe JsDuck::Aggregator do
    it_should_behave_like "object with properties"
  end

  # Tests with buggy syntax

  describe "config option with properties in wrong order" do
    before do
      @obj = parse(<<-EOS)[0]
        /**
         * @cfg {Object} coord Geographical coordinates
         * @cfg {Number} coord.lat.numerator Numerator part of a fraction
         * @cfg {Number} coord.lat.denominator Denominator part of a fraction
         * @cfg {Object} coord.lat Latitude
         * @cfg {Number} coord.lng Longitude
         */
      EOS
      @name = "coord"
    end

    it_should_behave_like "object with properties"
  end

  describe "only namespaced config options" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @cfg {Number} coord.lat Latitude
         * @cfg {Number} coord.lng Latitude
         */
      EOS
    end

    it "interpreted as just one config" do
      @doc[:name].should == "coord.lat"
    end
  end

  describe "normal config option name with dot after it" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @cfg {Number} coord. Coordinate
         */
      EOS
    end

    it "has no dot in name" do
      @doc[:name].should == "coord"
    end

    it "has dot in doc" do
      @doc[:doc].should == ". Coordinate"
    end
  end

  describe "normal config option name with dot before it" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @cfg {Number} .coord Coordinate
         */
      EOS
    end

    it "has empty name" do
      @doc[:name].should == ""
    end

    it "has dot in doc" do
      @doc[:doc].should == ".coord Coordinate"
    end
  end

end