Commit 9d09428e authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move :type merging to @type tag class.

parent 01c20ff3
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -52,10 +52,8 @@ module JsDuck

    def merge_like_property(docs, code)
      h = {}

      h[:type] = merge_if_code_matches(:type, docs, code)
      if h[:type] == nil
        h[:type] = code[:tagname] == :method ? "Function" : "Object"
      TagRegistry.mergers(:property_like).each do |tag|
        tag.merge(h, docs, code)
      end

      h[:default] = merge_if_code_matches(:default, docs, code)
+26 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ module JsDuck::Tag
    def initialize
      @pattern = "type"
      @key = :type
      @merge_context = :property_like
    end

    # matches @type {type}  or  @type type
@@ -26,5 +27,30 @@ module JsDuck::Tag
      h[:type] = tags[0][:type] unless h[:type]
    end

    # Do the merging of :type field
    def merge(h, docs, code)
      h[:type] = merge_if_code_matches(:type, docs, code)
      if h[:type] == nil
        h[:type] = code[:tagname] == :method ? "Function" : "Object"
      end
    end

    private

    def merge_if_code_matches(key, docs, code, default=nil)
      if docs[key]
        docs[key]
      elsif code[key] && code_matches_doc?(docs, code)
        code[key]
      else
        default
      end
    end

    # True if the name detected from code matches with explicitly documented name.
    # Also true when no explicit name documented.
    def code_matches_doc?(docs, code)
      return docs[:name] == nil || docs[:name] == code[:name]
    end
  end
end