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

Eliminate Logger#warn_nodoc method.

Change the #warn method so it can be used for also passing in
arguments, and then use it for reporting the :nodoc warning too.
For this to work:

- Logger#warn expects a `file` parameter instead of `filename` and
  `linenr`, which must always be a hash.
- The fourth parameter is then free to be used as optional `args`
  array for the warning types that need it.
parent dbbd780f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ module JsDuck
        end.map {|cls| cls[:name] }.sort

        if classes.length == 0
          Logger.warn(:cat_no_match, "No class found matching a pattern '#{name}' in categories file", @filename)
          Logger.warn(:cat_no_match, "No class found matching a pattern '#{name}' in categories file", {:filename => @filename})
        end
        classes
      end
@@ -58,7 +58,7 @@ module JsDuck
        # Check that each existing non-private & non-deprecated class is listed
        @relations.each do |cls|
          unless listed_classes[cls[:name]] || cls[:private] || cls[:deprecated]
            Logger.warn(:cat_class_missing, "Class '#{cls[:name]}' not found in categories file", @filename)
            Logger.warn(:cat_class_missing, "Class '#{cls[:name]}' not found in categories file", {:filename => @filename})
          end
        end
      end
+7 −3
Original line number Diff line number Diff line
@@ -54,11 +54,15 @@ module JsDuck
    end

    def load_guide(guide)
      return Logger.warn(:guide, "Guide not found", guide["url"]) unless File.exists?(guide["url"])
      return Logger.warn(:guide, "Guide not found", guide[:filename]) unless File.exists?(guide[:filename])
      unless File.exists?(guide["url"])
        return Logger.warn(:guide, "Guide not found", {:filename => guide["url"]})
      end
      unless File.exists?(guide[:filename])
        return Logger.warn(:guide, "Guide not found", {:filename => guide[:filename]})
      end
      unless js_ident?(guide["name"])
        # Guide name is also used as JSONP callback method name.
        return Logger.warn(:guide, "Guide name is not valid JS identifier: #{guide["name"]}", guide[:filename])
        return Logger.warn(:guide, "Guide name is not valid JS identifier: #{guide["name"]}", {:filename => guide[:filename]})
      end

      begin
+1 −1
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ module JsDuck
      end

      def warn_unused(img)
        Logger.warn(:image_unused, "Image not used.", img[:full_path])
        Logger.warn(:image_unused, "Image not used.", {:filename => img[:full_path]})
      end

      def img_record(filename)
+12 −25
Original line number Diff line number Diff line
@@ -79,37 +79,24 @@ module JsDuck
    # Works best when --processes=0, but it reduces the amount of
    # warnings greatly also when run multiple processes.
    #
    # Optionally filename and line number will be inserted to message.
    # These two last arguments can also be supplied as one hash of:
    # The `file` parameter must be a hash like:
    #
    #     {:filename => "foo.js", :linenr => 17}
    #
    def warn(type, msg, filename=nil, line=nil)
      if filename.is_a?(Hash)
        line = filename[:linenr]
        filename = filename[:filename]
      end

      if warning_enabled?(type, filename)
        print_warning(msg, filename, line)
    # When supplied, it the filename and line number will be appended
    # to the message, to convey where the warning was triggered.
    #
    # The optional `args` parameter must be an array of arguments and
    # only applies to some warning types like :nodoc.
    #
    def warn(type, msg, file={}, args=[])
      if warning_enabled?(type, file[:filename], args)
        print_warning(msg, file[:filename], file[:linenr])
      end

      return false
    end

    # Prints :nodoc warning message.
    #
    # Because the :nodoc warning needs different parameters, for now
    # we're using a separate method specially for these.
    def warn_nodoc(type, visibility, msg, file)
      filename = file[:filename]
      line = file[:linenr]

      if @warnings.enabled?(:nodoc, filename, [type, visibility])
        print_warning(msg, filename, line)
      end
    end

    # Prints fatal error message with backtrace.
    # The error param should be $! from resque block.
    def fatal(msg)
@@ -145,13 +132,13 @@ module JsDuck

    CLEAR = "\e[0m"

    def warning_enabled?(type, filename)
    def warning_enabled?(type, filename, args)
      if type == nil
        true
      elsif !@warnings.has?(type)
        warn(nil, "Unknown warning type #{type}")
      else
        @warnings.enabled?(type, filename)
        @warnings.enabled?(type, filename, args)
      end
    end

+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ module JsDuck
            files << fname
          end
        else
          Logger.warn(nil, "File not found", fname)
          Logger.warn(nil, "File not found", {:filename => fname})
        end

        files
Loading