Commit 6c3ae605 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add support for @type.

parent a5dc65fe
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -69,6 +69,8 @@ module JsDuck
          at_cfg
        elsif look(/@property\b/) then
          at_property
        elsif look(/@type\b/) then
          at_type
        elsif look(/@/) then
          @current_tag[:doc] += @input.scan(/@/)
        elsif look(/[^@]/) then
@@ -195,6 +197,25 @@ module JsDuck
      skip_white
    end

    # matches @type {type}  or  @type type
    #
    # The presence of @type implies that we are dealing with property.
    # ext-doc allows type name to be either inside curly braces or
    # without them at all.
    def at_type
      match(/@type/)
      unless @tags[:property] then
        set_root_tag(:property)
      end
      skip_horiz_white
      if look(/\{/) then
        @current_tag[:type] = typedef
      elsif look(/\S/) then
        @current_tag[:type] = @input.scan(/\S+/)
      end
      skip_white
    end

    # matches {type} if possible and sets it on @current_tag
    def maybe_type
      skip_horiz_white
+28 −0
Original line number Diff line number Diff line
@@ -124,6 +124,34 @@ class TestDocCommentParser < Test::Unit::TestCase
    assert_equal("Number", doc[:property][:type])
  end

  def test_type
    doc = parse_single("/**
 * @property foo
 * @type {Boolean}
 * This is property
 */")
    assert_equal("foo", doc[:property][:name])
    assert_equal("Boolean", doc[:property][:type])
    assert_equal("This is property", doc[:property][:doc])
  end

  def test_type_without_curlies
    doc = parse_single("/**
 * @property
 * @type Boolean|String
 */")
    assert_equal("Boolean|String", doc[:property][:type])
  end

  def test_type_implies_property
    doc = parse_single("/**
 * This is property
 * @type {Boolean}
 */")
    assert_equal("Boolean", doc[:property][:type])
    assert_equal("This is property", doc[:property][:doc])
  end

  def test_long_docs
    doc = parse_single("/**
 * @method foo
+2 −0
Original line number Diff line number Diff line
@@ -234,11 +234,13 @@ foo: true,
    docs = JsDuck.parse("
/**
 * @property
 * @type Boolean
 * My comment
 */
foo: true,
")
    assert_equal("foo", docs[0][:property][:name])
    assert_equal("Boolean", docs[0][:property][:type])
    assert_equal("My comment", docs[0][:property][:doc])
  end