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

Initial @enum tag implementation.

It acts just like class with special enum=true flag.

The values of an enum are just class properties.
parent 464fa18e
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -51,6 +51,9 @@ module JsDuck
        :singleton => !!doc_map[:singleton],
        :requires => detect_list(:requires, doc_map),
        :uses => detect_list(:uses, doc_map),
        # properties of enum-classes
        :enum => extract(doc_map, :class, :enum),
        :type => extract(doc_map, :class, :type),
      }, doc_map)
    end

+12 −0
Original line number Diff line number Diff line
@@ -130,6 +130,8 @@ module JsDuck
          at_var
        elsif look(/@throws\b/)
          at_throws
        elsif look(/@enum\b/)
          at_enum
        elsif look(/@inheritable\b/)
          boolean_at_tag(/@inheritable/, :inheritable)
        elsif look(/@accessor\b/)
@@ -282,6 +284,16 @@ module JsDuck
      skip_white
    end

    # matches @enum {type} name ...
    def at_enum
      match(/@enum/)
      add_tag(:class)
      @current_tag[:enum] = true
      maybe_type
      maybe_name_with_default
      skip_white
    end

    # matches @type {type}  or  @type type
    #
    # The presence of @type implies that we are dealing with property.
+83 −0
Original line number Diff line number Diff line
require "jsduck/aggregator"
require "jsduck/source_file"

describe JsDuck::Aggregator do
  def parse(string)
    agr = JsDuck::Aggregator.new
    agr.aggregate(JsDuck::SourceFile.new(string))
    agr.result
  end

  shared_examples_for "enum" do
    it "creates class" do
      doc[:tagname].should == :class
    end
    it "sets :enum flag to true" do
      doc[:enum].should == true
    end
    it "detects name" do
      doc[:name].should == "My.enum.Type"
    end
    it "detects type" do
      doc[:type].should == "String"
    end
    it "detects no extends" do
      doc[:extends].should == nil
    end
    it "detects docs" do
      doc[:doc].should == "Some documentation."
    end

    it "detects two properties" do
      doc[:members][:property].length.should == 2
    end

    describe "in first property" do
      let(:prop) { doc[:members][:property][0] }
      it "detects name" do
        prop[:name].should == 'foo'
      end
      it "detects type" do
        prop[:type].should == 'String'
      end
      it "detects default value" do
        prop[:default].should == "'a'"
      end
    end
  end

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

    it_should_behave_like "enum"
  end

  describe "implicitly named enum" do
    let(:doc) do
      parse(<<-EOS)[0]
        /**
         * @enum {String}
         * Some documentation.
         */
        My.enum.Type = {
            /** First value docs */
            foo: 'a',
            /** Second value docs */
            bar: 'b'
        };
      EOS
    end

    it_should_behave_like "enum"
  end

end