Commit 2c74e623 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Minor optimization of warning rules patterns search.

Instead of looking up all the matching rules and taking the last one,
maintain the rules array in reverse order and use #find to grab the
first matching rule.
parent caaf7078
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -22,10 +22,12 @@ module JsDuck
      # params array is not used for the basic warning type.
      def set(enabled, path_pattern=nil, params=[])
        if path_pattern
          @rules << {
          # Prepend to the front of array, so we can use #find to
          # search for the latest rule.
          @rules.unshift({
            :enabled => enabled,
            :path_re => Regexp.new(Regexp.escape(path_pattern))
          }
          })
        else
          # When no path specified, the warning is turned on/off
          # globally, so we can discard all the existing rules and
@@ -40,8 +42,8 @@ module JsDuck
      # True when warning is enabled for the given filename.
      # (The params parameter is ignored).
      def enabled?(filename="", params=[])
        # Filter out rules that apply to our current item
        @rules.find_all {|r| r[:path_re].nil? || r[:path_re] =~ filename }.last[:enabled]
        # Find the most recently added rule that has an effect to our current item
        @rules.find {|r| r[:path_re].nil? || r[:path_re] =~ filename }[:enabled]
      end

      # Documentation for the warning.
+5 −5
Original line number Diff line number Diff line
@@ -26,12 +26,12 @@ module JsDuck
          raise WarnException, "Invalid warning parameters: nodoc(#{type},#{visibility})"
        end

        @rules << {
        @rules.unshift({
          :enabled => enabled,
          :type => type,
          :visibility => visibility,
          :path_re => path_pattern ? Regexp.new(Regexp.escape(path_pattern)) : nil
        }
        })
      end

      # True when the warning is enabled for the given filename and
@@ -41,14 +41,14 @@ module JsDuck
        type = params[0]
        visibility = params[1]

        # Filter out rules that apply to our current item
        matches = @rules.find_all do |r|
        # Filter out the most recently added rule that applies to our current item
        match = @rules.find do |r|
          (r[:type].nil? || r[:type] == type) &&
            (r[:visibility].nil? || r[:visibility] == visibility) &&
            (r[:path_re].nil? || r[:path_re] =~ filename)
        end

        return matches.last[:enabled]
        return match[:enabled]
      end

      # Extensive documentation for :nodoc warning