Commit 926521d0 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Enabling of warnings within a specific path.

Reworked the warnings system to allow one to specify a path pattern
to enable or disable a set of warnings only within that path.
parent 9920e2bc
Loading
Loading
Loading
Loading
+37 −10
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ module JsDuck
      # When running JSDuck app, the Options class enables most warnings.
      @warnings = {}
      @warning_docs.each do |w|
        @warnings[w[0]] = false
        @warnings[w[0]] = {:enabled => false, :patterns => []}
      end

      @shown_warnings = {}
@@ -65,15 +65,26 @@ module JsDuck
      end
    end

    # Enabled or disables a particular warning
    # or all warnings when type == :all
    def set_warning(type, enabled)
    # Enables or disables a particular warning
    # or all warnings when type == :all.
    # Additionally a filename pattern can be specified.
    def set_warning(type, enabled, pattern=nil)
      if type == :all
        # When used with a pattern, only add the pattern to the rules
        # where it can have an effect - otherwise we get a warning.
        @warnings.each_key do |key|
          @warnings[key] = enabled
          set_warning(key, enabled, pattern) unless pattern && @warnings[key][:enabled] == enabled
        end
      elsif @warnings.has_key?(type)
        @warnings[type] = enabled
        if pattern
          if @warnings[type][:enabled] == enabled
            warn(nil, "Warning rule '#{enabled ? '+' : '-'}#{type}:#{pattern}' has no effect")
          else
            @warnings[type][:patterns] << Regexp.new(Regexp.escape(pattern))
          end
        else
          @warnings[type] = {:enabled => enabled, :patterns => []}
        end
      else
        warn(nil, "Warning of type '#{type}' doesn't exist")
      end
@@ -81,7 +92,7 @@ module JsDuck

    # get documentation for all warnings
    def doc_warnings
      @warning_docs.map {|w| " #{@warnings[w[0]] ? '+' : '-'}#{w[0]} - #{w[1]}" }
      @warning_docs.map {|w| " #{@warnings[w[0]][:enabled] ? '+' : '-'}#{w[0]} - #{w[1]}" }
    end

    # Prints warning message.
@@ -107,18 +118,34 @@ module JsDuck

      msg = paint(:yellow, "Warning: ") + format(filename, line) + " " + msg

      if type == nil || @warnings[type]
      if warning_enabled?(type, filename)
        if !@shown_warnings[msg]
          $stderr.puts msg
          @shown_warnings[msg] = true
        end
      elsif !@warnings.has_key?(type)
        warn(nil, "Unknown warning type #{type}")
      end

      return false
    end

    # True when the warning is enabled for the given type and filename
    # combination.
    def warning_enabled?(type, filename)
      if type == nil
        true
      elsif !@warnings.has_key?(type)
        warn(nil, "Unknown warning type #{type}")
        false
      else
        rule = @warnings[type]
        if rule[:patterns].any? {|re| filename =~ re }
          !rule[:enabled]
        else
          rule[:enabled]
        end
      end
    end

    # Formats filename and line number for output
    def format(filename=nil, line=nil)
      out = ""
+19 −3
Original line number Diff line number Diff line
@@ -605,17 +605,33 @@ module JsDuck
        opts.on('--warnings=+A,-B,+C', Array,
          "Turns warnings selectively on/off.",
          "",
          " +all - to turn on all warnings",
          " +all - to turn on all warnings.",
          " -all - to turn off all warnings.",
          "",
          "Additionally a pattern can be specified to only apply the",
          "setting for a particular set of files.  For example to turn",
          "off all warnings related to chart classes:",
          "",
          " -all:extjs/src/chart",
          "",
          "Note, that the order of the rules matters.  When you first",
          "say +link and then -all, the result will be that all warnings",
          "get disabled.",
          "",
          "Currently one can't mix disabling and enabling file patterns.",
          "For example  --warnings=-link,+link:/src,-link:/src/ux  will",
          "ignore the last rule about /src/ux.",
          "",
          "List of all available warning types:",
          "(Those with '+' in front of them default to on)",
          "",
          *Logger.doc_warnings) do |warnings|
          warnings.each do |op|
            if op =~ /^([-+]?)(.*)$/
            if op =~ /^([-+]?)(\w+)(?::(.*))?$/
              enable = !($1 == "-")
              name = $2.to_sym
              Logger.set_warning(name, enable)
              path = $3
              Logger.set_warning(name, enable, path)
            end
          end
        end