Commit 7d955f50 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract DocsCodeComparer class.

Removing duplication of the docs and code comparison and merging code.
parent 4f746411
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
require 'jsduck/util/singleton'

module JsDuck

  # Compares documentation and code hashes.
  # Provides an utility method to help with merging.
  class DocsCodeComparer
    include Util::Singleton

    # When docs has the key, returns value from there.
    # When code has the key and matches with docs, gets value from there.
    # Otherwise returns nil.
    def merge_if_matches(key, docs, code)
      if docs[key]
        docs[key]
      elsif code[key] && matches?(docs, code)
        code[key]
      else
        nil
      end
    end

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

  end

end
+0 −16
Original line number Diff line number Diff line
@@ -91,22 +91,6 @@ module JsDuck
      end
    end

    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
+2 −19
Original line number Diff line number Diff line
require "jsduck/tag/tag"
require "jsduck/docs_code_comparer"

module JsDuck::Tag
  # There is no @default tag.
@@ -15,25 +16,7 @@ module JsDuck::Tag
    end

    def merge(h, docs, code)
      h[:default] = merge_if_code_matches(:default, docs, code)
    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]
      h[:default] = JsDuck::DocsCodeComparer.merge_if_matches(:default, docs, code)
    end
  end
end
+2 −6
Original line number Diff line number Diff line
require "jsduck/tag/tag"
require "jsduck/doc/subproperties"
require "jsduck/docs_code_comparer"

module JsDuck::Tag
  class Param < Tag
@@ -33,7 +34,7 @@ module JsDuck::Tag

    def merge_params(docs, code)
      explicit = docs[:params] || []
      implicit = code_matches_doc?(docs, code) ? (code[:params] || []) : []
      implicit = JsDuck::DocsCodeComparer.matches?(docs, code) ? (code[:params] || []) : []
      # Override implicit parameters with explicit ones
      # But if explicit ones exist, don't append the implicit ones.
      params = []
@@ -52,10 +53,5 @@ module JsDuck::Tag
      params
    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
+2 −18
Original line number Diff line number Diff line
require "jsduck/tag/tag"
require "jsduck/docs_code_comparer"

module JsDuck::Tag
  class Type < Tag
@@ -29,28 +30,11 @@ module JsDuck::Tag

    # Do the merging of :type field
    def merge(h, docs, code)
      h[:type] = merge_if_code_matches(:type, docs, code)
      h[:type] = JsDuck::DocsCodeComparer.merge_if_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