Skip to content

Commit

Permalink
Remove dependency on tailor.
Browse files Browse the repository at this point in the history
New version is way too slow, and we only need very simple checks
anyways.
  • Loading branch information
xaviershay committed Apr 21, 2012
1 parent 2ce6007 commit 1ae7138
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 82 deletions.
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ PATH
remote: .
specs:
cane (1.2.0)
tailor

GEM
remote: http://rubygems.org/
Expand All @@ -22,9 +21,6 @@ GEM
multi_json (~> 1.0.3)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
tailor (0.1.5)
term-ansicolor (>= 1.0.5)
term-ansicolor (1.0.7)

PLATFORMS
ruby
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ begin

desc "Run cane to check quality metrics"
Cane::RakeTask.new(:quality) do |cane|
cane.abc_max = 10
cane.abc_max = 11
cane.add_threshold 'coverage/covered_percent', :>=, 100
end

Expand Down
1 change: 0 additions & 1 deletion cane.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Gem::Specification.new do |gem|
gem.executables << "cane"
gem.version = Cane::VERSION
gem.has_rdoc = false
gem.add_dependency 'tailor', '~> 0'
gem.add_development_dependency 'rspec', '~> 2.0'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'simplecov'
Expand Down
99 changes: 23 additions & 76 deletions lib/cane/style_check.rb
Original file line number Diff line number Diff line change
@@ -1,97 +1,44 @@
require 'tailor'

require 'cane/style_violation'

module Cane

# Creates violations for files that do not meet style conventions. This uses
# the `tailor` gem, configured to exclude some of its less reliable checks
# (mostly spacing around punctuation).
# Creates violations for files that do not meet style conventions. Only
# highly obvious, probable, and non-controversial checks are performed here.
# It is not the goal of the tool to provide an extensive style report, but
# only to prevent studid mistakes.
class StyleCheck < Struct.new(:opts)
def violations
Dir[opts.fetch(:files)].map do |file_name|
find_violations_in_file(file_name)
file_list.map do |file_path|
map_lines(file_path) do |line, line_number|
violations_for_line(line.chomp).map do |message|
StyleViolation.new(file_path, line_number + 1, message)
end
end
end.flatten
end

protected

def find_violations_in_file(file_name)
source = File.open(file_name, 'r:utf-8')
file_path = Pathname.new(file_name)

source.each_line.map.with_index do |source_line, line_number|
violations_for_line(file_path, source_line, line_number)
def violations_for_line(line)
result = []
if line.length > measure
result << "Line is >%i characters (%i)" % [measure, line.length]
end
result << "Line contains trailing whitespace" if line =~ /\s$/
result << "Line contains hard tabs" if line =~ /\t/
result
end

def violations_for_line(file_path, source_line, line_number)
FileLine.new(source_line, opts).problems.map do |message|
StyleViolation.new(file_path, line_number + 1, message)
end
end
end

# The `tailor` gem was not designed to be used as a library, so interfacing
# with it is a bit of a mess. This wrapper is attempt to confine that mess to
# a single point in the code.
class FileLine < Tailor::FileLine
attr_accessor :opts

def initialize(source_line, opts)
self.opts = opts

super(source_line, nil, nil)
end

def find_problems
# This is weird. These methods actually have side-effects! We capture
# the effects my monkey-patching #print_problem below.
spacing_problems
method_line? && camel_case_method?
class_line? && snake_case_class?
too_long?
end

def print_problem(message)
@problems << message.gsub(/\[.+\]\s+/, '')
def file_list
Dir[opts.fetch(:files)]
end

def problems
@problems = []
find_problems
@problems
end

# A copy of the parent method that only uses a small subset of the spacing
# checks we actually want (the others are too buggy or controversial).
def spacing_problems
spacing_conditions.each_pair do |condition, values|
unless self.scan(values.first).empty?
print_problem values[1]
end
end
end

def spacing_conditions
SPACING_CONDITIONS.select {|k, _|
[:hard_tabbed, :trailing_whitespace].include?(k)
}
end

# Copy of parent method using a configurable line length.
def too_long?
length = self.chomp.length
if length > line_length_max
print_problem "Line is >#{line_length_max} characters (#{length})"
return true
end

false
def measure
opts.fetch(:measure)
end

def line_length_max
opts.fetch(:measure)
def map_lines(file_path, &block)
File.open(file_path).each_line.map.with_index(&block)
end
end

Expand Down

0 comments on commit 1ae7138

Please sign in to comment.