Skip to content

Commit

Permalink
add Warning to code_analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Sep 4, 2012
1 parent 3233ec2 commit f35a2b1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/code_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module CodeAnalyzer
autoload :AnalyzerException, "code_analyzer/analyzer_exception"
autoload :Checker, "code_analyzer/checker"
autoload :CheckingVisitor, "code_analyzer/checking_visitor"
autoload :Warning, "code_analyzer/warning"
end
19 changes: 6 additions & 13 deletions lib/code_analyzer/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,17 @@ def parse_file?(node_file)
interesting_files.any? { |pattern| node_file =~ pattern }
end

# add error if source code violates rails best practice.
# add an warning.
#
# @param [String] message, is the string message for violation of the rails best practice
# @param [String] message, is the warning message
# @param [String] filename, is the filename of source code
# @param [Integer] line_number, is the line number of the source code which is reviewing
def add_error(message, filename = @node.file, line_number = @node.line)
errors << RailsBestPractices::Core::Error.new(
filename: filename,
line_number: line_number,
message: message,
type: self.class.to_s,
url: url
)
def add_warning(message, filename = @node.file, line_number = @node.line)
warnings << Warning.new(filename: filename, line_number: line_number, message: message)
end

# errors that vialote the rails best practices.
def errors
@errors ||= []
def warnings
@warnings ||= []
end

class <<self
Expand Down
19 changes: 19 additions & 0 deletions lib/code_analyzer/warning.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# encoding: utf-8
module CodeAnalyzer
# Warning is the violation.
#
# it indicates the filenname, line number and error message for the violation.
class Warning
attr_reader :filename, :line_number, :message

def initialize(options={})
@filename = options[:filename]
@line_number = options[:line_number].to_s
@message = options[:message]
end

def to_s
"#{@filename}:#{@line_number} - #{@message}"
end
end
end
12 changes: 3 additions & 9 deletions lib/rails_best_practices/core/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ module Core
# Error is the violation to rails best practice.
#
# it indicates the filenname, line number and error message for the violation.
class Error
attr_reader :filename, :line_number, :message, :type, :url
class Error < CodeAnalyzer::Warning
attr_reader :type, :url
attr_accessor :git_commit, :git_username, :hg_commit, :hg_username, :highlight

def initialize(options={})
@filename = options[:filename]
@line_number = options[:line_number].to_s
@message = options[:message]
super
@type = options[:type]
@url = options[:url]
@git_commit = options[:git_commit]
Expand All @@ -28,10 +26,6 @@ def short_filename
def first_line_number
line_number.split(',').first
end

def to_s
"#{@filename}:#{@line_number} - #{@message}"
end
end
end
end
12 changes: 12 additions & 0 deletions spec/code_analyzer/warning_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'

module CodeAnalyzer
describe Warning do
it "should return error with filename, line number and message" do
Warning.new(
filename: "app/models/user.rb",
line_number: "100",
message: "not good").to_s.should == "app/models/user.rb:100 - not good"
end
end
end

0 comments on commit f35a2b1

Please sign in to comment.