Skip to content
This repository
Browse code

Add basic suppression support based on matching constants

  • Loading branch information...
commit 71710620da084a227c71143ed92d5fcb442866c3 1 parent db88f80
Kevin Clark authored March 20, 2009
3  Changelog
... ...
@@ -1,3 +1,6 @@
  1
+== HEAD / DATE
  2
+ * Suppression file support [NEW]
  3
+
1 4
 == 1.0.1 / 3-19-09
2 5
  * Typo in runner [FIXED]
3 6
 
14  bin/nitpick
@@ -13,24 +13,28 @@ opts = Trollop.options do
13 13
   banner <<-BANNER
14 14
 Nitpick is a lint-like static code analyzer for Ruby.
15 15
 
16  
-Usage: `nitpick [(--only str | --except str)*|-h] file1.rb file2.rb`
  16
+Usage: `nitpick [(--only str | --except str | --supp file)*|-h] file1.rb file2.rb`
17 17
 
18 18
 By default, nitpick will analyze *all* loaded code. To reduce the noise:
19 19
 
20 20
 BANNER
21  
-  opt :only, "Nitpick only classes/modules that match this string.", :type => :string, :multi => true
22  
-  opt :except, "Don't nitpick classes/modules that match this string.", :type => :string, :multi => true
  21
+  opt :only,    "Nitpick only classes/modules that match this string.", :type => :string, :multi => true
  22
+  opt :except,  "Don't nitpick classes/modules that match this string.", :type => :string, :multi => true
  23
+  opt :supp,    "Suppress warnings based on a suppression file", :type => :io, :multi => true
23 24
 end
24 25
 
25 26
 # TODO(kevinclark) 2009-03-19: Figure out how to make this print -h
26 27
 Trollop.die "I need something to nitpick. Gimmie a filename or two. Or ten" if ARGV.size == 0
27 28
 
28  
-NitpickOptions = { :only => opts[:only] || [], :except => opts[:except] || []}
  29
+NitpickOptions = { :only => opts[:only] || [], :except => opts[:except] || [], :supp => opts[:supp] || [] }
  30
+
29 31
 Nitpickers = [
30 32
   Nitpick::ArgumentNitpicker, Nitpick::BlockNitpicker, Nitpick::BranchNitpicker,
31 33
   Nitpick::LocalVariableNitpicker, Nitpick::MethodNitpicker, Nitpick::RescueNitpicker
32 34
 ]
33 35
 
  36
+SuppressionRules = NitpickOptions[:supp].map {|supp| Nitpick::Suppression.new(supp.read) }
  37
+
34 38
 class Module
35 39
   # So we don't set off -w
36 40
   alias_method :original_method_added, :method_added
@@ -43,6 +47,8 @@ class Module
43 47
     # Only means it must match one
44 48
     return unless NitpickOptions[:only].find {|namespace|  /#{namespace}/ =~ self.to_s } or
45 49
                     NitpickOptions[:only].empty?
  50
+    # skip if any of our rules say so
  51
+    return if SuppressionRules.any? {|rule| rule.suppress?(self.to_s) }
46 52
     
47 53
     warnings = Nitpickers.map do |nitpicker_class|
48 54
       nitpicker = nitpicker_class.new(self, name)
2  lib/nitpick.rb
@@ -4,6 +4,8 @@
4 4
 require 'parse_tree'
5 5
 require 'ruby2ruby'
6 6
 
  7
+require 'nitpick/supression'
  8
+
7 9
 require 'nitpick/warnings/simple_warning'
8 10
 require 'nitpick/warnings/unused_variable'
9 11
 require 'nitpick/warnings/unprotected_block'
32  lib/nitpick/supression.rb
... ...
@@ -0,0 +1,32 @@
  1
+require 'yaml'
  2
+
  3
+module Nitpick
  4
+  class Suppression < SexpProcessor
  5
+    # Load suppression data from a file
  6
+    def self.open(file)
  7
+      raise ArgumentError.new("No file #{file.inspect} found") unless File.exists?(file)
  8
+      self.new(File.read(file))
  9
+    end
  10
+    
  11
+    # Create a Suppression object from yaml
  12
+    def initialize(yaml)
  13
+      @rules = YAML.load(yaml)
  14
+    end
  15
+    
  16
+    def ==(other)
  17
+      @rules == other.rules
  18
+    end
  19
+    
  20
+    def suppress?(const)
  21
+      return true if @rules.any? { |rule| /^#{rule["constant"]}/ =~ const }
  22
+      
  23
+      false
  24
+    end
  25
+    
  26
+    protected
  27
+    
  28
+      def rules
  29
+        @rules
  30
+      end
  31
+  end
  32
+end
8  spec/fixtures/suppressions.yaml
... ...
@@ -0,0 +1,8 @@
  1
+[
  2
+{
  3
+  constant: Sinatra
  4
+},
  5
+{
  6
+  constant: Rails
  7
+}
  8
+]
17  spec/suppression_file_spec.rb
... ...
@@ -0,0 +1,17 @@
  1
+require File.dirname(__FILE__) + '/spec_helper'
  2
+
  3
+describe Nitpick::Suppression do
  4
+  before do
  5
+    @supp_file = File.join(File.dirname(__FILE__), *%w[fixtures suppressions.yaml])
  6
+    @supp = Nitpick::Suppression.new(File.read(@supp_file))
  7
+  end
  8
+  
  9
+  it "should open options from a file" do
  10
+    Nitpick::Suppression.open(@supp_file).should == @supp
  11
+  end
  12
+  
  13
+  it "should suppress based on a constant" do
  14
+    @supp.suppress?("Sinatra").should be_true
  15
+    @supp.suppress?("Object").should be_false
  16
+  end
  17
+end

0 notes on commit 7171062

Please sign in to comment.
Something went wrong with that request. Please try again.