Commit caaf7078 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Allow mixing disabled/enabled PATHs in warnings.

Implement the basic warning type like the Nodoc warning, so we can
combine +link:/src with -link:/src/ux
parent ecd107b5
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -757,10 +757,6 @@ module JsDuck
          "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)",
          "",
+18 −15
Original line number Diff line number Diff line
@@ -11,34 +11,37 @@ module JsDuck
      def initialize(type, msg)
        @type = type
        @msg = msg
        @enabled = false
        @patterns = []

        @rules = []
        # disable by default
        set(false)
      end

      # Enables or disables the warning.
      # Optionally enables/disables it for files matching a path_pattern.
      # params array is not used for the basic warning type.
      def set(enabled, path_pattern=nil, params=[])
        if path_pattern
          # When warning is already enabled, enabling a path will have no effect.
          # Similarly when it's disabled, disabling a path has also no effect.
          # Therefore we'll just ignore a setting like that.
          if @enabled != enabled
            @patterns << Regexp.new(Regexp.escape(path_pattern))
          end
          @rules << {
            :enabled => enabled,
            :path_re => Regexp.new(Regexp.escape(path_pattern))
          }
        else
          @enabled = enabled
          @patterns = []
          # When no path specified, the warning is turned on/off
          # globally, so we can discard all the existing rules and
          # start over with just one.
          @rules = [{
            :enabled => enabled,
            :path_re => nil
          }]
        end
      end

      # True when warning is enabled for the given filename.
      # (The params parameter is ignored).
      def enabled?(filename="", params=[])
        if @patterns.any? {|re| filename =~ re }
          !@enabled
        else
          @enabled
        end
        # Filter out rules that apply to our current item
        @rules.find_all {|r| r[:path_re].nil? || r[:path_re] =~ filename }.last[:enabled]
      end

      # Documentation for the warning.
+14 −0
Original line number Diff line number Diff line
@@ -80,6 +80,20 @@ describe JsDuck::Warning::Registry do
        warnings.enabled?(:tag, "/other/path/file.js").should == true
      end
    end

    describe "and disabling it in /some/path/sub/path" do
      before do
        warnings.set(:tag, false, "/some/path/sub/path")
      end

      it "has the :tag warning enabled for /some/path/file.js" do
        warnings.enabled?(:tag, "/some/path/file.js").should == true
      end

      it "has the :tag warning disabled for /some/path/sub/path/file.js" do
        warnings.enabled?(:tag, "/some/path/sub/path/file.js").should == false
      end
    end
  end

  describe "after enabling :nodoc warning" do