Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move interesting_nodes and interesting_files to CodeAnalyzer
  • Loading branch information
flyerhzm committed Sep 4, 2012
1 parent 1eb575e commit 1f5a60b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lib/code_analyzer.rb
Expand Up @@ -2,6 +2,7 @@
require 'code_analyzer/sexp'

module CodeAnalyzer
autoload :CheckingVisitor, "code_analyzer/checking_visitor"
autoload :AnalyzerException, "code_analyzer/analyzer_exception"
autoload :Checker, "code_analyzer/checker"
autoload :CheckingVisitor, "code_analyzer/checking_visitor"
end
55 changes: 55 additions & 0 deletions lib/code_analyzer/checker.rb
@@ -0,0 +1,55 @@
# encoding: utf-8
module CodeAnalyzer
# A checker class that takes charge of checking the sexp.
class Checker
# interesting nodes that the check will parse.
def interesting_nodes
self.class.interesting_nodes
end

# interesting files that the check will parse.
def interesting_files
self.class.interesting_files
end

# check if the checker will parse the node file.
#
# @param [String] the file name of node.
# @return [Boolean] true if the checker will parse the file.
def parse_file?(node_file)
interesting_files.any? { |pattern| node_file =~ pattern }
end

# add error if source code violates rails best practice.
#
# @param [String] message, is the string message for violation of the rails best practice
# @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
)
end

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

class <<self
def interesting_nodes(*nodes)
@interesting_nodes ||= []
@interesting_nodes += nodes
end

def interesting_files(*file_patterns)
@interesting_files ||= []
@interesting_files += file_patterns
end
end
end
end
24 changes: 1 addition & 23 deletions lib/rails_best_practices/core/check.rb
Expand Up @@ -2,7 +2,7 @@
module RailsBestPractices
module Core
# A Check class that takes charge of checking the sexp.
class Check
class Check < CodeAnalyzer::Checker
ALL_FILES = /.*/
CONTROLLER_FILES = /app\/(controllers|cells)\/.*\.rb$/
MIGRATION_FILES = /db\/migrate\/.*\.rb$/
Expand All @@ -24,16 +24,6 @@ def initialize(options={})
end
end

# interesting nodes that the check will parse.
def interesting_nodes
self.class.interesting_nodes
end

# interesting files that the check will parse.
def interesting_files
self.class.interesting_files
end

# check if the check will need to parse the node file.
#
# @param [String] the file name of node.
Expand Down Expand Up @@ -128,18 +118,6 @@ def method_missing(method_name, *args)
end

class <<self
def interesting_nodes(*nodes)
@interesting_nodes ||= []
@interesting_nodes += nodes
@interesting_nodes.uniq
end

def interesting_files(*file_patterns)
@interesting_files ||= []
@interesting_files += file_patterns
@interesting_files.uniq
end

# callbacks for start_xxx and end_xxx.
def callbacks
@callbacks ||= {}
Expand Down
28 changes: 28 additions & 0 deletions spec/code_analyzer/checker_spec.rb
@@ -0,0 +1,28 @@
require 'spec_helper'

module CodeAnalyzer
describe Checker do
let(:checker) { Checker.new }


it "should get empty interesting nodes" do
checker.interesting_nodes.should == []
end

it "should match none of interesting files" do
checker.interesting_files.should == []
end

context "#parse_file?" do
it "should return true if node_file matches pattern" do
checker.stub(:interesting_files).and_return([/spec\/.*\.rb/, /lib\/.*\.rb/])
checker.parse_file?("lib/code_analyzer.rb").should be_true
end

it "should return false if node_file doesn't match pattern" do
checker.stub(:interesting_files).and_return([/spec\/.*\.rb/])
checker.parse_file?("lib/code_analyzer.rb").should be_false
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -2,8 +2,8 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))

require 'rspec'
require 'rails_best_practices'
require 'code_analyzer'
require 'rails_best_practices'

RSpec.configure do |config|
config.after do
Expand Down

0 comments on commit 1f5a60b

Please sign in to comment.