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

Make auto-detected enum properties public.

Auto detected properties default to being private normally, but for
enums, all possible values should be public.
parent 4587709f
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -230,10 +230,10 @@ module JsDuck
    end

    # Loops through all enums and auto-detects their types if needed.
    def infer_enum_types
    def process_enums
      @classes.each_value do |cls|
        if cls[:enum] && !cls[:enum][:type]
          Enum.infer_type(cls)
        if cls[:enum]
          Enum.process(cls)
        end
      end
    end
+1 −1
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@ module JsDuck
      agr.remove_ignored_classes
      agr.create_accessors
      agr.append_ext4_event_options
      agr.infer_enum_types
      agr.process_enums
      agr.result
    end

+18 −2
Original line number Diff line number Diff line
@@ -3,16 +3,32 @@ require 'jsduck/logger'
module JsDuck

  class Enum
    # Applies additional processing to enum-class.
    def self.process(cls)
      cls[:enum][:type] = self.infer_type(cls) unless cls[:enum][:type]
      self.unsure_public(cls)
    end

    private

    # Given an enum class, returns the type infered from its values.
    def self.infer_type(cls)
      if cls[:members][:property].length > 0
        types = cls[:members][:property].map {|p| p[:type] }
        cls[:enum][:type] = types.sort.uniq.join("/")
        types.sort.uniq.join("/")
      else
        cls[:enum][: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)
        "Object"
      end
    end

    # Ensures that enum values are all public.
    # For this we remove the auto-inserted inheritdoc tag.
    def self.unsure_public(cls)
      cls[:members][:property].each do |p|
        p[:inheritdoc] = nil if p[:autodetected]
      end
    end
  end
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ module JsDuck

        new_cfgs = []
        cls.all_local_members.each do |member|
          if member[:inheritdoc] || member[:autodetected]
          if member[:inheritdoc]
            resolve(member, new_cfgs)
          end
        end
+22 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ describe JsDuck::Aggregator do
  def parse(string)
    agr = JsDuck::Aggregator.new
    agr.aggregate(JsDuck::SourceFile.new(string))
    agr.infer_enum_types
    agr.process_enums
    agr.result
  end

@@ -153,4 +153,25 @@ describe JsDuck::Aggregator do
    end
  end

  describe "enum two properties" do
    let(:doc) do
      parse(<<-EOS)[0]
        /** @enum */
        My.enum.Type = {
            foo: "hello",
            /** @inheritdoc */
            bar: 8
        };
      EOS
    end

    it "gets stripped from :inheritdoc tag in auto-detected property" do
      doc[:members][:property][0][:inheritdoc].should == nil
    end

    it "keeps the explicit :inheritdoc tag in doc-commented property" do
      doc[:members][:property][1][:inheritdoc].should_not == nil
    end
  end

end