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

Add parameters to :tag warning type.

Allows one to supply a list of unknown tag names on which the
warning is not shown.

Fixes #500
parent 6c704d74
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ module JsDuck

          skip_white
        else
          warn(:tag, "Unsupported tag: @#{name}")
          warn(:tag, "Unsupported tag: @#{name}", [name.to_sym])
          @multiline_tag[:doc] += "@"
        end
      end
+2 −2
Original line number Diff line number Diff line
@@ -73,8 +73,8 @@ module JsDuck
      end

      # Prints a warning message
      def warn(type, msg)
        Logger.warn(type, msg, @position)
      def warn(type, msg, params=[])
        Logger.warn(type, msg, @position, params)
      end

    end
+4 −1
Original line number Diff line number Diff line
require 'jsduck/warning/basic'
require 'jsduck/warning/nodoc'
require 'jsduck/warning/tag'
require 'jsduck/warning/deprecated'
require 'jsduck/warning/all'
require 'jsduck/warning/warn_exception'
@@ -19,7 +20,6 @@ module JsDuck
          [:global, "Member doesn't belong to any class"],
          [:inheritdoc, "@inheritdoc referring to unknown class or member"],
          [:extend, "@extend/mixin/requires/uses referring to unknown class"],
          [:tag, "Use of unsupported @tag"],
          [:tag_repeated, "An @tag used multiple times, but only once allowed"],
          [:tag_syntax, "@tag syntax error"],
          [:link, "{@link} to unknown class or member"],
@@ -52,6 +52,9 @@ module JsDuck
          register(w[0], Warning::Basic.new(w[0], w[1]))
        end

        # :tag warning
        register(:tag, Warning::Tag.new)

        # :nodoc warning
        register(:nodoc, Warning::Nodoc.new)

+57 −0
Original line number Diff line number Diff line
module JsDuck
  module Warning

    # Unknown tag warning.
    class Tag

      # Creates the :tag warning type
      def initialize
        @rules = []
        # disable by default
        set(false)
      end

      # Enables or disables a particular sub-warning
      def set(enabled, path_pattern=nil, tagnames=[])
        @rules.unshift({
          :enabled => enabled,
          :tagnames => tagnames,
          :path_re => path_pattern ? Regexp.new(Regexp.escape(path_pattern)) : nil
        })
      end

      # True when the warning is enabled for the given filename and
      # params combination where params contains one tagname.
      def enabled?(filename="", params=[])
        tagname = params[0]

        # Filter out the most recently added rule that applies to our current item
        match = @rules.find do |r|
          (r[:tagnames].empty? || r[:tagnames].include?(tagname)) &&
            (r[:path_re].nil? || r[:path_re] =~ filename)
        end

        return match[:enabled]
      end

      # Extensive documentation for :nodoc warning
      def doc
        [
          "",
          " +tag(<name1>,<name2>,...) - Use of unsupported @tag",
          "",
          "     This warning type can optionally take a list of tag names",
          "     to limit its effect to only these tags.",
          "",
          "     So, to disable warnings for JavaDoc tags @file and @overview",
          "     which aren't supported by JSDuck:",
          "",
          "         --warnings='-tag(file,overview)'",
          "",
        ]
      end

    end

  end
end
+42 −0
Original line number Diff line number Diff line
@@ -96,6 +96,48 @@ describe JsDuck::Warning::Registry do
    end
  end

  describe "after enabling :tag warning" do
    before do
      warnings.set(:tag, true)
    end

    it "has the :tag warning enabled for a @footag" do
      warnings.enabled?(:tag, "foo.js", [:footag]).should == true
    end

    describe "and disabling it for @footag and @bartag" do
      before do
        warnings.set(:tag, false, nil, [:footag, :bartag])
      end

      it "has the :tag warning disabled for a @footag" do
        warnings.enabled?(:tag, "bar.js", [:footag]).should == false
      end

      it "has the :tag warning disabled for a @bartag" do
        warnings.enabled?(:tag, "bar.js", [:bartag]).should == false
      end

      it "has the :tag warning still enabled for a @mytag" do
        warnings.enabled?(:tag, "bar.js", [:mytag]).should == true
      end

      describe "and enabling it for files in /foo/" do
        before do
          warnings.set(:tag, true, "/foo/")
        end

        it "has the :tag warning enabled for a @footag in /foo/" do
          warnings.enabled?(:tag, "/foo/bar.js", [:footag]).should == true
        end

        it "still has the :tag warning disabled for a @footag outside of /foo/" do
          warnings.enabled?(:tag, "bar.js", [:footag]).should == false
        end
      end
    end
  end

  describe "after enabling :nodoc warning" do
    before do
      warnings.set(:nodoc, true)