Sha256: 1c85a74d48103fa08107aef664d5eff2dff99a6f6d9c37cc57a6b98c33ffcd00

Contents?: true

Size: 1.85 KB

Versions: 28

Compression:

Stored size: 1.85 KB

Contents

require "jsduck/logger"
require "jsduck/merger"

module JsDuck
  # Performs the merging of :params field.
  # Used by Method, Event and CssMixin members.
  class ParamsMerger
    # Ensures the existance of params array.
    # Defaults type of each parameter to "Object".
    # Logs warnings for inconsistencies between params in code and in docs.
    def self.merge(h, docs, code)
      h[:params] = [] unless h[:params]

      h[:params].each do |p|
        p[:type] = "Object" unless p[:type]
      end

      check_consistency(docs, code, h[:files].first)
    end

    def self.check_consistency(docs, code, file)
      explicit = docs[:params] || []
      implicit = JsDuck::Merger.can_be_autodetected?(docs, code) ? (code[:params] || []) : []
      ex_len = explicit.length
      im_len = implicit.length

      if ex_len == 0 || im_len == 0
        # Skip when either no implicit or explicit params
      elsif ex_len != im_len && explicit.last[:type] =~ /\.\.\.$/
        # Skip when vararg params are in play.
      elsif ex_len < im_len
        # Warn when less parameters documented than found from code.
        JsDuck::Logger.warn(:param_count, "Detected #{im_len} params, but only #{ex_len} documented.", file)
      elsif ex_len > im_len
        # Warn when more parameters documented than found from code.
        JsDuck::Logger.warn(:param_count, "Detected #{im_len} params, but #{ex_len} documented.", file)
      elsif implicit.map {|p| p[:name] } != explicit.map {|p| p[:name] }
        # Warn when parameter names don't match up.
        ex_names = explicit.map {|p| p[:name] }
        im_names = implicit.map {|p| p[:name] }
        str = ex_names.zip(im_names).map {|p| ex, im = p; ex == im ? ex : (ex||"")+"/"+(im||"") }.join(", ")
        JsDuck::Logger.warn(:param_count, "Documented and auto-detected params don't match: #{str}", file)
      end
    end

  end
end

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/params_merger.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/params_merger.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/params_merger.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/params_merger.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/params_merger.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/params_merger.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.10 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.9 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.8 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.7 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.5 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.4 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.3 lib/jsduck/params_merger.rb
jsduck-troopjs-0.0.1 lib/jsduck/params_merger.rb
jsduck-6.0.0beta lib/jsduck/params_merger.rb
jsduck-5.3.4 lib/jsduck/params_merger.rb
jsduck-5.3.3 lib/jsduck/params_merger.rb
jsduck-5.3.2 lib/jsduck/params_merger.rb
jsduck-5.3.1 lib/jsduck/params_merger.rb