Commit 07336f8f authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Infer enum type from value types.

parent 7fcfd126
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -228,6 +228,22 @@ module JsDuck
      end
    end

    def infer_enum_types
      @classes.each_value do |cls|
        if cls[:enum] && !cls[:type]
          if cls[:members][:property].length > 0
            types = cls[:members][:property].map {|p| p[:type] }
            cls[:type] = types.sort.uniq.join("/")
          else
            cls[:type] = "Object"
            file = cls[:files][0][:filename]
            line = cls[:files][0][:linenr]
            Logger.instance.warn(:enum, "Enum #{cls[:name]} defined without values in it", file, line)
          end
        end
      end
    end

    # Are we dealing with ExtJS 4?
    # True if any of the classes is defined with Ext.define()
    def ext4?
+1 −0
Original line number Diff line number Diff line
@@ -125,6 +125,7 @@ module JsDuck
      agr.remove_ignored_classes
      agr.create_accessors
      agr.append_ext4_event_options
      agr.infer_enum_types
      agr.result
    end

+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ module JsDuck
        [:sing_static, "Singleton class member marked as @static"],
        [:type_syntax, "Syntax error in {type definition}"],
        [:type_name, "Unknown type referenced in {type definition}"],
        [:enum, "Enum defined without any values in it"],

        [:image, "{@img} referring to missing file"],
        [:image_unused, "An image exists in --images dir that's not used"],
+56 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ describe JsDuck::Aggregator do
  def parse(string)
    agr = JsDuck::Aggregator.new
    agr.aggregate(JsDuck::SourceFile.new(string))
    agr.infer_enum_types
    agr.result
  end

@@ -97,4 +98,59 @@ describe JsDuck::Aggregator do
    it_should_behave_like "enum"
  end

  describe "enum without a type" do
    let(:doc) do
      parse(<<-EOS)[0]
        /**
         * @enum
         * Some documentation.
         */
        My.enum.Type = {
            foo: 'a',
            bar: 'b'
        };
      EOS
    end

    it "infers type from code" do
      doc[:type].should == 'String'
    end
  end

  describe "enum without a type and no type in code" do
    let(:doc) do
      parse(<<-EOS)[0]
        /**
         * @enum
         * Some documentation.
         */
        My.enum.Type = {};
      EOS
    end

    it "defaults to Object type" do
      doc[:type].should == 'Object'
    end
  end

  describe "enum with multiple types in code" do
    let(:doc) do
      parse(<<-EOS)[0]
        /**
         * @enum
         * Some documentation.
         */
        My.enum.Type = {
            foo: 15,
            bar: 'hello',
            baz: 8
        };
      EOS
    end

    it "defaults to auto-generated type union" do
      doc[:type].should == 'Number/String'
    end
  end

end