Permalink
Browse files

Add basic suppression support based on matching constants

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

0 comments on commit 7171062

Please sign in to comment.