forked from flyerhzm/rails_best_practices
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
move interesting_nodes and interesting_files to CodeAnalyzer
- Loading branch information
Showing
5 changed files
with
87 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters